JavaMail and non-ASCII character in file names

I can send attachments that have filenames other than ascii to JavaMail, but I cannot upload them. I get java.io.FileNotFoundException specifically for those attachments whose file names contain characters without ascii.

FYI: I use something like messageBodyPart.setFileName(MimeUtility.encodeText(filename[i]))for encoding text and MimeUtility.decodeText(bodyPart.getFileName())for decoding file names without ascii

Is there a workaround for this?

CHANGE @Bill, that's part of my code that reads investments. I also added properties properties.setProperty ("mail.mime.decodeparameters", "true") and properties.setProperty ("mail.mime.decodefilename", "true") in my code.

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());

                   }
           }
     }

FileNotFoundException BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFile)), , ascii (- ሰላም. pdf). .

+5
2

@semytech (OP). , . .

MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName(MimeUtility.encodeText(filename, "UTF-8", null));
+3

.

, , JavaMail /:

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

. javadocs javax.mail.internet.

, , . , .

MIME. .

, FileNotFoundException, , , , .

0

All Articles