Spring MVC - MultipartFile实现文件上传(单文件与多文件上传)

准备工作: 需要先搭建一个spirngmvc的maven项目

  • 1、加入jar包
<dependency>
      <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3</version> </dependency>
  • 2、在springmvc的配置文件中,加入如下配置:
  <!--SpringMVC上传文件时,需要配置MultipartResolver处理器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8" /> <!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --> <property name="maxUploadSize" value="200000"/> <!-- 指定上传文件的临时路径 --> <!-- <property name="uploadTempDir" value="uploadTempDirectory" /> --> </bean>
  • 3、创建Controller
package cn.van.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; /** * Created by van on 2017-07-18. */ @Controller @RequestMapping("/upload") public class MultipartFileController { //单文件上传 @RequestMapping("/toFileUpload") public String toUpload(){ return "fileUpload/fileUpload"; } @RequestMapping("fileUpload") @ResponseBody public String upload(MultipartFile multipartFile){ if(!multipartFile.isEmpty()){ //设置文件的保存路径 String filePath = "D:\\MultipartFile\\" + multipartFile.getOriginalFilename(); //转存文件 try { multipartFile.transferTo(new File(filePath)); } catch (IOException e) { e.printStackTrace(); } } return "success"; } //多文件上传 @RequestMapping("/toFileUploadFiles") public String toUploadFiles(){ return "fileUpload/fileUploadFiles"; } @RequestMapping("fileUploadFiles") @ResponseBody //此处用@RequestParam("xx")来指定参数名,不加会报错 public String uploadFiles(@RequestParam("multipartFile") MultipartFile[] multipartfiles) throws IOException { String savePath = "D:\\MultipartFile\\"; if(multipartfiles != null && multipartfiles.length != 0){ if(null != multipartfiles && multipartfiles.length > 0){ //遍历并保存文件 for(MultipartFile file : multipartfiles){ file.transferTo(new File(savePath + file.getOriginalFilename())); } } } return "success"; } }
  • 4、写两个简单的上传页面(单文件和多文件)

单文件:

<%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@include file="/WEB-INF/jsp/common/common.jsp"%> <html> <title>test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <head> <style type="text/css"> .upload { margin-top: 100px; margin-left: 100px; text-align: center; } </style> </head> <body> <h1 style="text-align: center;margin-top: 20px">test</h1> <div> <form class="upload" action="${path}/upload/fileUpload" method="post" enctype="multipart/form-data"> <p> 选择文件:<input type="file" name="multipartFile"/> </p> <p></p> <p style="margin-top: 20px;"> <input style="" type="submit" value="上传并检测"/> </p> </form> </div> </body> </html>

多文件:

<%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@include file="/WEB-INF/jsp/common/common.jsp"%> <html> <title>test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <head> <style type="text/css"> .upload { margin-top: 100px; margin-left: 100px; text-align: center; } </style> </head> <body> <h1 style="text-align: center;margin-top: 20px">test</h1> <div> <form class="upload" action="${path}/upload/fileUploadFiles" method="post" enctype="multipart/form-data"> <p> 选择文件:<input type="file" name="multipartFile"/> <input type="file" name="multipartFile"/> <input type="file" name="multipartFile"/> </p> <p></p> <p style="margin-top: 20px;"> <input style="" type="submit" value="上传并检测"/> </p> </form> </div> </body> </html>

5、访问页面,选择本地文件,上传成功。

转载于:https://www.cnblogs.com/dengyungao/p/7716839.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值