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

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

内容简介:代码日志版权声明:翻译自: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保存到特定文件夹》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Handbook of Data Structures and Applications

Handbook of Data Structures and Applications

Dinesh P. Mehta / Chapman and Hall/CRC / 2004-10-28 / USD 135.95

In the late sixties, Donald Knuth, winner of the 1974Turing Award, published his landmark book The Art of Computer Programming: Fundamental Algorithms. This book brought to- gether a body of kno......一起来看看 《Handbook of Data Structures and Applications》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具