簡單fileupload 上傳

  1. blic class ReceiveFile extends HttpServlet {  
  2.       
  3.     private String uploadPath = "uploadpic/upload/"// 上傳文件的目錄    
  4.     private String tempPath = "uploadpic/uploadtmp/"// 臨時文件目錄    
  5.     private String serverPath = null;   
  6.     private String[] fileType = new String[]{".jpg",".gif",".bmp",".png",".jpeg",".ico"};  
  7.     private int sizeMax = 5;//圖片最大上限    
  8.       
  9.     @Override  
  10.     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  11.             throws ServletException, IOException {  
  12.         // 服務器端根目錄  
  13.         String serverPath = getServletContext().getRealPath("/").replace("\\", "/");    
  14. //        System.out.println(serverPath);  
  15.         //Servlet初始化時執行,如果上傳文件目錄不存在則自動創建    
  16.         if(!new File(serverPath+uploadPath).isDirectory()){   
  17.             new File(serverPath+uploadPath).mkdirs();    
  18.         }    
  19.         if(!new File(serverPath+tempPath).isDirectory()){  
  20.             new File(serverPath+tempPath).mkdirs();  
  21.         }   
  22.         DiskFileItemFactory factory = new DiskFileItemFactory();  
  23.         factory.setSizeThreshold(5*1024); //最大緩存    
  24.         factory.setRepository(new File(serverPath+tempPath));//臨時文件目錄    
  25.             
  26.         ServletFileUpload upload = new ServletFileUpload(factory);  
  27.         upload.setSizeMax(sizeMax*1024*1024);//文件最大上限   
  28.             
  29.         String filePath = null;    
  30.         try {    
  31.             List<FileItem> items = upload.parseRequest(request);//獲取所有文件列表   
  32.             //  
  33.             for (int i=0;i<items.size();i++) {  
  34.                 //裏面一個for循環,獲取一行的數據  
  35.                 FileItem item = items.get(i);  
  36. <span style="white-space:pre">  </span>          if(!item.isFormField()){//文件名    
  37.                     String fileName = item.getName().toLowerCase();  
  38.                     if(fileName.endsWith(fileType[0])||fileName.endsWith(fileType[1])||fileName.endsWith(fileType[2])||fileName.endsWith(fileType[3])||fileName.endsWith(fileType[4])||fileName.endsWith(fileType[5])){    
  39. //                        String uuid = UUID.randomUUID().toString();    
  40.                         filePath = serverPath+uploadPath+fileName;  
  41. //                        System.out.println(filePath);  
  42.                         File file = new File(filePath);  
  43.                         item.write(file);  
  44.                         System.out.println(fileName);  
  45.                      }else {  
  46.                         request.setAttribute("errorMsg""上傳失敗,請確認上傳的文件存在並且類型是圖片!");  
  47.                         request.getRequestDispatcher("uploaderror.jsp").forward(request,response);   
  48.                     }  
  49.                 }else {  
  50.                   //非文件流     
  51.                     String value=item.getString();  
  52.                     value = new String(value.getBytes("ISO-8859-1"),"UTF-8");  
  53. //                    System.out.println(value);  
  54.                     System.out.println(value);  
  55.                 }  
  56.                   
  57.             }   
  58.         } catch (Exception e) {  
  59.             e.printStackTrace();    
  60.             request.setAttribute("errorMsg""上傳失敗,請確認上傳的文件存在並且類型是圖片!");  
  61.             request.getRequestDispatcher("uploaderror.jsp").forward(request,response);   
  62.         }  
  63.     }  
  64.       
  65.     @Override  
  66.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  67.             throws ServletException, IOException {  
  68.         this.doPost(req, resp);  
  69.     }  
  70. }  
  1. public class ReceiveFile extends HttpServlet {  
  2.       
  3.     private String uploadPath = "uploadpic/upload/"// 上傳文件的目錄    
  4.     private String tempPath = "uploadpic/uploadtmp/"// 臨時文件目錄    
  5.     private String serverPath = null;   
  6.     private String[] fileType = new String[]{".jpg",".gif",".bmp",".png",".jpeg",".ico"};  
  7.     private int sizeMax = 5;//圖片最大上限    
  8.       
  9.     @Override  
  10.     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  11.             throws ServletException, IOException {  
  12.         // 服務器端根目錄  
  13.         String serverPath = getServletContext().getRealPath("/").replace("\\", "/");    
  14. //        System.out.println(serverPath);  
  15.         //Servlet初始化時執行,如果上傳文件目錄不存在則自動創建    
  16.         if(!new File(serverPath+uploadPath).isDirectory()){   
  17.             new File(serverPath+uploadPath).mkdirs();    
  18.         }    
  19.         if(!new File(serverPath+tempPath).isDirectory()){  
  20.             new File(serverPath+tempPath).mkdirs();  
  21.         }   
  22.         DiskFileItemFactory factory = new DiskFileItemFactory();  
  23.         factory.setSizeThreshold(5*1024); //最大緩存    
  24.         factory.setRepository(new File(serverPath+tempPath));//臨時文件目錄    
  25.             
  26.         ServletFileUpload upload = new ServletFileUpload(factory);  
  27.         upload.setSizeMax(sizeMax*1024*1024);//文件最大上限   
  28.             
  29.         String filePath = null;    
  30.         try {    
  31.             List<FileItem> items = upload.parseRequest(request);//獲取所有文件列表   
  32.             //  
  33.             for (int i=0;i<items.size();i++) {  
  34.                 //裏面一個for循環,獲取一行的數據  
  35.                 FileItem item = items.get(i);  
  36. <span style="white-space:pre">  </span>          if(!item.isFormField()){//文件名    
  37.                     String fileName = item.getName().toLowerCase();  
  38.                     if(fileName.endsWith(fileType[0])||fileName.endsWith(fileType[1])||fileName.endsWith(fileType[2])||fileName.endsWith(fileType[3])||fileName.endsWith(fileType[4])||fileName.endsWith(fileType[5])){    
  39. //                        String uuid = UUID.randomUUID().toString();    
  40.                         filePath = serverPath+uploadPath+fileName;  
  41. //                        System.out.println(filePath);  
  42.                         File file = new File(filePath);  
  43.                         item.write(file);  
  44.                         System.out.println(fileName);  
  45.                      }else {  
  46.                         request.setAttribute("errorMsg""上傳失敗,請確認上傳的文件存在並且類型是圖片!");  
  47.                         request.getRequestDispatcher("uploaderror.jsp").forward(request,response);   
  48.                     }  
  49.                 }else {  
  50.                   //非文件流     
  51.                     String value=item.getString();  
  52.                     value = new String(value.getBytes("ISO-8859-1"),"UTF-8");  
  53. //                    System.out.println(value);  
  54.                     System.out.println(value);  
  55.                 }  
  56.                   
  57.             }   
  58.         } catch (Exception e) {  
  59.             e.printStackTrace();    
  60.             request.setAttribute("errorMsg""上傳失敗,請確認上傳的文件存在並且類型是圖片!");  
  61.             request.getRequestDispatcher("uploaderror.jsp").forward(request,response);   
  62.         }  
  63.     }  
  64.       
  65.     @Override  
  66.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  67.             throws ServletException, IOException {  
  68.         this.doPost(req, resp);  
  69.     }  
  70. }  
  1. public class ReceiveFile extends HttpServlet {  
  2.       
  3.     private String uploadPath = "uploadpic/upload/"// 上傳文件的目錄    
  4.     private String tempPath = "uploadpic/uploadtmp/"// 臨時文件目錄    
  5.     private String serverPath = null;   
  6.     private String[] fileType = new String[]{".jpg",".gif",".bmp",".png",".jpeg",".ico"};  
  7.     private int sizeMax = 5;//圖片最大上限    
  8.       
  9.     @Override  
  10.     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  11.             throws ServletException, IOException {  
  12.         // 服務器端根目錄  
  13.         String serverPath = getServletContext().getRealPath("/").replace("\\", "/");    
  14. //        System.out.println(serverPath);  
  15.         //Servlet初始化時執行,如果上傳文件目錄不存在則自動創建    
  16.         if(!new File(serverPath+uploadPath).isDirectory()){   
  17.             new File(serverPath+uploadPath).mkdirs();    
  18.         }    
  19.         if(!new File(serverPath+tempPath).isDirectory()){  
  20.             new File(serverPath+tempPath).mkdirs();  
  21.         }   
  22.         DiskFileItemFactory factory = new DiskFileItemFactory();  
  23.         factory.setSizeThreshold(5*1024); //最大緩存    
  24.         factory.setRepository(new File(serverPath+tempPath));//臨時文件目錄    
  25.             
  26.         ServletFileUpload upload = new ServletFileUpload(factory);  
  27.         upload.setSizeMax(sizeMax*1024*1024);//文件最大上限   
  28.             
  29.         String filePath = null;    
  30.         try {    
  31.             List<FileItem> items = upload.parseRequest(request);//獲取所有文件列表   
  32.             //  
  33.             for (int i=0;i<items.size();i++) {  
  34.                 //裏面一個for循環,獲取一行的數據  
  35.                 FileItem item = items.get(i);  
  36. <span style="white-space:pre">  </span>          if(!item.isFormField()){//文件名    
  37.                     String fileName = item.getName().toLowerCase();  
  38.                     if(fileName.endsWith(fileType[0])||fileName.endsWith(fileType[1])||fileName.endsWith(fileType[2])||fileName.endsWith(fileType[3])||fileName.endsWith(fileType[4])||fileName.endsWith(fileType[5])){    
  39. //                        String uuid = UUID.randomUUID().toString();    
  40.                         filePath = serverPath+uploadPath+fileName;  
  41. //                        System.out.println(filePath);  
  42.                         File file = new File(filePath);  
  43.                         item.write(file);  
  44.                         System.out.println(fileName);  
  45.                      }else {  
  46.                         request.setAttribute("errorMsg""上傳失敗,請確認上傳的文件存在並且類型是圖片!");  
  47.                         request.getRequestDispatcher("uploaderror.jsp").forward(request,response);   
  48.                     }  
  49.                 }else {  
  50.                   //非文件流     
  51.                     String value=item.getString();  
  52.                     value = new String(value.getBytes("ISO-8859-1"),"UTF-8");  
  53. //                    System.out.println(value);  
  54.                     System.out.println(value);  
  55.                 }  
  56.                   
  57.             }   
  58.         } catch (Exception e) {  
  59.             e.printStackTrace();    
  60.             request.setAttribute("errorMsg""上傳失敗,請確認上傳的文件存在並且類型是圖片!");  
  61.             request.getRequestDispatcher("uploaderror.jsp").forward(request,response);   
  62.         }  
  63.     }  
  64.       
  65.     @Override  
  66.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  67.             throws ServletException, IOException {  
  68.         this.doPost(req, resp);  
  69.     }  
  70. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章