Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. package FileUploader;
  2.  
  3. import java.io.File;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.Part;
  6.  
  7.  
  8. public class FileUploader {
  9.    
  10.         private final boolean DEBUG = false;
  11.    
  12.         private String fileName;
  13.         private String applicationPath;
  14.         private String UPLOAD_DIR;
  15.         private String uploadFilePath;
  16.  
  17.         private HttpServletRequest request;
  18.        
  19.         private String username;
  20.         private String task;
  21.                
  22.         public FileUploader(HttpServletRequest request,String username,String task){
  23.            
  24.             applicationPath = request.getServletContext().getRealPath("");
  25.             UPLOAD_DIR = "uploads";
  26.            
  27.             this.username = username;
  28.             this.task = task;
  29.             this.request = request;
  30.            
  31.             uploadFilePath = applicationPath + File.separator + UPLOAD_DIR + "\\" + username + "\\" + task;
  32.            
  33.             if(DEBUG){
  34.                 System.out.println(this.applicationPath);
  35.                 System.out.println(this.uploadFilePath);
  36.                 System.out.println(this.UPLOAD_DIR);
  37.             }
  38.            
  39.         }
  40.        
  41.         public String getFileName(Part part) {
  42.             String contentDisp = part.getHeader("content-disposition");
  43.             if(DEBUG){
  44.                 System.out.println("content-disposition header= "+contentDisp);
  45.             }
  46.             String[] tokens = contentDisp.split(";");
  47.             for (String token : tokens) {
  48.                 if (token.trim().startsWith("filename")) {
  49.                     return token.substring(token.indexOf("=") + 2, token.length()-1);
  50.                 }
  51.             }
  52.             return "";
  53.         }
  54.        
  55.         public void Upload(){
  56.             File fileSaveDir = new File(uploadFilePath);
  57.             if (!fileSaveDir.exists()) {
  58.                 fileSaveDir.mkdirs();
  59.             }
  60.            
  61.             if(DEBUG){
  62.                 System.out.println("Upload File Directory="+fileSaveDir.getAbsolutePath());
  63.             }
  64.  
  65.             fileName = null;
  66.             try {
  67.                 for (Part part : request.getParts()) {
  68.                     fileName = getFileName(part);
  69.                     part.write(uploadFilePath + File.separator + fileName);
  70.                 }
  71.             } catch (Exception e) {
  72.                 System.out.println(e);
  73.             }
  74.         }
  75.        
  76.  
  77.     public String getFileName() {
  78.         return fileName;
  79.     }    
  80.        
  81.     public String getApplicationPath() {
  82.         return applicationPath;
  83.     }
  84.  
  85.     public String getUploadDir() {
  86.         return UPLOAD_DIR;
  87.     }
  88.  
  89.     public String getUploadFilePath() {
  90.         return uploadFilePath;
  91.     }
  92.  
  93.     public HttpServletRequest getRequest() {
  94.         return request;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement