采用的是axis2 webservice 上传文件到服务端,开始采用图片,可是通了,本以为ok了,可是换成其他格式就抛出异常了:
name:null
type:text/plain
size:0
java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Unknown Source)
at java.util.regex.Matcher.reset(Unknown Source)
at java.util.regex.Matcher.<init>(Unknown Source)
at java.util.regex.Pattern.matcher(Unknown Source)
实在不明白。求高手帮助
客服端上传代码
SendMessage sendMms = sml.getSendMessage(new java.net.URL( "http://127.0.0.1:8080/WebServiceProject/services/SendMessage")); for (int i = 0; i < mmsFileList.length; i++) { File f = new File(mmsFileList[i]); System.out.println("fname:"+f.getName()); ((SendMessageBindingStub) sendMms) .addAttachment(new DataHandler(new FileDataSource(f))); }
服务端获取代码:
try { //Attachments attachments = new Attachment(); org.apache.axis.MessageContext msgCtx = org.apache.axis.MessageContext.getCurrentContext(); Iterator attachments=msgCtx.getCurrentMessage().getAttachments(); while(attachments.hasNext()) { AttachmentPart ap=(AttachmentPart) attachments.next(); System.out.println("name:"+ap.getAttachmentFile()); System.out.println("type:"+ap.getContentType()); System.out.println("size:"+ap.getSize()); Pattern p = Pattern.compile(".*\\\\(\\w*).att"); Matcher m = p.matcher(ap.getAttachmentFile()); m.matches(); MatchResult result=m.toMatchResult(); String filename=result.group(1); File file = null; if (ap.getContentType().contains("text")) { file = new File("C:\\" + filename + ".txt"); //文本附件 } else if (ap.getContentType().contains("image")) { System.out.println("image:"); file = new File("C:\\"+filename+".jpg"); //图片附件 } else if (ap.getContentType().contains("application")) { System.out.println("smil:"); file = new File("C:\\"+filename+".smi"); //图片附件 } else { System.out.println("other"); file = new File("C:\\"+filename+".dat"); } FileOutputStream fileOutputStream = new FileOutputStream(file); ap.getActivationDataHandler().writeTo(fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); } } catch (Exception e) { e.printStackTrace(); }
图片能成功发送,其他格式文件不支持,拜托各位大神了,小弟在线等 qq:1004143305 雪地里跪求啊
用到的jar包:
activation.jar
mail.jar
axis2
实在对不起大家,误导大家了,这么嗝屁的问题,我想也不可能出嘛。
出现这问题有两个原因,
1:》我传的txt本来就是空的,后来无意中发觉了,实在对不起大家。
2:》
AttachmentPart ap=(AttachmentPart) attachments.next(); System.out.println("name:"+ap.getAttachmentFile());
这样确实取不出来txt的名字,但能取出图片的名字,到底为什么不想深究, 取得文件名应该这样取:
ap.getDataHandler().getName()
over!!!!
sorry