简体   繁体   English

将零件保存到目录中

[英]Saving a Part into a directory

I've googled around enough but I can't find the right solution to my issue. 我已经在Google上搜寻了足够多的内容,但是找不到适合我问题的正确解决方案。

I'm using javamail to download all the attachments of an email and now I do have the attachments but I would like to store them all in a directory. 我正在使用javamail下载电子邮件的所有附件,现在我确实有附件,但是我想将它们全部存储在目录中。 So my code does create a directory (using file.mkdir()) but I haven't been able to move those attachments into my folder (Attachments). 所以我的代码确实创建了一个目录(使用file.mkdir()),但是我无法将这些附件移到我的文件夹(附件)中。 This is what I am using so far: 到目前为止,这是我正在使用的:

MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);
    if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
        part.saveFile("/Attachments/" + part.getFileName());
    }

Is there a way to save the part into a folder? 有没有办法将零件保存到文件夹中?

打开FileOutputStream并写出字节。

You don't say exactly what happens when you try to save the attachment. 您没有确切说明尝试保存附件时发生的情况。 Do you get an exception? 你有例外吗? Is the attachment stored in the wrong file? 附件是否存储在错误的文件中? I'm sure you understand that what you wrote tries to save it in the /Attachments directory in the root of your filesystem. 我确定您了解您编写的内容试图将其保存在文件系统根目录的/ Attachments目录中。

Note also that the filename that comes with a part can be complete garbage. 还要注意,部分附带的文件名可以是完整的垃圾。 Consider what should happen if the filename in the part is "../etc/passwd". 考虑如果该部分中的文件名为“ ../etc/passwd”,该怎么办。

我得到了要保存的文件位置的绝对路径,然后从apache使用FileUtils将文件移动到目录,然后将目录移动到另一个目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM