我想将上传的图像保存到部署在 Tomcat 上的 Spring 3 MVC 应用程序中的特定文件夹中
我的问题是我无法将上传的图像文件保存到运行应用程序的主机。
这是我尝试过的:
private void saveFile(MultipartFile multipartFile, int id) throws Exception {
String destination = "/images/" + id + "/" + multipartFile.getOriginalFilename();
File file = new File(destination);
multipartFile.transferTo(file);
}
结果:FileNotFoundException - 是的,我确实想创建这个文件!?!
我尝试使用 context.getRealPath
或 getResources("destination")
,但没有任何成功。
如何在我的应用程序的特定文件夹中使用我的多部分文件的内容创建一个新文件?
最佳答案
此代码一定会对您有所帮助。
String filePath = request.getServletContext().getRealPath("/");
multipartFile.transferTo(new File(filePath));
关于Spring MVC 将上传的 MultipartFile 保存到特定文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10847994/