使用org.apache.commons.fileupload.servlet.ServletFileUpload上传文件:
* 创建FileUploadUtil工具类
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadUtil{
private ServletFileUpload servletFileUpload;
private String filePath;//文件保存路径
private int fileIndex=0;
private long singleSizeMax = 2*1024*1024;//上传单个文件大小上额2M
private long sizeMax = 20*1024*1024;//总上传大小上额 20M
private List<FileBean> fileList;//解析后的需要保存的文件集合
private List upFileList;//上传的文件集合
private String fileNameStart = "";
private Map<String,Object> paramMap = new HashMap<String,Object>();
private HttpServletRequest request;
/**
* 重置相关信息
*/
public void refresh() throws Exception{
this.fileList = new ArrayList();
this.paramMap.clear();
this.fileIndex = 0;
this.fileNameStart = "";
}
/**
* 初始化
*/
public void init(HttpServletRequest request) throws Exception{
this.refresh();
this.servletFileUpload.setSizeMax(this.sizeMax);
fileList = new ArrayList<FileBean>();
this.upFileList = new ArrayList();
//解析request中相关信息
List<FileItem> fileItemList = this.servletFileUpload.parseRequest(this.request);
Iterator iterator = fileItemList.iterator();
while(iterator.hasNext()){
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
//是字段
this.processFormField(item);
}else{
//是file
this.upFileList.add(item);
}
}
}
/**
* 解析普通表单字段
*/
public void processFormField(FileItem item){
try{
String filedName=item.getFieldName().trim().toUpperCase();
String filedValue=item.getString(this.getServletFileUpload().getHeaderEncoding());
if(paramMap.get(filedName)!=null){
paramMap.put(filedName,paramMap.get(filedName)+","+filedValue);
}else{
paramMap.put(filedName,filedValue);
}
}catch(Exception ex){
//解析普通表单字段出错
}
}
/**
* 取上傳文件的原始文件名稱(含後綴)
* @param pathName
* @return
*/
public String getUploadFileName(String pathName){
int startIndex = pathName.lastIndexOf("\\");
if(startIndex<0) startIndex=0;
int endIndex = pathName.length();
return pathName.substring(startIndex+1,endIndex);
}
/**
* 生成不重複的保存文件名
* @param pathName
* @return
*/
public String getSaveFileName(String pathName){
fileIndex++;
java.util.Date date=new java.util.Date();
java.text.DateFormat ft = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS");
String dateStr = ft.format(date);
return this.getFileNameStart()+dateStr+"_"+String.valueOf(fileIndex)+"."+IoUtils.getFileType(pathName);
}
/**
* 解析上传的文件
* @param pathName
* @return
*/
public FileBean processUploadedFile(FileItem item) throws Exception{
String fileName = getUploadFileName(item.getName());
String newName = getSaveFileName(item.getName());
String fileType = IoUtils.getFileType(item.getName());
File newFile = new File(this.filePath + newName);
item.write(newFile);
FileBean fileBean = new FileBean();
fileBean.setFileName(fileName);
fileBean.setFileSize(item.getSize());
fileBean.setFileType(fileType);
fileBean.setContentType(item.getContentType());
fileBean.setPathName(newName);
fileBean.setFieldName(item.getFieldName());
return fileBean;
}
/**
* 數據回滾
* @throws java.lang.Exception
*/
public void rollBack()throws Exception{
this.delete(fileList);
this.fileList = null;
this.upFileList=null;
}
/**
* 刪除文件
* @param filePath
* @throws java.lang.Exception
*/
public void delete(String filePath) throws Exception{
IoUtils.deleteFile(this.filePath+filePath);
}
/**
* 刪除文件
* @param fileList
* @throws java.lang.Exception
*/
public void delete(List fileList) throws Exception{
String fileNames="";
if(fileList!=null && fileList.size()>0){
for(int i=0;i<fileList.size();i++){
FileBean file = (FileBean)fileList.get(i);
fileNames += filePath+file.getPathName()+",";
}
IoUtils.deleteFile(fileNames);
}
}
//取值的一些方法
/**
* 取字段值
* @param name
* @return
*/
public String getParameter(String name){
if(paramMap.get(name.toUpperCase())!=null){
return (String) paramMap.get(name.toUpperCase());
}else{
return null;
}
}
/**
* 提取所有上传的文件
* @author : qinzhanxing
* @date : Nov 13, 2009 3:27:07 PM
* @return
*/
public List<FileBean> getFiles(){
return fileList;
}
/**
* 批量文件保存(按字段名分組)
* @param nameStartsWith
* @param rollBack 當某個文件出錯是否回滾(刪除所有已經上傳的文件,竎throws Exception)
* @return list+FileBean
* @throws java.lang.Exception
*/
public List<FileBean> getFilesByName(String nameStartsWith,boolean rollBack)throws Exception{
//filePath = request.getContextPath()+filePath;
this.save(rollBack);
nameStartsWith = nameStartsWith.trim();
List namefileList = new ArrayList();
for(FileBean file:fileList){
String fieldName = file.getFieldName().trim();
log.debug("fieldName="+fieldName);
if(fieldName.startsWith(nameStartsWith)){
namefileList.add(file);
}
}
return namefileList;
}
}
* 使用实例:
FileUploadUtil fuTool = new FileUploadUtil();
fuTool.setServletFileUpload(this.getServletFileUpload());
fuTool.setSizeMax(this.getFileMaxSize());
fuTool.setFilePath(this.getFilePath());
fuTool.init(request);
fuTool.save(true);
String parameterValue = fuTool.getParameter("parameterName");
for(FileBean file : fuTool.getFiles()){
}
* 创建FileUploadUtil工具类
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadUtil{
private ServletFileUpload servletFileUpload;
private String filePath;//文件保存路径
private int fileIndex=0;
private long singleSizeMax = 2*1024*1024;//上传单个文件大小上额2M
private long sizeMax = 20*1024*1024;//总上传大小上额 20M
private List<FileBean> fileList;//解析后的需要保存的文件集合
private List upFileList;//上传的文件集合
private String fileNameStart = "";
private Map<String,Object> paramMap = new HashMap<String,Object>();
private HttpServletRequest request;
/**
* 重置相关信息
*/
public void refresh() throws Exception{
this.fileList = new ArrayList();
this.paramMap.clear();
this.fileIndex = 0;
this.fileNameStart = "";
}
/**
* 初始化
*/
public void init(HttpServletRequest request) throws Exception{
this.refresh();
this.servletFileUpload.setSizeMax(this.sizeMax);
fileList = new ArrayList<FileBean>();
this.upFileList = new ArrayList();
//解析request中相关信息
List<FileItem> fileItemList = this.servletFileUpload.parseRequest(this.request);
Iterator iterator = fileItemList.iterator();
while(iterator.hasNext()){
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
//是字段
this.processFormField(item);
}else{
//是file
this.upFileList.add(item);
}
}
}
/**
* 解析普通表单字段
*/
public void processFormField(FileItem item){
try{
String filedName=item.getFieldName().trim().toUpperCase();
String filedValue=item.getString(this.getServletFileUpload().getHeaderEncoding());
if(paramMap.get(filedName)!=null){
paramMap.put(filedName,paramMap.get(filedName)+","+filedValue);
}else{
paramMap.put(filedName,filedValue);
}
}catch(Exception ex){
//解析普通表单字段出错
}
}
/**
* 取上傳文件的原始文件名稱(含後綴)
* @param pathName
* @return
*/
public String getUploadFileName(String pathName){
int startIndex = pathName.lastIndexOf("\\");
if(startIndex<0) startIndex=0;
int endIndex = pathName.length();
return pathName.substring(startIndex+1,endIndex);
}
/**
* 生成不重複的保存文件名
* @param pathName
* @return
*/
public String getSaveFileName(String pathName){
fileIndex++;
java.util.Date date=new java.util.Date();
java.text.DateFormat ft = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSS");
String dateStr = ft.format(date);
return this.getFileNameStart()+dateStr+"_"+String.valueOf(fileIndex)+"."+IoUtils.getFileType(pathName);
}
/**
* 解析上传的文件
* @param pathName
* @return
*/
public FileBean processUploadedFile(FileItem item) throws Exception{
String fileName = getUploadFileName(item.getName());
String newName = getSaveFileName(item.getName());
String fileType = IoUtils.getFileType(item.getName());
File newFile = new File(this.filePath + newName);
item.write(newFile);
FileBean fileBean = new FileBean();
fileBean.setFileName(fileName);
fileBean.setFileSize(item.getSize());
fileBean.setFileType(fileType);
fileBean.setContentType(item.getContentType());
fileBean.setPathName(newName);
fileBean.setFieldName(item.getFieldName());
return fileBean;
}
/**
* 數據回滾
* @throws java.lang.Exception
*/
public void rollBack()throws Exception{
this.delete(fileList);
this.fileList = null;
this.upFileList=null;
}
/**
* 刪除文件
* @param filePath
* @throws java.lang.Exception
*/
public void delete(String filePath) throws Exception{
IoUtils.deleteFile(this.filePath+filePath);
}
/**
* 刪除文件
* @param fileList
* @throws java.lang.Exception
*/
public void delete(List fileList) throws Exception{
String fileNames="";
if(fileList!=null && fileList.size()>0){
for(int i=0;i<fileList.size();i++){
FileBean file = (FileBean)fileList.get(i);
fileNames += filePath+file.getPathName()+",";
}
IoUtils.deleteFile(fileNames);
}
}
//取值的一些方法
/**
* 取字段值
* @param name
* @return
*/
public String getParameter(String name){
if(paramMap.get(name.toUpperCase())!=null){
return (String) paramMap.get(name.toUpperCase());
}else{
return null;
}
}
/**
* 提取所有上传的文件
* @author : qinzhanxing
* @date : Nov 13, 2009 3:27:07 PM
* @return
*/
public List<FileBean> getFiles(){
return fileList;
}
/**
* 批量文件保存(按字段名分組)
* @param nameStartsWith
* @param rollBack 當某個文件出錯是否回滾(刪除所有已經上傳的文件,竎throws Exception)
* @return list+FileBean
* @throws java.lang.Exception
*/
public List<FileBean> getFilesByName(String nameStartsWith,boolean rollBack)throws Exception{
//filePath = request.getContextPath()+filePath;
this.save(rollBack);
nameStartsWith = nameStartsWith.trim();
List namefileList = new ArrayList();
for(FileBean file:fileList){
String fieldName = file.getFieldName().trim();
log.debug("fieldName="+fieldName);
if(fieldName.startsWith(nameStartsWith)){
namefileList.add(file);
}
}
return namefileList;
}
}
* 使用实例:
FileUploadUtil fuTool = new FileUploadUtil();
fuTool.setServletFileUpload(this.getServletFileUpload());
fuTool.setSizeMax(this.getFileMaxSize());
fuTool.setFilePath(this.getFilePath());
fuTool.init(request);
fuTool.save(true);
String parameterValue = fuTool.getParameter("parameterName");
for(FileBean file : fuTool.getFiles()){
}