Advertisement
Guest User

Untitled

a guest
Nov 29th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. private void uploadRequestHandler(ServletFileUpload upload, HttpServletRequest request)
  2.   {
  3.     // Handle the request
  4.     String fileName = "blank";
  5.     try{        
  6.       List items = upload.parseRequest(request);
  7.       //Process the uploaded items
  8.       Iterator iter = items.iterator();
  9.       File uploadedFile = new File("home" + File.separator + "temp");
  10.       if(uploadedFile.exists()){
  11.         boolean tempDeleted = uploadedFile.delete();
  12.         if(!tempDeleted)
  13.           throw new Exception("Existing temp file could not be deleted.");
  14.       }
  15.       //write the file
  16.       while (iter.hasNext()) {
  17.         DiskFileItem item = (DiskFileItem) iter.next();
  18.         if(item.isFormField()){
  19.           String fieldName = item.getFieldName();
  20.           String fieldValue = item.getString();
  21.           if(fieldName.equals("fileName"))
  22.             fileName = fieldValue;
  23.             //other form values would need to be handled here, right now only need for fileName
  24.         }else{
  25.           item.write(uploadedFile);
  26.         }
  27.       }
  28.       if(fileName.equals("blank"))
  29.         throw new Exception("File name could not be parsed.");
  30.       //move file
  31.       File wellnessDir = new File("home" + File.separator + "medcottage" + File.separator + "wellness");
  32.       File destination = new File(wellnessDir + File.separator + fileName + ".pdf");
  33.      
  34.       System.out.println("destination file exists: " + destination.exists());
  35.       System.out.println("file to be moved exists: " + uploadedFile.exists());
  36.      
  37.       if(destination.exists()){
  38.         boolean deleted = destination.delete();
  39.         if(!deleted)
  40.           throw new Exception("Could not delete file at " + destination);
  41.       }        
  42.       FileUtil.move(uploadedFile, new File(wellnessDir + File.separator + fileName + ".pdf"));
  43.       writeResponse();
  44.     } catch (Exception e) {
  45.       System.out.println("Error handling upload request.");
  46.       e.printStackTrace();
  47.     }
  48.   }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement