• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringMVC 通过commons-fileupload实现文件上传功能

java 搞代码 4年前 (2022-01-05) 13次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了SpringMVC 通过commons-fileupload实现文件上传,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

配置

web.xml

   <!--注册DispatcherServlet--> springmvcorg.springframework.web.servlet.DispatcherServlet contextConfigLocationclasspath:applicationContext.xml1 springmvc/

SpringMVC配置文件 applicationContext.xml

上传文件的核心配置类:CommonsMultipartResolver,注意id="multipartResolver"不要写错

   <!--配置自动扫描controller包--><!--配置静态资源过滤--><!--配置注解驱动--><!--配置视图解析器--> <!--前缀--><!--后缀--><!--SpringMVC文件上传配置--> <!--设置请求的编码格式, 必须和pageEncoding的属性一致, 以便正确读取表单的值, 默认为ISO-8859-1--><!--上传文件的大小限制, 单位为字节 (10485760 = 10M)-->

文件上传 Controller

上传实现一

 package com.pro.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.commons.CommonsMultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; @RestController public class FileController { /* * 采用file.transferTo 来保存上传的文件 */ @RequestMapping("/upload2") public Map fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { //上传路径保存设置 String path = request.getServletContext().getRealPath("/upload"); File realPath = new File(path); if (!realPath.exists()){ realPath.mkdir(); } //上传文件地址 System.out.println("上传文件保存地址 --> "+realPath); //通过CommonsMultipartFile的方法直接写文件(注意这个时候) file.transferTo(new File(realPath +"/"+ file.getOriginalFilename())); Map hashMap = new HashMap(); hashMap.put("code", 0); hashMap.put("msg", "上传成功"); return hashMap; } }

上传实现二

这里的文件名称没有使用 UUID组合名称 为了方便测试

 package com.pro.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.commons.CommonsMultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; @RestController public class FileController { // @RequestParam("file") 将 name=file 控件得到的文件封装成 CommonsMultipartFile 对象 // 批量上传把 CommonsMultipartFile 改为数组即可 @RequestMapping("/upload") public Stri<span style="color:transparent">来源gaodai#ma#com搞*!代#%^码$网</span>ng upload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { // 获取文件名称 String uploadFileName = file.getOriginalFilename(); // 如果文件名为空, 直接返回首页 if ("".equals(uploadFileName)) { return "file upload error"; } System.out.println("上传文件名 --> " + uploadFileName); // 设置文件的保存位置 String path = request.getServletContext().getRealPath("/upload"); // 判断路径是否存在 File realPath = new File(path); if (!realPath.exists()) { // 如果不存在就创建 realPath.mkdir(); } System.out.println("文件保存路径 --> " + realPath); // 获取文件输入流 InputStream is = file.getInputStream(); // 获取文件输出流 FileOutputStream os = new FileOutputStream(new File(realPath, uploadFileName)); // 缓冲区读写文件 byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); os.flush(); } // 关闭流 os.close(); is.close(); return "file upload success"; } }

测试

   <title>$Title$</title>   

依赖

核心依赖就是 commons-fileupload

 <!--导入依赖--> <!--单元测试--> junitjunit4.13<!--spring--> org.springframeworkspring-webmvc5.2.0.RELEASE<!--文件上传--> commons-fileuploadcommons-fileupload1.3.3<!--servlet-api导入高版本的--> javax.servletjavax.servlet-api4.0.1<!--jsp--> javax.servlet.jspjsp-api2.2<!--jstl表达式--> javax.servletjstl1.2

以上就是SpringMVC 通过commons-fileupload实现文件上传功能的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringMVC 通过commons-fileupload实现文件上传功能
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址