1.导入Jar包
2.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>文件上传</title> </head> <body> <h2>单文件上传【不改名字】</h2> <form action="/upload/upload01.action" enctype="multipart/form-data" method="post"> 选择文件:<input type="file" name="mf"/> <br> 提交文件:<input type="submit" value="提交"> </form> <br> <h2>多文件上传【不改名字】</h2> <form action="/upload/upload02.action" enctype="multipart/form-data" method="post"> 选择文件:<input type="file" name="mf"/> <br> 选择文件:<input type="file" name="mf"/> <br> 选择文件:<input type="file" name="mf"/> <br> 提交文件:<input type="submit" value="提交"> </form> <br> <h2>单文件上传【改名字】</h2> <form action="/upload/upload03.action" enctype="multipart/form-data" method="post"> 选择文件:<input type="file" name="mf"/> <br> 提交文件:<input type="submit" value="提交"> </form> <br> <h2>单文件上传【改名字并分文件夹管理】</h2> <form action="/upload/upload04.action" enctype="multipart/form-data" method="post"> 选择文件:<input type="file" name="mf"/> <br> 提交文件:<input type="submit" value="提交"> </form> <br> <h2>单文件上传【改名字并分文件夹管理保存到数据库】</h2> <form action="/upload/upload05.action" enctype="multipart/form-data" method="post"> 选择文件:<input type="file" name="mf"/> <br> 提交文件:<input type="submit" value="提交"> </form> </body> </html>
3.配置文件添加
<!-- 配置springmvc对文件上传的支持 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置文件名的编码 --> <property name="defaultEncoding" value="utf-8"></property> <!-- 配置最上传文件的支持 20M --> <property name="maxUploadSize" value="20971520"></property> <!-- 设置文件上传的临时目录 --> <property name="uploadTempDir" value="/upload/temp" /> </bean>
4.controller
package com.zhs.sys.controller; import com.zhs.sys.service.AttachmentService; import com.zhs.sys.utils.RandomUtils; import com.zhs.sys.vo.AttachmentVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.util.Date; @Controller @RequestMapping("upload") public class FileUploadController { /** * 上传一个文件不改名字 */ @RequestMapping("upload01") public String upload01(MultipartFile mf, HttpServletRequest request){ //1.得到文件的相关信息 String contentType = mf.getContentType(); String name = mf.getName(); String oldname = mf.getOriginalFilename(); long size = mf.getSize(); System.out.println(contentType); System.out.println(name); System.out.println(oldname); System.out.println(size); //2.得到要上传的路径 String realPath=request.getServletContext().getRealPath("/upload"); //3.组装文件对象 File file = new File(realPath,oldname); //4.把文件流写到file try { mf.transferTo(file); } catch (Exception e) { e.printStackTrace(); } return "success"; } /** * 上传多个文件不改名字 */ @RequestMapping("upload02") public String upload02(MultipartFile[] mf, HttpServletRequest request){ if(null!=mf&&mf.length>0){ for(int i=0;i<mf.length;i++){ // 得到文件的相关信息 String contentType = mf[i].getContentType(); String name = mf[i].getName(); String oldname = mf[i].getOriginalFilename(); long size = mf[i].getSize(); System.out.println(contentType); System.out.println(name); System.out.println(oldname); System.out.println(size); //得到要上传的路径 String realPath=request.getServletContext().getRealPath("/upload"); //组装文件对象 File file = new File(realPath,oldname); //把文件流写到file try { mf[i].transferTo(file); } catch (Exception e) { e.printStackTrace(); } } } return "success"; } /** * 上传一个文件【改名字】 */ @RequestMapping("upload03") public String upload03(MultipartFile mf, HttpServletRequest request){ //1.得到文件的相关信息 String oldname = mf.getOriginalFilename(); //2.得到要上传的路径 String realPath=request.getServletContext().getRealPath("/upload"); //3.得到新名称 String newName = RandomUtils.createRandomFileNameUseTime(oldname); //4.组装文件对象 File file = new File(realPath,oldname); //5.把文件流写到file try { mf.transferTo(file); } catch (Exception e) { e.printStackTrace(); } return "success"; } /** * 上传一个文件【改名字并分文件夹管理】 */ @RequestMapping("upload04") public String upload04(MultipartFile mf, HttpServletRequest request){ //1.得到文件的相关信息 String oldname = mf.getOriginalFilename(); //2.得到要上传的路径 String realPath=request.getServletContext().getRealPath("/upload"); //3.根据当前日期得到文件夹名称 String dirName = RandomUtils.createDirUseDate(); //根据文件夹 File dir = new File(realPath, dirName); //判断文件夹是否存在 if (!dir.exists()) { dir.mkdirs();//创建文件夹 } //6.得到新名称 String newName = RandomUtils.createRandomFileNameUseTime(oldname); //7.组装文件对象 File file = new File(dir,newName); //8.把文件流写到file try { mf.transferTo(file); } catch (Exception e) { e.printStackTrace(); } return "success"; } /** * 上传一个文件【改名字并分文件夹管理保存到数据库】 */ @Autowired private AttachmentService attachmentService; @RequestMapping("upload05") public String upload05(MultipartFile mf, HttpServletRequest request){ String contentType = mf.getContentType(); long size = mf.getSize(); //1.得到文件的相关信息 String oldname = mf.getOriginalFilename(); //2.得到要上传的路径 String realPath=request.getServletContext().getRealPath("/upload"); //3.根据当前日期得到文件夹名称 String dirName = RandomUtils.createDirUseDate(); //根据文件夹 File dir = new File(realPath, dirName); //判断文件夹是否存在 if (!dir.exists()) { dir.mkdirs();//创建文件夹 } //6.得到新名称 String newName = RandomUtils.createRandomFileNameUseTime(oldname); //7.组装文件对象 File file = new File(dir,newName); //8.把文件流写到file try { mf.transferTo(file); } catch (Exception e) { e.printStackTrace(); } //9.保存信息到数据库 AttachmentVo vo = new AttachmentVo(); vo.setContenttype(contentType); vo.setSize(Double.valueOf(size)); vo.setOldname(oldname); vo.setUploadtime(new Date()); vo.setPath("upload/"+dirName+"/"+newName);//存相对路径 attachmentService.addAttachment(vo); return "success"; } }
5.工具类
package com.zhs.sys.utils; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import java.util.UUID; /** * 生成随机字符串 */ public class RandomUtils { private static SimpleDateFormat format1=new SimpleDateFormat("yyyyMMddHHmmssSSS"); private static Random random =new Random(); /** * 使用时间+4位随机数 */ public static String createRandomFileNameUseTime(String oldName){ //得到文件名的最后一个点的下表 int lastDotIndex = oldName.lastIndexOf("."); //从lastDotIndex截取到oldName.length() String suffix = oldName.substring(lastDotIndex, oldName.length()); //生成时间字符串 String timeStr=format1.format(new Date()); //生成四位随机数 int random4Num=random.nextInt(9000)+1000; return timeStr+random4Num+suffix; } public static void main(String[] args) { System.out.println(createRandomFileNameUseGNTime("a.b.daasf.sfa.feg.jpg")); } /** * 根据日期生成文件夹名称 */ private static SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd"); public static String createDirUseDate(){ return format2.format(new Date()); } /** * 使用uuid */ public static String createRandomFileNameUseUUID(String oldName){ //得到文件名的最后一个点的下表 int lastDotIndex = oldName.lastIndexOf("."); //从lastDotIndex截取到oldName.length() String suffix = oldName.substring(lastDotIndex, oldName.length()); //生成uuid字符串 String uuid = UUID.randomUUID().toString().replace("-",""); return uuid+suffix; } /** * 使用格林毫秒+4位承随机数 */ public static String createRandomFileNameUseGNTime(String oldName){ //得到文件名的最后一个点的下表 int lastDotIndex = oldName.lastIndexOf("."); //从lastDotIndex截取到oldName.length() String suffix = oldName.substring(lastDotIndex, oldName.length()); //生成时间字符串 long time = new Date().getTime(); //生成四位随机数 int random4Num=random.nextInt(9000)+1000; return time+random4Num+suffix; } }
原文地址:https://www.cnblogs.com/zhsv/p/12638150.html
时间: 2024-11-09 01:05:54