List of usage examples for org.apache.commons.fileupload FileUploadException toString
public String toString()
From source file:hu.sztaki.lpds.pgportal.portlets.workflow.WorkflowUploadPortlet.java
@Override protected void doUpload(ActionRequest request, ActionResponse response) { PortletContext context = getPortletContext(); context.log("[FileUploadPortlet] doUpload() called"); try {// w ww . ja v a 2s .c om DiskFileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload pfu = new PortletFileUpload(factory); pfu.setSizeMax(uploadMaxSize); // Maximum upload size pfu.setProgressListener((ProgressListener) new FileUploadProgressListener()); PortletSession ps = request.getPortletSession(); if (ps.getAttribute("uploads", ps.APPLICATION_SCOPE) == null) ps.setAttribute("uploads", new Hashtable<String, ProgressListener>()); //get the FileItems String fieldName = null; List fileItems = pfu.parseRequest(request); Iterator iter = fileItems.iterator(); File serverSideFile = null; Hashtable h = new Hashtable(); //fileupload while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); // retrieve hidden parameters if item is a form field if (item.isFormField()) { fieldName = item.getFieldName(); if ("newGrafName".equals(fieldName)) h.put("newGrafName", item.getString()); if ("newAbstName".equals(fieldName)) h.put("newAbstName", item.getString()); if ("newRealName".equals(fieldName)) h.put("newRealName", item.getString()); } else { // item is not a form field, do file upload Hashtable<String, ProgressListener> tmp = (Hashtable<String, ProgressListener>) ps .getAttribute("uploads");//,ps.APPLICATION_SCOPE pfu.setProgressListener((ProgressListener) new FileUploadProgressListener()); String s = item.getName(); s = FilenameUtils.getName(s); ProgressListener pl = pfu.getProgressListener(); tmp.put(s, pl); ps.setAttribute("uploads", tmp); String tempDir = System.getProperty("java.io.tmpdir") + "/uploads/" + request.getRemoteUser(); File f = new File(tempDir); if (!f.exists()) f.mkdirs(); serverSideFile = new File(tempDir, s); item.write(serverSideFile); item.delete(); context.log("[FileUploadPortlet] - file " + s + " uploaded successfully to " + tempDir); } } // file upload to storage try { ServiceType st = InformationBase.getI().getService("wfs", "portal", new Hashtable(), new Vector()); h.put("senderObj", "ZipFileSender"); h.put("portalURL", PropertyLoader.getInstance().getProperty("service.url")); h.put("wfsID", st.getServiceUrl()); h.put("userID", request.getRemoteUser()); Hashtable hsh = new Hashtable(); // st = InformationBase.getI().getService("storage", "portal", hsh, new Vector()); // hsh.put("url", "http://localhost:8080/storage"); st = InformationBase.getI().getService("storage", "portal", hsh, new Vector()); PortalStorageClient psc = (PortalStorageClient) Class.forName(st.getClientObject()).newInstance(); psc.setServiceURL(st.getServiceUrl()); psc.setServiceID("/receiver"); if (serverSideFile != null) psc.fileUpload(serverSideFile, "fileName", h); } catch (Exception ex) { response.setRenderParameter("full", "error.upload"); ex.printStackTrace(); return; } ps.removeAttribute("uploads", ps.APPLICATION_SCOPE); } catch (FileUploadException fue) { response.setRenderParameter("full", "error.upload"); fue.printStackTrace(); context.log("[FileUploadPortlet] - failed to upload file - " + fue.toString()); return; } catch (Exception e) { e.printStackTrace(); response.setRenderParameter("full", "error.exception"); context.log("[FileUploadPortlet] - failed to upload file - " + e.toString()); return; } response.setRenderParameter("full", "action.succesfull"); }
From source file:com.krawler.esp.servlets.FileImporterServlet.java
public File getfile(HttpServletRequest request, String fileid) throws ServiceException { File uploadFile = null;/* w w w . jav a 2 s . co m*/ try { String destinationDirectory = StorageHandler.GetDocStorePath() + StorageHandler.GetFileSeparator() + "importplans"; File destdir = new File(destinationDirectory); if (!destdir.exists()) { destdir.mkdir(); } DiskFileUpload fu = new DiskFileUpload(); String Ext = ""; 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(".")); } long size = fi.getSize(); if (fi.getSize() != 0) { String projid = request.getParameter("projectid"); uploadFile = new File( destinationDirectory + StorageHandler.GetFileSeparator() + fileid + Ext); fi.write(uploadFile); fildoc(fileid, fileName, projid, fileid + Ext, AuthHandler.getUserid(request), size); } } catch (IOException ex) { KrawlerLog.op.warn("Problem While Reading file :" + ex.toString()); throw ServiceException.FAILURE("FileImporterServlet.getfile", ex); } catch (ServiceException ex) { KrawlerLog.op.warn("Problem While Reading file :" + ex); throw ServiceException.FAILURE("FileImporterServlet.getfile", ex); } catch (Exception ex) { KrawlerLog.op.warn("Problem While Reading file :" + ex.toString()); throw ServiceException.FAILURE("FileImporterServlet.getfile", ex); } } else { arrParam.put(fi.getFieldName(), fi.getString()); } } } catch (ConfigurationException ex) { KrawlerLog.op.warn("Problem Storing Data In DB [FileImporterServlet.storeInDB()]:" + ex); throw ServiceException.FAILURE("FileImporterServlet.getfile", ex); } return uploadFile; }
From source file:eionet.gdem.utils.MultipartFileUpload.java
/** * @param request// ww w .ja v a2s .co m * Servlet request * @throws GDEMException * If an error occurs */ public void processMultiPartRequest(HttpServletRequest request) throws GDEMException { List items = null; setEncoding(request.getCharacterEncoding()); try { items = upload.parseRequest(request); } catch (FileUploadException fue) { throw new GDEMException(fue.toString()); } Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); // String itemName = item.getName(); String fieldName = item.getFieldName(); // String id = (String) this.parameterNames.get(fieldName); /* * System.out.println("Multipart item name is: " + itemName + " and fieldname is: " + fieldName); // + " and id is: " + * id); System.out.println("Is formfield: " + item.isFormField()); System.out.println("Content: " + item.getString()); */ if (item.isFormField()) { // It's a field name, it means that we got a non-file // form field. Upload is not required. String itemValue = null; try { // use encoding from request itemValue = item.getString(getEncoding()); } catch (UnsupportedEncodingException e) { // use default encoding itemValue = item.getString(); } if (_params.containsKey(fieldName)) { Object curObj = _params.get(fieldName); List valueList = null; if (curObj instanceof String) { valueList = new ArrayList(); valueList.add((String) curObj); } else if (curObj instanceof List) { valueList = (List) curObj; } valueList.add(itemValue); _params.put(fieldName, valueList); } else { _params.put(fieldName, itemValue); } } else { _fileItem = item; addFileItem(_fileItem); String fileName = getFileItemName(_fileItem.getName()); setFileName(fileName); if (_uploadAtOnce) saveFile(); } } }
From source file:com.krawler.esp.servlets.ExportImportContactsServlet.java
private File getfile(HttpServletRequest request) { DiskFileUpload fu = new DiskFileUpload(); String Ext = null;//from w w w . jav a 2 s . c om 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 = File.createTempFile("contacts", ".csv"); fi.write(uploadFile); } } catch (Exception e) { KrawlerLog.op.warn("Problem While Reading file :" + e.toString()); } } else { arrParam.put(fi.getFieldName(), fi.getString()); } } return uploadFile; }
From source file:com.krawler.esp.servlets.importICSServlet.java
private File getfile(HttpServletRequest request) { DiskFileUpload fu = new DiskFileUpload(); String Ext = null;// w w w .j a v a2 s .co m 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 = File.createTempFile("iCalDeskeraTemp", ".ics"); fi.write(uploadFile); } } catch (Exception e) { KrawlerLog.op.warn("Problem While Reading file :" + e.toString()); } } else { arrParam.put(fi.getFieldName(), fi.getString()); } } return uploadFile; }
From source file:com.krawler.spring.importFunctionality.ImportUtil.java
/** * @param request/*from ww w. jav a2 s. c o m*/ * @param fileid * @return * @throws ServiceException */ private static String uploadDocument(HttpServletRequest request, String fileid) throws ServiceException { String result = ""; try { String destinationDirectory = storageHandlerImpl.GetDocStorePath() + "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("UTF-8")); 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) { int startIndex = fileName.contains("\\") ? (fileName.lastIndexOf("\\") + 1) : 0; fileName = fileName.substring(startIndex, fileName.lastIndexOf(".")); fileName = fileName.replaceAll(" ", ""); fileName = fileName.replaceAll("/", ""); result = fileName + "_" + fileid + Ext; File uploadFile = new File(destinationDirectory + "/" + result); docTmpFI.write(uploadFile); // fildoc(fileid, fileName, fileid + Ext, AuthHandler.getUserid(request), size); } } } // catch (ConfigurationException ex) { // Logger.getLogger(ExportImportContacts.class.getName()).log(Level.SEVERE, null, ex); // throw ServiceException.FAILURE("ExportImportContacts.uploadDocument", ex); // } catch (Exception ex) { Logger.getLogger(ImportHandler.class.getName()).log(Level.SEVERE, null, ex); throw ServiceException.FAILURE("ExportImportContacts.uploadDocument", ex); } return result; }
From source file:hu.sztaki.lpds.pgportal.portlets.workflow.RealWorkflowPortlet.java
@Override public void doUpload(ActionRequest request, ActionResponse response) { PortletContext context = getPortletContext(); context.log("[FileUploadPortlet] doUpload() called"); try {/* w w w. ja va 2s.c om*/ DiskFileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload pfu = new PortletFileUpload(factory); pfu.setSizeMax(uploadMaxSize); // Maximum upload size pfu.setProgressListener((ProgressListener) new FileUploadProgressListener()); PortletSession ps = request.getPortletSession(); if (ps.getAttribute("uploaded") == null) ps.setAttribute("uploaded", new Vector<String>()); ps.setAttribute("upload", pfu); //get the FileItems String fieldName = null; List fileItems = pfu.parseRequest(request); Iterator iter = fileItems.iterator(); File serverSideFile = null; while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); // retrieve hidden parameters if item is a form field if (item.isFormField()) { fieldName = item.getFieldName(); } else { // item is not a form field, do file upload Hashtable<String, ProgressListener> tmp = (Hashtable<String, ProgressListener>) ps .getAttribute("uploads", ps.APPLICATION_SCOPE); String s = item.getName(); s = FilenameUtils.getName(s); ps.setAttribute("uploading", s); String tempDir = System.getProperty("java.io.tmpdir") + "/uploads/" + request.getRemoteUser(); File f = new File(tempDir); if (!f.exists()) f.mkdirs(); serverSideFile = new File(tempDir, s); item.write(serverSideFile); item.delete(); context.log("[FileUploadPortlet] - file " + s + " uploaded successfully to " + tempDir); // file upload to storage try { Hashtable h = new Hashtable(); h.put("portalURL", PropertyLoader.getInstance().getProperty("service.url")); h.put("userID", request.getRemoteUser()); String uploadField = ""; // retrieve hidden parameters if item is a form field for (FileItem item0 : (List<FileItem>) fileItems) { if (item0.isFormField()) h.put(item0.getFieldName(), item0.getString()); else uploadField = item0.getFieldName(); } Hashtable hsh = new Hashtable(); ServiceType st = InformationBase.getI().getService("storage", "portal", hsh, new Vector()); PortalStorageClient psc = (PortalStorageClient) Class.forName(st.getClientObject()) .newInstance(); psc.setServiceURL(st.getServiceUrl()); psc.setServiceID("/upload"); if (serverSideFile != null) { psc.fileUpload(serverSideFile, uploadField, h); } } catch (Exception ex) { response.setRenderParameter("full", "error.upload"); ex.printStackTrace(); return; } ((Vector<String>) ps.getAttribute("uploaded")).add(s); } } // ps.removeAttribute("uploads",ps.APPLICATION_SCOPE); ps.setAttribute("finaluploads", ""); } catch (FileUploadException fue) { response.setRenderParameter("full", "error.upload"); fue.printStackTrace(); context.log("[FileUploadPortlet] - failed to upload file - " + fue.toString()); return; } catch (Exception e) { e.printStackTrace(); response.setRenderParameter("full", "error.exception"); context.log("[FileUploadPortlet] - failed to upload file - " + e.toString()); return; } response.setRenderParameter("full", "action.succesfull"); }
From source file:hu.sztaki.lpds.pgportal.portlets.workflow.EasyWorkflowPortlet.java
@Override public void doUpload(ActionRequest request, ActionResponse response) { PortletContext context = getPortletContext(); context.log("[FileUploadPortlet] doUpload() called"); try {/*from w w w . ja v a 2 s . co m*/ DiskFileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload pfu = new PortletFileUpload(factory); pfu.setSizeMax(uploadMaxSize); // Maximum upload size pfu.setProgressListener((ProgressListener) new FileUploadProgressListener()); PortletSession ps = request.getPortletSession(); if (ps.getAttribute("uploaded") == null) ps.setAttribute("uploaded", new Vector<String>()); ps.setAttribute("upload", pfu); //get the FileItems String fieldName = null; List fileItems = pfu.parseRequest(request); Iterator iter = fileItems.iterator(); File serverSideFile = null; while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); // retrieve hidden parameters if item is a form field if (item.isFormField()) { fieldName = item.getFieldName(); } else { // item is not a form field, do file upload Hashtable<String, ProgressListener> tmp = (Hashtable<String, ProgressListener>) ps .getAttribute("uploads", ps.APPLICATION_SCOPE); String s = item.getName(); s = FilenameUtils.getName(s); ps.setAttribute("uploading", s); String tempDir = System.getProperty("java.io.tmpdir") + "/uploads/" + request.getRemoteUser(); File f = new File(tempDir); if (!f.exists()) f.mkdirs(); serverSideFile = new File(tempDir, s); item.write(serverSideFile); item.delete(); context.log("[FileUploadPortlet] - file " + s + " uploaded successfully to " + tempDir); // file upload to storage try { Hashtable h = new Hashtable(); h.put("portalURL", PropertyLoader.getInstance().getProperty("service.url")); h.put("userID", request.getRemoteUser()); String uploadField = ""; // retrieve hidden parameters if item is a form field for (FileItem item0 : (List<FileItem>) fileItems) { if (item0.isFormField()) h.put(item0.getFieldName(), item0.getString()); else uploadField = item0.getFieldName(); } Hashtable hsh = new Hashtable(); ServiceType st = InformationBase.getI().getService("storage", "portal", hsh, new Vector()); PortalStorageClient psc = (PortalStorageClient) Class.forName(st.getClientObject()) .newInstance(); psc.setServiceURL(st.getServiceUrl()); psc.setServiceID("/upload"); if (serverSideFile != null) { psc.fileUpload(serverSideFile, uploadField, h); } } catch (Exception ex) { response.setRenderParameter("full", "error.upload"); ex.printStackTrace(); return; } ((Vector<String>) ps.getAttribute("uploaded")).add(s); } } // ps.removeAttribute("uploads",ps.APPLICATION_SCOPE); ps.setAttribute("finaluploads", ""); } catch (SizeLimitExceededException see) { response.setRenderParameter("full", "error.upload.sizelimit"); request.getPortletSession().setAttribute("finaluploads", ""); see.printStackTrace(); context.log("[FileUploadPortlet] - failed to upload file - " + see.toString()); return; } catch (FileUploadException fue) { response.setRenderParameter("full", "error.upload"); fue.printStackTrace(); context.log("[FileUploadPortlet] - failed to upload file - " + fue.toString()); return; } catch (Exception e) { e.printStackTrace(); response.setRenderParameter("full", "error.exception"); context.log("[FileUploadPortlet] - failed to upload file - " + e.toString()); return; } response.setRenderParameter("full", "action.succesfull"); }
From source file:ngse.org.FileUploadTool.java
static public 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 w w. j a v a2s . co m // 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("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:org.apache.jackrabbit.server.util.HttpMultipartPost.java
/** * /*from w w w . j av a 2 s .c om*/ * @param request * @param tmpDir * @throws IOException */ private void extractMultipart(HttpServletRequest request, File tmpDir) throws IOException { if (!ServletFileUpload.isMultipartContent(request)) { log.debug("Request does not contain multipart content -> ignoring."); return; } ServletFileUpload upload = new ServletFileUpload(getFileItemFactory(tmpDir)); // make sure the content disposition headers are read with the charset // specified in the request content type (or UTF-8 if no charset is specified). // see JCR if (request.getCharacterEncoding() == null) { upload.setHeaderEncoding("UTF-8"); } try { @SuppressWarnings("unchecked") List<FileItem> fileItems = upload.parseRequest(request); for (FileItem fileItem : fileItems) { addItem(fileItem); } } catch (FileUploadException e) { log.error("Error while processing multipart.", e); throw new IOException(e.toString()); } }