Servlet上传文件

来源:互联网 发布:淘宝直通车助手软件 编辑:程序博客网 时间:2024/05/01 04:29
上传到E盘的uploadTemp文件夹里,以年月日时分秒毫秒命名。

 

package com.ray.upload;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Iterator;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;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.commons.io.FileExistsException;import com.ray.util.ImportExcel;public class UploadFile extends HttpServlet {/** * Constructor of the object. */public UploadFile() {super();}/** * Destruction of the servlet. <br> */public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. *  * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHssmmSSSS");DiskFileItemFactory factory = new DiskFileItemFactory();ServletFileUpload upload = new ServletFileUpload(factory);List<FileItem> fileList = null;if(ServletFileUpload.isMultipartContent(request)) {try {fileList = upload.parseRequest(request);Iterator<FileItem> fileIt = fileList.iterator();while (fileIt.hasNext()) {FileItem fileItem = fileIt.next();if(!fileItem.isFormField()) {File checkFileDir = new File("E:/uploadTemp");if(checkFileDir.exists()) {int index = 0;while(!checkFileDir.isDirectory()) {checkFileDir = new File("E:/uploadTemp"+(++index));if(!checkFileDir.exists()) {checkFileDir.mkdirs();break;}if(index>9999999) {break;}}} else {checkFileDir.mkdirs();}if(!checkFileDir.exists() || !checkFileDir.isDirectory()) {throw new IllegalArgumentException("未找到文件夹路径");}if(fileItem.getName()!=null && !fileItem.getName().equals("")) {System.out.println("文件大小:"+fileItem.getSize());System.out.println("文件类型:"+fileItem.getContentType());String tempName = fileItem.getName();tempName = tempName.indexOf(".")!=-1?tempName.substring(tempName.indexOf(".")):"";String fileName = df.format(new Date(System.currentTimeMillis()))+tempName;File file = new File(checkFileDir.getAbsolutePath()+"/"+fileName);if(file.exists()) {throw new FileExistsException(file);}fileItem.write(file);if(!ImportExcel.importPerson(file)) {response.getWriter().println("上传成功,但导入失败");} else {response.getWriter().println("上传成功");}response.getWriter().println("文件大小:"+fileItem.getSize()+"");response.getWriter().println("文件类型:"+fileItem.getContentType()+"");}}}} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();response.getWriter().println("上传失败:"+e.getMessage());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}} else {response.getWriter().println("上传失败");}}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. *  * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */public void init() throws ServletException {// Put your code here}}

0 0
原创粉丝点击