1 package com.zhanghaobo.fileupload; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import org.apache.commons.fileupload.FileItem; 13 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 14 import org.apache.commons.fileupload.servlet.ServletFileUpload; 15 16 public class UploadServlet extends HttpServlet { 17 @Override 18 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 19 throws ServletException, IOException { 20 DiskFileItemFactory factory=new DiskFileItemFactory(); 21 22 String realPath=req.getRealPath("/upload"); 23 24 //物理緩存存放地 25 factory.setRepository(new File(realPath)); 26 27 //設置內存存放大小 28 factory.setSizeThreshold(1024*1024); 29 30 ServletFileUpload upload=new ServletFileUpload(factory); 31 //FileItem 可以表示文件(file)也可以表示文本域(form item) 有一個isFormField方法判斷是否是表單 32 try{ 33 List<FileItem> list=(List<FileItem>)upload.parseRequest(req); 34 for(FileItem item:list){ 35 String name=item.getFieldName(); 36 if(item.isFormField()){ 37 String value=item.getString(); 38 req.setAttribute(name, value); 39 }else{ 40 String value=item.getName(); 41 int start=value.lastIndexOf("\\"); 42 String fileName=value.substring(start+1); 43 req.setAttribute(name, fileName); 44 //上傳文件存放地 45 item.write(new File(realPath,fileName)); 46 } 47 } 48 }catch(Exception e){ 49 e.printStackTrace(); 50 } 51 52 req.getRequestDispatcher("showResult.jsp").forward(req, resp); 53 } 54 }
1 public class UploadServlet extends HttpServlet { 2 @Override 3 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 4 throws ServletException, IOException { 5 DiskFileItemFactory factory=new DiskFileItemFactory(); 6 7 String realPath=req.getRealPath("/upload"); 8 9 //物理緩存存放地 10 factory.setRepository(new File(realPath)); 11 12 //設置內存存放大小 13 factory.setSizeThreshold(1024*1024); 14 15 ServletFileUpload upload=new ServletFileUpload(factory); 16 //FileItem 可以表示文件(file)也可以表示文本域(form item) 有一個isFormField方法判斷是否是表單 17 try{ 18 List<FileItem> list=(List<FileItem>)upload.parseRequest(req); 19 for(FileItem item:list){ 20 String name=item.getFieldName(); 21 if(item.isFormField()){ 22 String value=item.getString(); 23 req.setAttribute(name, value); 24 }else{ 25 String value=item.getName(); 26 int start=value.lastIndexOf("\\"); 27 String fileName=value.substring(start+1); 28 req.setAttribute(name, fileName); 29 30 //上傳文件存放地 31 // item.write(new File(realPath,fileName)); 32 33 InputStream is=item.getInputStream(); 34 35 long all=item.getSize(); 36 37 OutputStream os=new FileOutputStream(new File(realPath,fileName)); 38 39 byte[] buffer=new byte[400]; 40 41 int length=0; 42 int l=0; 43 DecimalFormat df=new DecimalFormat("#.00"); 44 while(-1!=(length=is.read(buffer))){ 45 os.write(buffer,0,length); 46 l+=length; 47 System.out.println("總大小"+all+""+"進度:"+(df.format((double)l/all*100))+"%"+"已經讀取的大小"+l); 48 } 49 50 is.close(); 51 os.close(); 52 53 } 54 } 55 }catch(Exception e){ 56 e.printStackTrace(); 57 } 58 59 req.getRequestDispatcher("showResult.jsp").forward(req, resp); 60 } 61 }