List of usage examples for org.apache.commons.fileupload FileUploadException toString
public String toString()
From source file:beans.service.FileUploadTool.java
static public String FileUpload(Map<String, String> fields, List<String> filesOnServer, HttpServletRequest request, HttpServletResponse response) { boolean isMultipart = ServletFileUpload.isMultipartContent(request); DiskFileItemFactory factory = new DiskFileItemFactory(); int MaxMemorySize = 10000000; int MaxRequestSize = MaxMemorySize; String tmpDir = System.getProperty("TMP", "/tmp"); System.out.printf("temporary directory:%s", tmpDir); factory.setSizeThreshold(MaxMemorySize); factory.setRepository(new File(tmpDir)); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(MaxRequestSize);// w ww . ja v a 2 s.c om // Parse the request try { List<FileItem> items = upload.parseRequest(request); // Process the uploaded items Iterator<FileItem> iter = items.iterator(); while (iter.hasNext()) { FileItem item = iter.next(); if (item.isFormField()) {//k -v String name = item.getFieldName(); String value = item.getString(); fields.put(name, value); } else { String fieldName = item.getFieldName(); String fileName = item.getName(); if (fileName == null || fileName.length() < 1) { return "file name is empty."; } String localFileName = ServletConfig.fileServerRootDir + File.separator + "tmp" + File.separator + fileName; System.out.printf("upload file:%s", localFileName); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); File uploadedFile = new File(localFileName); item.write(uploadedFile); filesOnServer.add(localFileName); } } return "success"; } catch (FileUploadException e) { e.printStackTrace(); return e.toString(); } catch (Exception e) { e.printStackTrace(); return e.toString(); } }
From source file:msec.org.FileUploadServlet.java
static protected String FileUpload(Map<String, String> fields, List<String> filesOnServer, HttpServletRequest request, HttpServletResponse response) { boolean isMultipart = ServletFileUpload.isMultipartContent(request); // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); int MaxMemorySize = 10000000; int MaxRequestSize = MaxMemorySize; String tmpDir = System.getProperty("TMP", "/tmp"); //System.out.printf("temporary directory:%s", tmpDir); // Set factory constraints factory.setSizeThreshold(MaxMemorySize); factory.setRepository(new File(tmpDir)); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("utf8"); // Set overall request size constraint upload.setSizeMax(MaxRequestSize);/*from w ww . j ava 2s.c o m*/ // Parse the request try { @SuppressWarnings("unchecked") List<FileItem> items = upload.parseRequest(request); // Process the uploaded items Iterator<FileItem> iter = items.iterator(); while (iter.hasNext()) { FileItem item = iter.next(); if (item.isFormField()) {//k -v String name = item.getFieldName(); String value = item.getString("utf-8"); fields.put(name, value); } else { String fieldName = item.getFieldName(); String fileName = item.getName(); if (fileName == null || fileName.length() < 1) { return "file name is empty."; } String localFileName = ServletConfig.fileServerRootDir + File.separator + "tmp" + File.separator + fileName; //System.out.printf("upload file:%s", localFileName); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); File uploadedFile = new File(localFileName); item.write(uploadedFile); filesOnServer.add(localFileName); } } return "success"; } catch (FileUploadException e) { e.printStackTrace(); return e.toString(); } catch (Exception e) { e.printStackTrace(); return e.toString(); } }
From source file:com.krawler.esp.servlets.importToDoTask.java
public static String uploadDocument(HttpServletRequest request, String fileid) throws ServiceException { String result = ""; try {/*w w w .j a va 2s .co m*/ String destinationDirectory = StorageHandler.GetDocStorePath() + StorageHandler.GetFileSeparator() + "importplans"; org.apache.commons.fileupload.DiskFileUpload fu = new org.apache.commons.fileupload.DiskFileUpload(); org.apache.commons.fileupload.FileItem fi = null; org.apache.commons.fileupload.FileItem docTmpFI = null; List fileItems = null; try { fileItems = fu.parseRequest(request); } catch (FileUploadException e) { KrawlerLog.op.warn("Problem While Uploading file :" + e.toString()); } long size = 0; String Ext = ""; String fileName = null; boolean fileupload = false; java.io.File destDir = new java.io.File(destinationDirectory); fu.setSizeMax(-1); fu.setSizeThreshold(4096); fu.setRepositoryPath(destinationDirectory); java.util.HashMap arrParam = new java.util.HashMap(); for (java.util.Iterator k = fileItems.iterator(); k.hasNext();) { fi = (org.apache.commons.fileupload.FileItem) k.next(); arrParam.put(fi.getFieldName(), fi.getString()); if (!fi.isFormField()) { size = fi.getSize(); fileName = new String(fi.getName().getBytes(), "UTF8"); docTmpFI = fi; fileupload = true; } } if (fileupload) { if (!destDir.exists()) { destDir.mkdirs(); } if (fileName.contains(".")) { Ext = fileName.substring(fileName.lastIndexOf(".")); } if (size != 0) { File uploadFile = new File(destinationDirectory + "/" + fileid + Ext); docTmpFI.write(uploadFile); // fildoc(fileid, fileName, fileid + Ext, AuthHandler.getUserid(request), size); result = fileid + Ext; } } } catch (ConfigurationException ex) { Logger.getLogger(importToDoTask.class.getName()).log(Level.SEVERE, null, ex); throw ServiceException.FAILURE("importProjectPlanCSV.uploadDocument", ex); } catch (Exception ex) { Logger.getLogger(importToDoTask.class.getName()).log(Level.SEVERE, null, ex); throw ServiceException.FAILURE("importProjectPlanCSV.uploadDocument", ex); } return result; }
From source file:com.krawler.esp.handlers.basecampHandler.java
public static File getfile(HttpServletRequest request, String docid) throws ConfigurationException { String destinationDirectory = StorageHandler.GetDocStorePath() + StorageHandler.GetFileSeparator() + "baseCamxml"; java.io.File destDir = new java.io.File(destinationDirectory); if (!destDir.exists()) { destDir.mkdirs();/*from ww w .j a va 2 s. c o m*/ } DiskFileUpload fu = new DiskFileUpload(); String Ext = null; File uploadFile = null; List fileItems = null; try { fileItems = fu.parseRequest(request); } catch (FileUploadException e) { KrawlerLog.op.warn("Problem While Uploading file :" + e.toString()); } for (Iterator i = fileItems.iterator(); i.hasNext();) { FileItem fi = (FileItem) i.next(); if (!fi.isFormField()) { String fileName = null; try { fileName = new String(fi.getName().getBytes(), "UTF8"); if (fileName.contains(".")) { Ext = fileName.substring(fileName.lastIndexOf(".")); } if (fi.getSize() != 0) { uploadFile = new File( destinationDirectory + StorageHandler.GetFileSeparator() + docid + ".xml"); fi.write(uploadFile); } } catch (Exception e) { KrawlerLog.op.warn("Problem While Reading file :" + e.toString()); } } } return uploadFile; }
From source file:com.krawler.esp.servlets.ExportImportContactsServlet.java
public static String uploadDocument(HttpServletRequest request, String fileid, String userId) throws ServiceException { String result = ""; try {//from w w w .ja v a2 s .c o m String destinationDirectory = StorageHandler.GetDocStorePath() + StorageHandler.GetFileSeparator() + "importcontacts" + StorageHandler.GetFileSeparator() + userId; org.apache.commons.fileupload.DiskFileUpload fu = new org.apache.commons.fileupload.DiskFileUpload(); org.apache.commons.fileupload.FileItem fi = null; org.apache.commons.fileupload.FileItem docTmpFI = null; List fileItems = null; try { fileItems = fu.parseRequest(request); } catch (FileUploadException e) { KrawlerLog.op.warn("Problem While Uploading file :" + e.toString()); } long size = 0; String Ext = ""; String fileName = null; boolean fileupload = false; java.io.File destDir = new java.io.File(destinationDirectory); fu.setSizeMax(-1); fu.setSizeThreshold(4096); fu.setRepositoryPath(destinationDirectory); java.util.HashMap arrParam = new java.util.HashMap(); for (java.util.Iterator k = fileItems.iterator(); k.hasNext();) { fi = (org.apache.commons.fileupload.FileItem) k.next(); arrParam.put(fi.getFieldName(), fi.getString()); if (!fi.isFormField()) { size = fi.getSize(); fileName = new String(fi.getName().getBytes(), "UTF8"); docTmpFI = fi; fileupload = true; } } if (fileupload) { if (!destDir.exists()) { destDir.mkdirs(); } if (fileName.contains(".")) { Ext = fileName.substring(fileName.lastIndexOf(".")); } if (size != 0) { File uploadFile = new File(destinationDirectory + "/" + fileid + Ext); docTmpFI.write(uploadFile); // fildoc(fileid, fileName, fileid + Ext, AuthHandler.getUserid(request), size); result = fileid + Ext; } } } catch (ConfigurationException ex) { Logger.getLogger(importToDoTask.class.getName()).log(Level.SEVERE, null, ex); throw ServiceException.FAILURE("ExportImportContactServlet.uploadDocument", ex); } catch (Exception ex) { Logger.getLogger(importToDoTask.class.getName()).log(Level.SEVERE, null, ex); throw ServiceException.FAILURE("ExportImportContactServlet.uploadDocument", ex); } return result; }
From source file:com.krawler.esp.servlets.importProjectPlanCSV.java
public static String uploadDocument(HttpServletRequest request, String fileid) throws ServiceException { String result = ""; try {// w ww . j a va 2 s . c o m String destinationDirectory = StorageHandler.GetDocStorePath() + StorageHandler.GetFileSeparator() + "importplans"; org.apache.commons.fileupload.DiskFileUpload fu = new org.apache.commons.fileupload.DiskFileUpload(); org.apache.commons.fileupload.FileItem fi = null; org.apache.commons.fileupload.FileItem docTmpFI = null; List fileItems = null; try { fileItems = fu.parseRequest(request); } catch (FileUploadException e) { KrawlerLog.op.warn("Problem While Uploading file :" + e.toString()); } long size = 0; String Ext = ""; String fileName = null; boolean fileupload = false; java.io.File destDir = new java.io.File(destinationDirectory); fu.setSizeMax(-1); fu.setSizeThreshold(4096); fu.setRepositoryPath(destinationDirectory); java.util.HashMap arrParam = new java.util.HashMap(); for (java.util.Iterator k = fileItems.iterator(); k.hasNext();) { fi = (org.apache.commons.fileupload.FileItem) k.next(); arrParam.put(fi.getFieldName(), fi.getString()); if (!fi.isFormField()) { size = fi.getSize(); fileName = new String(fi.getName().getBytes(), "UTF8"); docTmpFI = fi; fileupload = true; } } if (fileupload) { if (!destDir.exists()) { destDir.mkdirs(); } if (fileName.contains(".")) { Ext = fileName.substring(fileName.lastIndexOf(".")); } if (size != 0) { File uploadFile = new File(destinationDirectory + "/" + fileid + Ext); docTmpFI.write(uploadFile); // fildoc(fileid, fileName, fileid + Ext, AuthHandler.getUserid(request), size); result = fileid + Ext; } } } catch (ConfigurationException ex) { Logger.getLogger(importProjectPlanCSV.class.getName()).log(Level.SEVERE, null, ex); throw ServiceException.FAILURE("importProjectPlanCSV.uploadDocument", ex); } catch (Exception ex) { Logger.getLogger(importProjectPlanCSV.class.getName()).log(Level.SEVERE, null, ex); throw ServiceException.FAILURE("importProjectPlanCSV.uploadDocument", ex); } return result; }
From source file:br.com.SGIURD.utils.FileUploadWrapper.java
/** Constructor. */ public FileUploadWrapper(HttpServletRequest aRequest) throws IOException { super(aRequest); ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); try {/*from w w w . jav a 2 s .c om*/ @SuppressWarnings("unchecked") List<FileItem> fileItems = upload.parseRequest(aRequest); convertToMaps(fileItems); } catch (FileUploadException ex) { throw new IOException("Cannot parse underlying request: " + ex.toString()); } }
From source file:com.itpro.restws.helper.MultipartFromWrapper.java
/** Constructor. */ public MultipartFromWrapper(HttpServletRequest aRequest) throws IOException { super(aRequest); ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); try {/*from w w w . j a v a 2 s. c o m*/ List<FileItem> fileItems = upload.parseRequest(aRequest); convertToMaps(fileItems); } catch (FileUploadException ex) { throw new IOException("Cannot parse underlying request: " + ex.toString()); } }
From source file:hirondelle.situris.main.centrosInteresse.FileUploadWrapper.java
/** Constructor. */ public FileUploadWrapper(HttpServletRequest aRequest) throws IOException { super(aRequest); ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); try {//from www . ja v a 2 s . c om List<FileItem> fileItems = upload.parseRequest(aRequest); convertToMaps(fileItems); } catch (FileUploadException ex) { throw new IOException("Cannot parse underlying request: " + ex.toString()); } }
From source file:com.google.zxing.web.DecodeServlet.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!ServletFileUpload.isMultipartContent(request)) { log.info("File upload was not multipart"); response.sendRedirect("badimage.jspx"); return;// ww w .ja va 2 s . com } ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory); upload.setFileSizeMax(MAX_IMAGE_SIZE); // Parse the request try { for (FileItem item : upload.parseRequest(request)) { if (!item.isFormField()) { if (item.getSize() <= MAX_IMAGE_SIZE) { log.info("Decoding uploaded file"); try (InputStream is = item.getInputStream()) { processStream(is, request, response); } } else { log.info("Too large"); response.sendRedirect("badimage.jspx"); } break; } } } catch (FileUploadException fue) { log.info(fue.toString()); response.sendRedirect("badimage.jspx"); } }