/UploadFileServlet"
method="post" target="hidden_frame">
webapp是项目的路径名
String webapp=request.getContextPath();
后台代码:
import java.io.File; EnvBean envBean =(EnvBean)SpringFacesUtil 这句是调Spring找类,根据实际情况变化 UUIDGenerator generator=new UUIDGenerator(); 这句是给每个上传的文件加一个唯一标示,可用别的方法替代
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import com.siyue.ciap.beans.dic.EnvBean;
import com.siyue.ciap.dao.cms.UUIDGenerator;
import com.siyue.ciap.dao.phone.ReadFileUtil;
import com.siyue.ciap.dao.uitls.ObjectToJSON;
import com.siyue.ciap.service.uitl.SpringFacesUtil;
public class UploadFileServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
private static final Logger logger = Logger.getLogger(UploadFileServlet.class);
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("execute Upload File.................");
PrintWriter out = response.getWriter();
EnvBean envBean =(EnvBean)SpringFacesUtil
.getBean(this.getServletContext(), "EnvBean");
String uploadPath = envBean.getEnv().getProperty("upload_dest");//上传的路径
UUIDGenerator generator=new UUIDGenerator();
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);//设置缓冲
factory.setRepository(new File(uploadPath));//设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
upload.setSizeMax(1024000000);
List items = new ArrayList();
try {
items = upload.parseRequest(request);
Map rtnMap = new HashMap();
for(int i=0;i
String autoFileName = generator.generate().toString();
if("".equals(item.getName()))continue;
//获得文件名
String fileName = item.getName().substring(item.getName()
.lastIndexOf("\\")+1, item.getName().length());
//获得文件后缀名
String extName = fileName
.substring(fileName.lastIndexOf(".")+1, fileName.length());
//判断文件类型
if(!("xls".equalsIgnoreCase(extName) || "txt".equalsIgnoreCase(extName) ||
"csv".equalsIgnoreCase(extName))) {
out.print("");
out.close();
}else {
String fileOnlyName= uploadPath + autoFileName + "." + extName;
item.write(new File(fileOnlyName));
Map excelMap = null;
if("xls".equalsIgnoreCase(extName)) {
System.out.println("fileOnlyName------>"+fileOnlyName);
excelMap = ReadFileUtil.readSheetName(fileOnlyName);
}
rtnMap.put("file", fileOnlyName);
rtnMap.put("sheet", excelMap);
out.print("");
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java代码
EnvBean envBean =(EnvBean)SpringFacesUtil
.getBean(
this
.getServletContext(),
"EnvBean"
);
String uploadPath = envBean.getEnv().getProperty(
"upload_dest"
);
//上传的路径
.getBean(this.getServletContext(), "EnvBean");
String uploadPath = envBean.getEnv().getProperty("upload_dest");//上传的路径
Java代码
UUIDGenerator generator=
new
UUIDGenerator();
String autoFileName = generator.generate().toString();
String autoFileName = generator.generate().toString();
Java代码
ObjectToJSON这个类是自己写一个Object和Json互转的类
用FileUpload做得文件上传