package com.xyt.cyt.util; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; /** * 多文件上传 * @author liangrui * */ public class FileUpload { //文件信息 public static final String REALNAME = "realName"; //真实名字 public static final String STORENAME = "storeName"; //存储名字 public static final String SIZE = "size"; // private static final String SUFFIX = "suffix"; // private static final String CONTENTTYPE = "contentType"; public static final String CREATETIME = "createTime"; public static final String UPLOADDIR = "public/certificate/temp/"; //文件所在目录 uploadDir/ public static final String ABSOLUTEPATH="absolutePath";//保存文件 绝对路径 absolute public static final String RELATIVEPATH="relativePath"; //相对 路径 relative //多文件上传 public static List<Map<String, Object>> fileUpload(HttpServletRequest request) throws IllegalStateException, IOException{ //上传文件的解析器 CommonsMultipartResolver mutiparRe=new CommonsMultipartResolver(); if(mutiparRe.isMultipart(request)){//如果是文件类型的请求 //保存文件信息 List<Map<String, Object>> fileInfo = new ArrayList<Map<String, Object>>(); MultipartHttpServletRequest mhr=(MultipartHttpServletRequest) request; //创建子目录 取当前日期 DateFormat df= new SimpleDateFormat("yyyyMMdd"); String dirs=df.format(new Date()); //获取路径 String uploadDir = request.getSession().getServletContext() .getRealPath("/") + FileUpload.UPLOADDIR+dirs; //如果目录不存在,创建一个目录 if (!new File(uploadDir).exists()) { File dir= new File(uploadDir); dir.mkdirs(); } //跌带文件名称 Iterator<String> it= mhr.getFileNames(); while(it.hasNext()){ //获取一个文件 MultipartFile mf=mhr.getFile(it.next()); if(mf!=null){ //获取原文件名 String resFileName=mf.getOriginalFilename(); //保存的文件名 String fileName=rename(resFileName); //路径加文件名 File outFile=new File(uploadDir+"/"+fileName); //保存到 mf.transferTo(outFile); //返回文件信息 Map<String, Object> map = new HashMap<String, Object>(); map.put(FileUpload.REALNAME, resFileName); map.put(FileUpload.STORENAME, fileName); map.put(FileUpload.SIZE, mf.getSize()); //new File(fileName).length() map.put(FileUpload.CREATETIME, new Date()); //map.put(FileUpload.UPLOADDIR, uploadDir); map.put(FileUpload.ABSOLUTEPATH, uploadDir+fileName);//文件的绝对路径 map.put(FileUpload.RELATIVEPATH, "/"+FileUpload.UPLOADDIR+fileName);//文件的相对路径 fileInfo.add(map); } } return fileInfo; } return null; } //更改文件名称 public static String rename(String name) { //时分秒 /* Long now = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss") .format(new Date())); Long random = (long) (Math.random() * now); String fileName = now + "" + random; */ //uuid生成文件名 UUID uuid=UUID.randomUUID(); String uustr=uuid.toString(); if (name.indexOf(".") != -1) { uustr += name.substring(name.lastIndexOf(".")); } return uustr; } }
xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000000" /> <property name="defaultEncoding" value="UTF-8" /> </bean>
//文件上传==== 测试 @RequestMapping public String fileuploadTest(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("-----------statr"); List<Map<String, Object>> fileInfo=FileUpload.fileUpload(request); if(fileInfo!=null&&fileInfo.size()>0){ for(Map<String, Object> map:fileInfo){ System.out.println("filePath:"+map.get(FileUpload.RELATIVEPATH)); System.out.println("STORENAME:"+map.get(FileUpload.STORENAME)); System.out.println("ABSOLUTEPATH:"+map.get(FileUpload.ABSOLUTEPATH)); System.out.println("SIZE:"+map.get(FileUpload.SIZE)); } } request.setAttribute("fileInfo", fileInfo); return "/showimg"; }
因为在项目中用到,不能给出完整的代码,见凉,我相信有点基础的朋友都能看懂........