package cn.com.henan.net.servlet; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jspsmart.upload.SmartUpload; import com.jspsmart.upload.SmartUploadException; import cn.com.henan.socket.MobileNumberErrerExce; import cn.com.henan.socket.newsDetailSMS; import cn.com.henan.socket.newsSockBase; import cn.com.henan.util.DelFileUtil; import cn.com.henan.util.readTxtData; public class DetailSMSverlet extends HttpServlet { //彩信发送 private static final long serialVersionUID = 1L; public DetailSMSverlet() { super(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public byte[] gbk2utf8(String chenese){ char c[] = chenese.toCharArray(); byte [] fullByte =new byte[3*c.length]; for(int i=0; i<c.length; i++){ int m = (int)c[i]; String word = Integer.toBinaryString(m); // System.out.println(word); StringBuffer sb = new StringBuffer(); int len = 16 - word.length(); //补零 for(int j=0; j<len; j++){ sb.append("0"); } sb.append(word); sb.insert(0, "1110"); sb.insert(8, "10"); sb.insert(16, "10"); // System.out.println(sb.toString()); String s1 = sb.substring(0, 8); String s2 = sb.substring(8, 16); String s3 = sb.substring(16); byte b0 = Integer.valueOf(s1, 2).byteValue(); byte b1 = Integer.valueOf(s2, 2).byteValue(); byte b2 = Integer.valueOf(s3, 2).byteValue(); byte[] bf = new byte[3]; bf[0] = b0; fullByte[i*3] = bf[0]; bf[1] = b1; fullByte[i*3+1] = bf[1]; bf[2] = b2; fullByte[i*3+2] = bf[2]; } return fullByte; } private String utf8Togb2312(String str){ StringBuffer sb = new StringBuffer(); for(int i=0; i<str.length(); i++) { char c = str.charAt(i); switch (c) { case '+': sb.append(' '); break; case '%': try { sb.append((char)Integer.parseInt( str.substring(i+1,i+3),16)); } catch (NumberFormatException e) { throw new IllegalArgumentException(); } i += 2; break; default: sb.append(c); break; } } // Undo conversion to external encoding String result = sb.toString(); String res=null; try{ byte[] inputBytes = result.getBytes("8859_1"); res= new String(inputBytes,"UTF-8"); } catch(Exception e){} return res; } //彩信发送 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter writer = response.getWriter(); String action = request.getParameter("action"); if("number".equals(action)){ String mbNumber= request.getParameter("mbNumber"); String[]mbNumbers = mbNumber.split(";"); String mmsid= new String ( request.getParameter("mmsid").getBytes("ISO-8859-1"),"utf-8") ; byte b[]=mmsid.getBytes(Charset.forName("gb2312")); // java.net.URLDecoder.decode("name", "UTF-8"); System.out.println(mmsid); // System.out.println("mmsid:"+new String (mmsid.getBytes("ISO-8859-1"),"utf-8") ); System.out.println("mbNumber:"+ mbNumber ); //byte b[]="亀井金融相1".getBytes(Charset.forName("UTF8")); //SHIFT_JIS 日文编码 // System.out.println(b.length); newsSockBase newsTask = new newsSockBase(); newsTask.setIPandPort("20.0.0.31", 8888); newsDetailSMS SMS = new newsDetailSMS(); SMS.setType(4); SMS.setMessages(mmsid); SMS.setMessagesL(b.length ); SMS.setIsdnL(mbNumbers.length); SMS.setIsdn(mbNumbers); newsTask.start(SMS); System.out.println("彩信SMS.getResultMessage()"+SMS.getResultMessage()); writer.print("{success:true,result:'"+ SMS.getResultMessage()+ "'}"); }else if("upfile".equals(action)){ System.out.println("upfile"); String savePath=request.getRealPath("\\upload"); //System.out.println(savePath); SmartUpload mySmartUpload = new SmartUpload(); //String currentDirectoryPath=null; mySmartUpload.setForcePhysicalPath(true); mySmartUpload.initialize(this.getServletConfig(), request,response); // 1.限制每个上传文件的最大长度。10M mySmartUpload.setMaxFileSize(10000000); // 3.设定允许上传的文件(通过扩展名限制),仅允许 txt文件。 mySmartUpload.setAllowedFilesList("txt"); com.jspsmart.upload.SmartFile file=null; try { mySmartUpload.upload("utf-8"); } catch ( SecurityException e1) { } catch (NegativeArraySizeException e1) { } catch (SmartUploadException e1) { e1.printStackTrace(); } com.jspsmart.upload.SmartRequest req = mySmartUpload.getRequest(); // System.out.println(request.getCharacterEncoding()); String test= req.getParameter("messageDetail"); /* String mmsid= new String ( test.getBytes("gbk"),"ISO-8859-1") ; String mmsid2= new String ( mmsid.getBytes("ISO-8859-1"),"utf-8") ; System.out.println("mmsid2"+mmsid2);*/ //System.out.println( mmsid ); // String mmsid= utf8Togb2312(test); String mmsid= new String ( test.getBytes("utf-8"),"ISO-8859-1") ; String mmsid2= new String ( mmsid.getBytes("ISO-8859-1"),"utf-8") ; System.out.println("mmsid2"+mmsid2); byte b[]=mmsid2.getBytes(Charset.forName("gb2312")); /*-------------------------删除指定目录下的文件begin---------------------------------*/ DelFileUtil.delAllFile(savePath); /*------------------------删除指定目录下的文件end-----------------------------------*/ /*------------------------保存上传的文件 begin-----------------------------------*/ //保存文件 for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) { file = mySmartUpload.getFiles().getFile(i); if (file.isMissing()) continue; try { file.saveAs("/upload/" + file.getFileName()); } catch (SmartUploadException e){ e.printStackTrace(); } } /*------------------------保存上传的文件 end-----------------------------------*/ List<String> messages = new ArrayList <String>(); String Isdns[]=null; readTxtData readTD = new readTxtData(); System.out.println(savePath+"\\"+file.getFileName()); readTD.setTxtPath( savePath+"\\"+file.getFileName() ); try { Isdns =readTD.readTxtPath(""); } catch(FileNotFoundException f){ f.printStackTrace(); System.out.println("找不到文件"); return; } catch (IOException e1){ e1.printStackTrace(); } catch (MobileNumberErrerExce e){ //writer.print("{success:true,mobileNewspaper:'"+ "" +"',optype:'"+" "+"', opresult:'"+"号码格式错误"+"'}"); return ; } newsSockBase newsTask = new newsSockBase(); newsTask.setIPandPort("20.0.0.31", 8888); //短信发送 newsDetailSMS SMS = new newsDetailSMS(); SMS.setType(4); SMS.setMessages(mmsid2); SMS.setMessagesL(b.length ); SMS.setIsdnL(Isdns.length); SMS.setIsdn(Isdns); newsTask.start(SMS); System.out.println("彩信SMS.getResultMessage()"+SMS.getResultMessage()); writer.print("{success:true,result:'"+ SMS.getResultMessage()+ "'}"); } } }