7

我可以在 JavaMail 中发送具有非 ascii 文件名的附件,但我无法下载它们。我专门为那些文件名包含非 ascii 字符的附件获取 java.io.FileNotFoundException。

仅供参考:我正在使用类似messageBodyPart.setFileName(MimeUtility.encodeText(filename[i])) 编码文本和MimeUtility.decodeText(bodyPart.getFileName())解码非 ascii 文件名的东西

有解决方法吗?

编辑 @Bill,这是我读取附件的代码的一部分。我还在我的代码中添加了 properties.setProperty("mail.mime.decodeparameters", "true") 和 properties.setProperty("mail.mime.decodefilename", "true") 属性。

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, 0, ret);
                        }
                        bos.close();
                        is.close();
                        //System.out.println(bodyPart.getContentType());


                    }else {
                        //display body (message) of the attachment;
                        //System.out.println(bodyPart.getContent().toString());

                   }
           }
     }

上面的代码在 line 处引发 FileNotFoundException 异常,BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFile))并且对于文件名是非 ascii 字符的附件(类似于 ሰላም.pdf)会引发此异常。其他一切都很好。

4

2 回答 2

7

这个答案取自@semytech (OP) 的评论。在那里很难找到它,所以我将它添加为更多可见性的答案。它帮助我使用希伯来文文件名。

MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName(MimeUtility.encodeText(filename, "UTF-8", null));
于 2016-02-14T14:23:24.433 回答
0

您永远不需要自己进行编码或解码。

您可以设置两组属性来告诉 JavaMail 为您进行编码/解码:

  • mail.mime.encodefilename/mail.mime.decodefilename
  • mail.mime.encodeparameters/mail.mime.decodeparameters

有关详细信息,请参阅javax.mail.internet包的 javadocs。

第一组使用非标准编码技术,类似于您自己所做的。这适用于一些使用这种技术的旧邮件。

第二组使用 MIME 标准编码技术。此版本适用于大多数现代邮件。

这些都不能解释为什么你会得到 FileNotFoundException,但是你没有提供足够的细节来知道当你得到异常时你在做什么。

于 2013-03-09T18:48:29.057 回答