JavaMailに非ASCIIファイル名の添付ファイルを送信できますが、ダウンロードできません。ファイル名にASCII以外の文字が含まれている添付ファイルに対して、特にjava.io.FileNotFoundExceptionが発生しています。ファイル名にJavaMailと非ASCII文字がある
はFYI:私は非ASCIIファイル名に
を復号化するために、テキストとMimeUtility.decodeText(bodyPart.getFileName())
をエンコードするためにmessageBodyPart.setFileName(MimeUtility.encodeText(filename[i]))
のようなものを使用しています、このための回避策はありますか?
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ファイル)している添付ファイルの上げつつあります。他のすべてはうまく動作します。
私の問題を解決するためにまだ苦労しています。どんな更新? – semytech
この時点ではJavaMailの問題ではないので、私は多くの助けをすることはできません。おそらく、あなたのオペレーティングシステムがASCII以外の文字を使ったファイル名をどのように扱うかということになります。あなたは問題を再現するために自明なスタンドアロンのJavaプログラムを書くことができます。オペレーティングシステムがそのままファイルを処理しない場合は、ファイルを作成するときにファイル名をエンコードする必要があります。 –
ありがとうございます。私はそれのための簡単なJavaプログラムを作成して確認し、あなたに知らせます。 – semytech