springmvc如何实现文件上传与下载功能-创新互联
这篇文章给大家分享的是有关spring mvc如何实现文件上传与下载功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

文件上传
在pom.xml中引入spring mvc以及commons-fileupload的相关jar
org.springframework spring-webmvc 4.3.13.RELEASE commons-fileupload commons-fileupload 1.3.3
在springmvc.xml中加入文件上传的相关配置
10485760 UTF-8
在jsp文件中加入form表单
添加文件上传的方法
//上传文件会自动绑定到MultipartFile中
@RequestMapping(value="/upload",method=RequestMethod.POST)
public String upload(HttpServletRequest request,
@RequestParam("description") String description,
@RequestParam("file") MultipartFile file) throws Exception {
//如果文件不为空,写入上传路径
if(!file.isEmpty()) {
//上传文件路径
String path = request.getServletContext().getRealPath("/file/");
//上传文件名
String filename = file.getOriginalFilename();
File filepath = new File(path,filename);
//判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
//将上传文件保存到一个目标文件当中
file.transferTo(new File(path + File.separator + filename));
return "success";
} else {
return "error";
}
}感谢各位的阅读!关于“spring mvc如何实现文件上传与下载功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
文章标题:springmvc如何实现文件上传与下载功能-创新互联
网站网址:http://www.jxjierui.cn/article/ccsjsh.html


咨询
建站咨询
