commons-fileupload-1.3.1.jar
commons-io-2.4.jar
<form action="/fileUpload" method="post" enctype="multipart/form-data"> 名称:<input type="text" name="picname"/><br/> 图片:<input type="file" name="uploadFile"/><br/> <input type="submit" value="上传"/> </form>
@Controller("fileUploadController") public class FileUploadController { /** * 文件上传 */ @RequestMapping("/fileUpload") public String testResponseJson(String picname, MultipartFile uploadFile, HttpServletRequest request) throws Exception{ //定义文件名 String fileName = ""; //1.获取原始文件名 String uploadFileName = uploadFile.getOriginalFilename(); //2.截取文件扩展名 String extendName = uploadFileName.substring(uploadFileName.lastIndexOf(".")+1, uploadFileName.length()); //3.把文件加上随机数,防止文件重复 String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase(); //4.判断是否输入了文件名 if(!StringUtils.isEmpty(picname)) { fileName = uuid+"_"+picname+"."+extendName; }else { fileName = uuid+"_"+uploadFileName; } System.out.println(fileName); //2.获取文件路径 ServletContext context = request.getServletContext(); String basePath = context.getRealPath("/uploads"); //3.解决同一文件夹中文件过多问题 String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); //4.判断路径是否存在 File file = new File(basePath+"/"+datePath); if(!file.exists()) { file.mkdirs(); } //5.使用 MulitpartFile 接口中方法,把上传的文件写到指定位置 uploadFile.transferTo(new File(file,fileName)); return "success"; } }
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver" <!-- id 的值是固定的--> class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为 5MB -->
<property name="maxUploadSize">
<value>5242880</value> </property>
</bean>
SpringMVC传统方式上传文件 —— SpringMVC(九)
原文:https://www.cnblogs.com/guancangtingbai/p/12679259.html