0

在我下面的代码中,我想从电子邮件中下载附件,然后想存储在 C 驱动器中名为 Attachment 的特定文件夹中,而不是存储在下载中,我的代码如下

String attachFiles = "";
String saveDirectory = "C://Attachment//"; // Here I want to store my attachments
if (contentType.contains("multipart")) {
    Multipart multiPart = (Multipart) message.getContent();
    int numberOfParts = multiPart.getCount();
    for (int partCount = 0; partCount < numberOfParts; partCount++) {
        MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
        if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
            String fileName = part.getFileName();
            attachFiles += fileName + ", ";
            part.saveFile(saveDirectory + File.separator + fileName);
        } else {
            messageContent = part.getContent().toString();
        }
    }
}

任何帮助将不胜感激

4

1 回答 1

0

任何一个

String saveDirectory = "C:\\Attachment";
String saveDirectory = "C:/Attachment";

并且绝对过滤文件名

fileName = fileName.replaceAll("[^\\w.-]", ""); // Or so, period still dubious.
if (fileName.isEmpty()) fileName = "x";
于 2013-05-17T10:36:35.863 回答