<form id='myform' enctype="multipart/form-data" action="<%=webapp %>/UploadFileServlet" method="post" target="hidden_frame"> <input type="file" name="file" id="file" size="10" onchange="this.form.submit();" /> <iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe> </form>
webapp是项目的路径名
String webapp=request.getContextPath();
后台代码:
import java.io.File; 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<items.size();i++) { FileItem item = (FileItem) items.get(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("<script>alert('您上传的文件不是excel|txt|csv类型!');</script>"); 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("<script>parent.callBack("+ObjectToJSON.MapToJsonStr(rtnMap)+")</script>"); out.close(); } } } catch (Exception e) { e.printStackTrace(); } } }
EnvBean envBean =(EnvBean)SpringFacesUtil .getBean(this.getServletContext(), "EnvBean"); String uploadPath = envBean.getEnv().getProperty("upload_dest");//上传的路径
这句是调Spring找类,根据实际情况变化
UUIDGenerator generator=new UUIDGenerator(); String autoFileName = generator.generate().toString();
这句是给每个上传的文件加一个唯一标示,可用别的方法替代