java 了解 Spring MVC 中的 MultipartFile transferTo() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28268499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding MultipartFile transferTo() method in Spring MVC
提问by Kyle Walker
I am using Spring Framework's MultipartFile to allow a user to upload a picture to a profile. I've configured DispatcherServlet in a servlet initializer class that extends AbstractAnnotationConfigDispatcherServletInitializer. In that class, I've overridden the customizeRegistration() method as follows:
我正在使用 Spring Framework 的 MultipartFile 来允许用户将图片上传到配置文件。我已经在扩展 AbstractAnnotationConfigDispatcherServletInitializer 的 servlet 初始值设定项类中配置了 DispatcherServlet。在那个类中,我重写了 customRegistration() 方法如下:
@Override
protected void customizeRegistration(Dynamic registration) {
registration.setMultipartConfig(new MultipartConfigElement("/tmp/practicewellness/uploads", 2097152, 4194304, 0));
}
The MultipartFile's transferTo() method calls for a file location in the filesystem where the uploaded file will be written temporarily. Can this location be anywhere? When I used the following location in the method:
MultipartFile 的 transferTo() 方法调用文件系统中的文件位置,上传的文件将临时写入该位置。这个位置可以在任何地方吗?当我在方法中使用以下位置时:
profilePicture.transferTo(new File("tmp/practicewellness/" + employee.getUsername() + ".jpg"));
... I get the following error:
...我收到以下错误:
Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [C:\Users\kyle\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\work\Catalina\localhost\practicewellness\tmp\practicewellness\uploads] is not valid
So I can see that it's looking for this file location deep inside one of my Eclipse plugins. I don't understand why it looks there. When I look there, the "tmp" directory is not there. But regardless, is it okay for me to go into that plugin and create the directory there? Or is there a better way to smooth this out?
所以我可以看到它正在我的一个 Eclipse 插件的深处寻找这个文件位置。我不明白为什么它看起来在那里。当我看那里时,“tmp”目录不存在。但无论如何,我可以进入该插件并在那里创建目录吗?或者有没有更好的方法来解决这个问题?
回答by The Coder
I've uploaded files using Spring mvc, but never used transferTo(), I just assume that your problem is due to "No existence of specified path" because there wont be a path ending with .jpg. Try it like this.
我已经使用 Spring mvc 上传文件,但从未使用过 transferTo(),我只是假设您的问题是由于“不存在指定路径”,因为不会有以 .jpg 结尾的路径。像这样试试。
String path = "/tmp/practicewellness/";
File dirPath = new File(path);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
And then execute the transferTo() code. Also do not set the path directly like you've done. Since you're doing it in spring, so I assume you want the folder to be in your Project path not the eclipse's metadata path. So change your path to this.
然后执行 transferTo() 代码。也不要像你所做的那样直接设置路径。由于您是在春季进行的,因此我假设您希望该文件夹位于您的项目路径中,而不是 eclipse 的元数据路径中。所以改变你的道路。
String path = request.getSession().getServletContext().getRealPath("/tmp/practicewellness/");
It will create a folder inside your Project's Webapp folder using mkdir. If you want to save differentiate the files for each user, you can create a folder for each user by using this below path.
它将使用 mkdir 在项目的 Webapp 文件夹中创建一个文件夹。如果要为每个用户保存区分文件,可以使用以下路径为每个用户创建一个文件夹。
String path = request.getSession().getServletContext().getRealPath("/tmp/practicewellness")+"/"+employee.getUsername()+"/";