檔案上傳需要注意的問題

never123450發表於2014-05-09
package cn.itcast.web.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

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.FileUploadBase;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet3 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List types = Arrays.asList("jpg","gif","avi","txt");
		
		try{
			DiskFileItemFactory factory = new DiskFileItemFactory();  //10k
			factory.setSizeThreshold(1024*1024);
			factory.setRepository(new File(this.getServletContext().getRealPath("/temp")));
			
			ServletFileUpload upload = new ServletFileUpload(factory);
			upload.setProgressListener(new ProgressListener(){
				public void update(long pBytesRead, long pContentLength, int pItems) {
					System.out.println("當前已解析:" + pBytesRead);
				}
			});
			
			
			upload.setFileSizeMax(1024*1024*5);
			if(!upload.isMultipartContent(request)){
				//按照傳統方式獲取表單資料
				request.getParameter("username");
				return;
			}
			upload.setHeaderEncoding("UTF-8");
			List<FileItem> list = upload.parseRequest(request);
			
			for(FileItem item : list){
				if(item.isFormField()){
					//為普通輸入項
					String inputName = item.getFieldName();
					String inputValue = item.getString("UTF-8");
					//inputValue = new String(inputValue.getBytes("iso8859-1"),"UTF-8");
					System.out.println(inputName + "="  + inputValue);
				}else{
					String filename = item.getName().substring(item.getName().lastIndexOf("\\")+1);  //""
					if(filename==null || filename.trim().equals("")){
						continue;
					}
					
					/*String ext = filename.substring(filename.lastIndexOf(".")+1);
					if(!types.contains(ext)){
						request.setAttribute("message", "本系統不支援" + ext + "這種型別");
						request.getRequestDispatcher("/message.jsp").forward(request, response);
						return;
					}*/
					InputStream in = item.getInputStream();
					int len = 0;
					byte buffer[] = new byte[1024];
					String saveFileName = generateFileName(filename);
					String savepath = generateSavePath(this.getServletContext().getRealPath("/WEB-INF/upload"),saveFileName);
					FileOutputStream out = new FileOutputStream(savepath + File.separator + saveFileName);
					while((len=in.read(buffer))>0){
						out.write(buffer, 0, len);
					}
					in.close();
					out.close();
					item.delete();  //刪除臨時檔案
				}
			}
		}catch (FileUploadBase.FileSizeLimitExceededException e) {
			request.setAttribute("message", "檔案大小不能超過5m");
			request.getRequestDispatcher("/message.jsp").forward(request, response);
			return;
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
		request.setAttribute("message", "上傳成功!!");
		request.getRequestDispatcher("/message.jsp").forward(request, response);
	}
	
	//
	public String generateSavePath(String path,String filename){
		int hashcode = filename.hashCode();  //121221
		int dir1 = hashcode&15;
		int dir2 = (hashcode>>4)&0xf;
		
		String savepath = path + File.separator + dir1 + File.separator + dir2;
		File file = new File(savepath);
		if(!file.exists()){
			file.mkdirs();
		}
		return savepath;
	}
	
	public String generateFileName(String filename){
		//83434-83u483-934934
		return UUID.randomUUID().toString() + "_" + filename;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}


1.上傳檔案的中文亂碼
 1.1 解決檔案的亂碼
  ServletFileUpload.setHeaderEncoding("UTF-8")
 1.2 解決普通輸入項的亂碼(注意,表單型別為multipart/form-data的時候,設定request的編碼是無效的)
  FileItem.setString("UTF-8");  //解決亂碼

2.在處理表單之前,要記得呼叫:
 ServletFileUpload.isMultipartContent方法判斷提交表單的型別,如果該方法返回true,則按上傳方式處理,否則按照傳統方式處理表單即可。


3.設定解析器緩衝區的大小,以及臨時檔案的刪除
 設定解析器緩衝區的大小  DiskFileItemFactory.setSizeThreshold(1024*1024);
 臨時檔案的刪除:在程式中處理完上傳檔案後,一定要記得呼叫item.delete()方法,以刪除臨時檔案

4.在做上傳系統時,千萬要注意上傳檔案的儲存目錄,這個上傳檔案的儲存目錄絕對不能讓外界直接訪問到。

5.限制上傳檔案的型別
 在處理上傳檔案時,判斷上傳檔案的字尾名是不是允許的

6.限制上傳檔案的大小
 呼叫解析器的ServletFileUpload.setFileSizeMax(1024*1024*5);就可以限制上傳檔案的大小,如果上傳檔案超出限制,則解析器會拋FileUploadBase.FileSizeLimitExceededException異常,程式設計師通過是否抓到這個異常,進而就可以給使用者友好提示。


7.如何判斷空的上傳輸入項
 String filename = item.getName().substring(item.getName().lastIndexOf("\\")+1);  //""
 if(filename==null || filename.trim().equals("")){
  continue;
 }

8、為避免上傳檔案的覆蓋,程式在儲存上傳檔案時,要為每一個檔案生成一個唯一的檔名
 public String generateFileName(String filename){
  //83434-83u483-934934
  return UUID.randomUUID().toString() + "_" + filename;
 }

9、為避免在一個資料夾下面儲存超過1000個檔案,影響檔案訪問效能,程式應該把上傳檔案打散後儲存。
 public String generateSavePath(String path,String filename){
  int hashcode = filename.hashCode();  //121221
  int dir1 = hashcode&15;
  int dir2 = (hashcode>>4)&0xf;
  
  String savepath = path + File.separator + dir1 + File.separator + dir2;
  File file = new File(savepath);
  if(!file.exists()){
   file.mkdirs();
  }
  return savepath;
 }

10、監聽上傳進度
  ServletFileUpload upload = new ServletFileUpload(factory);
   upload.setProgressListener(new ProgressListener(){
    public void update(long pBytesRead, long pContentLength, int pItems) {
     System.out.println("當前已解析:" + pBytesRead);
    }
   });

11、在web頁面中新增動態上傳輸入項

 

相關文章