Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void uploadRequestHandler(ServletFileUpload upload, HttpServletRequest request)
- {
- // Handle the request
- String fileName = "blank";
- try{
- List items = upload.parseRequest(request);
- //Process the uploaded items
- Iterator iter = items.iterator();
- File uploadedFile = new File("home" + File.separator + "temp");
- if(uploadedFile.exists()){
- boolean tempDeleted = uploadedFile.delete();
- if(!tempDeleted)
- throw new Exception("Existing temp file could not be deleted.");
- }
- //write the file
- while (iter.hasNext()) {
- DiskFileItem item = (DiskFileItem) iter.next();
- if(item.isFormField()){
- String fieldName = item.getFieldName();
- String fieldValue = item.getString();
- if(fieldName.equals("fileName"))
- fileName = fieldValue;
- //other form values would need to be handled here, right now only need for fileName
- }else{
- item.write(uploadedFile);
- }
- }
- if(fileName.equals("blank"))
- throw new Exception("File name could not be parsed.");
- //move file
- File wellnessDir = new File("home" + File.separator + "medcottage" + File.separator + "wellness");
- File destination = new File(wellnessDir + File.separator + fileName + ".pdf");
- System.out.println("destination file exists: " + destination.exists());
- System.out.println("file to be moved exists: " + uploadedFile.exists());
- if(destination.exists()){
- boolean deleted = destination.delete();
- if(!deleted)
- throw new Exception("Could not delete file at " + destination);
- }
- FileUtil.move(uploadedFile, new File(wellnessDir + File.separator + fileName + ".pdf"));
- writeResponse();
- } catch (Exception e) {
- System.out.println("Error handling upload request.");
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement