package com.sys.tools;
//多文件上传的Java端的类
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.util.List;
import java.util.Iterator;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FileUploadAction extends HttpServlet {
// 限制文件的上传大小
private int maxPostSize = 100 * 1024 * 1024;
//文件上传地址
private String uploadPath;
public FileUploadAction() {
//从配置文件中取得文件上传地址
setUploadPath();
}
public String getUploadPath() {
return uploadPath;
}
public void setUploadPath() {
//从配置文件中取得文件上传地址
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// ServerAddress serverAddressBean=(ServerAddress)context.getBean("ServerAddressBean");
// this.uploadPath = serverAddressBean.getUploadPath().trim();
this.uploadPath = "c:/";
}
// 文件上传
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException
{
this.doUploadAdd(request, response);
}
private void logger(String info) {
System.out.println(info);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doUploadAdd(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doUploadAdd(request, response);
}
private void doUploadAdd(HttpServletRequest request,
HttpServletResponse response) throws ServletException {
logger("begin to upload");
try {
request.setCharacterEncoding("UTF-8");// 防止文件名称带有汉字后传到服务器乱码
//建立文件夹
this.makeDir(uploadPath);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
saveFiletoServer(request,response,uploadPath);
}
private void doUploadDelete(HttpServletRequest request,HttpServletResponse response) throws ServletException
{
String dirtyStr = "";//需要删除的文件名(单个文件)或文件夹名列表
try
{
if (request.getParameter("dirtyStr") != null) {
dirtyStr=URLDecoder.decode(request.getParameter("dirtyStr"),"utf-8");//前台ENCODE,后台DECODE
logger("删除的文件(夹)为:" + dirtyStr);
}
}
catch (Exception e) {
// TODO Auto-generated catch block
logger(e.getMessage());
}
try {
// new DeleteFiles().DeleteModifyFiles(dirtyStr, uploadPath);
} catch (Exception e) {
logger(e.getMessage());
}
}
//保存文件到服务器中
private void saveFiletoServer(HttpServletRequest request,HttpServletResponse response,String uploadPath)
{
// 操作文件
response.setContentType("text/html; charset=UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 4);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(maxPostSize);
logger("request========" + ObjectUtils.toString(request));
List fileItems = null;
try {
fileItems = upload.parseRequest(request);
logger("============" + ObjectUtils.toString(fileItems));
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
log(item.toString());
if (!item.isFormField()) {
String name = item.getName();
logger("上传的文件名 = " + name);
try {
item.write(new File(uploadPath + name));
} catch (Exception ex) {
logger(ex.getMessage());
}
}
}
} catch (FileUploadException ex1) {
logger("FileUploadException->" + ex1.getMessage());
}
}
// 建立文件夹路径
private boolean makeDir(String uploadPath) {
boolean isOK = false;
try {
File file = new File(uploadPath);
file.mkdirs();
isOK = true;
} catch (Exception e) {
isOK = false;
} finally {
return isOK;
}
}
// 建立文件夹路径
private boolean makeDirs(String uploadPath, String newDocStr) {
boolean isOK = false;
File file;
String[] temp;
try {
temp = newDocStr.split(",");
for (int i = 0; i < temp.length; i++) {
file = new File(uploadPath + temp[i] + "\\");
file.mkdirs();
}
isOK = true;
} catch (Exception e) {
isOK = false;
} finally {
return isOK;
}
}
}