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 {
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;
}
public static List uploadFiles(HttpServletRequest request,MultipartFile[] files,String fileBusiness,String userId)throws FileUploadException{
List fileEntitys= new ArrayList();
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{
private static final long serialVersionUID = 3835833847440273964L;
private String fileName;
private String suffix;
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;
}
}



