JavaMail で非 ASCII ファイル名の添付ファイルを送信できますが、ダウンロードできません。ファイル名にASCII以外の文字が含まれている添付ファイルに対して、特にjava.io.FileNotFoundExceptionを取得しています。
参考:messageBodyPart.setFileName(MimeUtility.encodeText(filename[i]))
テキストをエンコードしMimeUtility.decodeText(bodyPart.getFileName())
、非ASCIIファイル名をデコードするようなものを使用しています
これに対する回避策はありますか?
EDIT @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());
}
}
}
上記のコードは、BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFile))
行で FileNotFoundException 例外を発生させます。これは、ファイル名が非 ASCII 文字 (ሰላም.pdf など) である添付ファイルに対して発生しています。他のすべては正常に動作します。