文件名中的JavaMail和非ASCII字符
我可以在
JavaMail中发送具有非ascii文件名的附件,但我无法下载它们.我特意为那些文件名包含非ascii字符的附件获取java.io.FileNotFoundException.
仅供参考:我正在使用类似messageBodyPart.setFileName(MimeUtility.encodeText(filename [i]))来编码文本和MimeUtility.decodeText(bodyPart.getFileName())来解码非ascii文件名 这有解决方法吗? 编辑 if (message[a].getContent() instanceof MimeMultipart) { Multipart multipart = (Multipart) message[a].getContent(); for (int i = 0; i < multipart.getCount(); i++) { bodyPart = multipart.getBodyPart(i); disposition = bodyPart.getDisposition(); if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT) || (disposition.equals(BodyPart.INLINE)))) { DataHandler handler = bodyPart.getDataHandler(); String path = bodyPart.getFileName(); String[] str = path.split("/"); String fileName = str[str.length - 1]; String filePath = ReadConfigPropertiesFile.getPropertyValue("server.buildpath"); System.out.println(fileName); File tempDir = new File(filePath + user); if (!tempDir.exists()) { tempDir.mkdir(); } File saveFile = new File(tempDir + "/" + fileName); int count = 0; while (saveFile.exists()) { count++; saveFile = new File(tempDir + "/" + count + "_" + fileName); } BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFile)); byte[] buff = new byte[2048]; InputStream is = bodyPart.getInputStream(); int ret = 0; while ((ret = is.read(buff)) > 0) { bos.write(buff,ret); } bos.close(); is.close(); //System.out.println(bodyPart.getContentType()); }else { //display body (message) of the attachment; //System.out.println(bodyPart.getContent().toString()); } } } 上面的代码在BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFile))行引发了F??ileNotFoundException异常,并且对于文件名为非ascii字符的附件(类似于???.pdf)引发了这种情况.其他一切都很好. 解决方法
这个答案取自@semytech(OP)的评论.很难在那里找到它,所以我会将其添加为更具可见性的答案.它帮助我使用希伯来文件名.
MimeBodyPart attachment = new MimeBodyPart(); attachment.setFileName(MimeUtility.encodeText(filename,"UTF-8",null)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |