import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileUploadException; import org.springframework.web.multipart.MultipartFile; import com.yuanchuangyun.commons.entity.FileEntity; public class UploadFileUtils { /** * uploadFile:(上传单个文件调用方法) * TODO(这里描述这个方法适用条件 – 可选) * TODO(这里描述这个方法的执行流程 – 可选) * TODO(这里描述这个方法的使用方法 – 可选) * TODO(这里描述这个方法的注意事项 – 可选) * * @param @param request HttpServletRequest * @param @param file MultipartFile文件 * @param @param fileBusiness * @param @param userId * @param @return 设定文件 * @return FileEntity DOM对象 * @throws * @since CodingExample Ver 1.1 */ public static FileEntity uploadFile(HttpServletRequest request,MultipartFile file,String fileBusiness,String userId)throws FileUploadException{ FileEntity fileEntity = new FileEntity(); try { //取得当前上传文件的文件名称 String myFileName = file.getOriginalFilename(); //重命名上传后的文件名 String suffix = myFileName.substring(myFileName.lastIndexOf("."), myFileName.length()); String filePath = request.getSession().getServletContext().getRealPath("upload"); Date nowDate = new Date(); String businessPuth = File.separator+userId+File.separator+fileBusiness+File.separator; String newFileName =nowDate.getTime()+suffix; fileEntity.setFilePath(File.separator+"upload"+businessPuth+newFileName);//FileModelServiceImpl.java 同步修改 fileEntity.setFileName(myFileName); fileEntity.setSuffix(suffix); File newFile = new File(filePath+businessPuth); if(!newFile.exists()){ newFile.mkdirs(); } //定义上传路径 File localFile = new File(filePath+businessPuth+newFileName); file.transferTo(localFile); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new FileUploadException("IllegalStateException",e); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new FileUploadException("IOException",e); } return fileEntity; } /** * uploadFile:(上传多个文件调用方法) * TODO(这里描述这个方法适用条件 – 可选) * TODO(这里描述这个方法的执行流程 – 可选) * TODO(这里描述这个方法的使用方法 – 可选) * TODO(这里描述这个方法的注意事项 – 可选) * * @param @param request HttpServletRequest * @param @param file MultipartFile文件 * @param @param fileBusiness * @param @param userId * @param @return 设定文件 * @return FileEntity DOM对象 * @throws * @since CodingExample Ver 1.1 */ public static List<FileEntity> uploadFiles(HttpServletRequest request,MultipartFile[] files,String fileBusiness,String userId)throws FileUploadException{ List<FileEntity> fileEntitys= new ArrayList<FileEntity>(); try { for(MultipartFile file : files){ FileEntity fileEntity = new FileEntity(); //取得当前上传文件的文件名称 String myFileName = file.getOriginalFilename(); //重命名上传后的文件名 String suffix = myFileName.substring(myFileName.lastIndexOf("."), myFileName.length()); String filePath = request.getSession().getServletContext().getRealPath("upload"); Date nowDate = new Date(); String businessPuth = File.separator+userId+File.separator+fileBusiness+File.separator; String newFileName =nowDate.getTime()+suffix; fileEntity.setFilePath(File.separator+"upload"+businessPuth+newFileName); fileEntity.setFileName(myFileName); fileEntity.setSuffix(suffix); File newFile = new File(filePath+businessPuth); if(!newFile.exists()){ newFile.mkdirs(); } //定义上传路径 File localFile = new File(filePath+businessPuth+newFileName); file.transferTo(localFile); fileEntitys.add(fileEntity); } } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new FileUploadException("IllegalStateException",e); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new FileUploadException("IOException",e); } return fileEntitys; } /* * 获得上传文件的根目录 */ public static String getBaseUploadLocation(HttpServletRequest request){ return request.getSession().getServletContext().getRealPath(File.separator); } }
文件实体封装
public class FileEntity implements java.io.Serializable{ /** * * @since Ver 1.1 */ private static final long serialVersionUID = 3835833847440273964L; /** * 文件名称 */ private String fileName; /** * 后缀名 */ private String suffix; /** * 从 /upload/开始 的绝对路径文件名 */ private String filePath; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } }