Spring MVC将上传的MultipartFile保存到特定文件夹

栏目: Java · 发布时间: 5年前

内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/10847994/spring-mvc-save-uploaded-multipartfile-to-specific-folder

我想将上传的图像保存到部署在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));

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/10847994/spring-mvc-save-uploaded-multipartfile-to-specific-folder


以上所述就是小编给大家介绍的《Spring MVC将上传的MultipartFile保存到特定文件夹》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Elements of Statistical Learning

The Elements of Statistical Learning

Trevor Hastie、Robert Tibshirani、Jerome Friedman / Springer / 2009-10-1 / GBP 62.99

During the past decade there has been an explosion in computation and information technology. With it have come vast amounts of data in a variety of fields such as medicine, biology, finance, and mark......一起来看看 《The Elements of Statistical Learning》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换