List of usage examples for org.apache.commons.fileupload.disk DiskFileItemFactory setRepository
public void setRepository(File repository)
From source file:com.groupon.odo.Proxy.java
private DiskFileItemFactory createDiskFactory() { // Create a factory for disk-based file items DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); // Set factory constraints diskFileItemFactory.setSizeThreshold(this.getMaxFileUploadSize()); diskFileItemFactory.setRepository(FILE_UPLOAD_TEMP_DIRECTORY); return diskFileItemFactory; }
From source file:it.infn.ct.mi_parallel_portlet.java
/** * This method manages the user input fields managing two cases * distinguished by the type of the input <form ... statement The use of * upload file controls needs the use of "multipart/form-data" while the * else condition of the isMultipartContent check manages the standard input * case. The multipart content needs a manual processing of all <form items * All form' input items are identified by the 'name' input property inside * the jsp file/*w ww. ja va 2 s . c om*/ * @param request ActionRequest instance (processAction) * @param appInput AppInput instance storing the jobSubmission data */ void getInputForm(ActionRequest request, AppInput appInput) { if (PortletFileUpload.isMultipartContent(request)) { try { FileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload upload = new PortletFileUpload(factory); List items = upload.parseRequest(request); File repositoryPath = new File("/tmp"); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); diskFileItemFactory.setRepository(repositoryPath); Iterator iter = items.iterator(); String logstring = ""; int i = 0; int j = 0; while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fieldName = item.getFieldName(); // String fileName = item.getName(); // String contentType = item.getContentType(); // boolean isInMemory = item.isInMemory(); // long sizeInBytes = item.getSize(); // Prepare a log string with field list logstring += LS + "field name: '" + fieldName + "' - '" + item.getString() + "'"; switch (inputControlsIds.valueOf(fieldName)) { case JobIdentifier: appInput.jobIdentifier = item.getString(); break; case collection_type: appInput.collectionType = item.getString(); break; case task_number: appInput.taskNumber = item.getString(); break; case parametric_executable: appInput.executable = item.getString(); break; case executables: appInput.executables.add(item.getString()); i++; break; case argument: appInput.arguments.add(item.getString()); j++; break; case final_executable: appInput.finalJobExecutable = item.getString(); break; case final_argument: appInput.finalJobArgument = item.getString(); break; default: _log.warn("Unhandled input field: '" + fieldName + "' - '" + item.getString() + "'"); } // switch fieldName } // while iter.hasNext() _log.info(LS + "Reporting" + LS + "---------" + LS + logstring + LS); } // try catch (Exception e) { _log.info("Caught exception while processing files to upload: '" + e.toString() + "'"); } } // The input form do not use the "multipart/form-data" else { // Retrieve from the input form the given application values appInput.jobIdentifier = (String) request.getParameter("JobIdentifier"); appInput.collectionType = (String) request.getParameter("collection_type"); appInput.taskNumber = (String) request.getParameter("task_number"); } // ! isMultipartContent String tmp = ""; for (int i = 0; i < appInput.executables.size(); i++) { if (!appInput.collectionType.equals("JOB_PARAMETRIC")) { tmp += LS + "executables[" + i + "]:" + appInput.executables.get(i) + "'"; } else { tmp += LS + "executable:" + appInput.executable + "'"; } tmp += LS + "arguments[" + i + "]:" + appInput.arguments.get(i) + "'"; } // Show into the log the taken inputs _log.info(LS + "Taken input parameters:" + LS + "-----------------------" // + LS + "inputFileName: '" + appInput.inputFileName + "'" // + LS + "inputFileText: '" + appInput.inputFileText + "'" + LS + "jobIdentifier: '" + appInput.jobIdentifier + "'" + LS + "collectionType: '" + appInput.collectionType + "'" + LS + "taskNumber: '" + appInput.taskNumber + "'" + LS + tmp + LS + "finalJobExecutable: '" + appInput.finalJobExecutable + "'" + LS + "finalJobArgument: '" + appInput.finalJobArgument + "'" + LS); }
From source file:it.infn.ct.molon_portlet.java
/** * This method manages the user input fields managing two cases * distinguished by the type of the input <form ... statement The use of * upload file controls needs the use of "multipart/form-data" while the * else condition of the isMultipartContent check manages the standard input * case. The multipart content needs a manual processing of all <form items * All form' input items are identified by the 'name' input property inside * the jsp file/* ww w . j a va2 s. c om*/ * @param request ActionRequest instance (processAction) * @param appInput AppInput instance storing the jobSubmission data */ void getInputForm(ActionRequest request, molon_portlet.AppInput appInput) { if (PortletFileUpload.isMultipartContent(request)) { try { FileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload upload = new PortletFileUpload(factory); List items = upload.parseRequest(request); File repositoryPath = new File("/tmp"); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); diskFileItemFactory.setRepository(repositoryPath); Iterator iter = items.iterator(); String logstring = ""; int i = 0; int j = 0; while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); // Prepare a log string with field list switch (molon_portlet.inputControlsIds.valueOf(fieldName)) { case jobPID: logstring += LS + "FIELD IS A JOB PID WITH NAME : '" + item.getString() + "'"; appInput.jobPID = item.getString(); break; case inputURL: logstring += LS + "FIELD IS AN INPUT URL WITH NAME : '" + item.getString() + "'"; appInput.inputURL = item.getString(); break; case JobIdentifier: logstring += LS + "FIELD IS A JOB IDENTIFIER WITH NAME : '" + item.getString() + "'"; appInput.jobIdentifier = item.getString(); break; case file_inputFile: if (fileName == "") break; logstring += LS + "FIELD IS A FILE WITH NAME : '" + fileName + "'"; appInput.inputFile = appInput.path + "/" + fileName; logstring += LS + "COPYING IT TO PATH : '" + appInput.path + "'"; copyFile(item, appInput.inputFile); break; default: _log.warn("Unhandled input field: '" + fieldName + "' - '" + item.getString() + "'"); } // switch fieldName } // while iter.hasNext() _log.info(LS + "Reporting" + LS + "---------" + LS + logstring + LS); } // try catch (Exception e) { _log.info("Caught exception while processing files to upload: '" + e.toString() + "'"); } } // The input form do not use the "multipart/form-data" else { // Retrieve from the input form the given application values // appInput.inputFileName = (String) request.getParameter("file_inputFile"); // appInput.inputFileText = (String) request.getParameter("inputFile"); appInput.jobIdentifier = (String) request.getParameter("JobIdentifier"); } // ! isMultipartContent // Show into the log the taken inputs _log.info(LS + "Taken input parameters:" + LS + "-----------------------" // + LS + "inputFileName: '" + appInput.inputFileName + "'" // + LS + "inputFileText: '" + appInput.inputFileText + "'" + LS + "jobIdentifier: '" + appInput.jobIdentifier + "'" + LS + "jobPID: '" + appInput.jobPID + "'" + LS + "inputURL: '" + appInput.inputURL + "'" + LS + "inputFile: '" + appInput.inputFile + "'" + LS); }
From source file:it.infn.ct.picalc_portlet.java
/** * This method manages the user input fields managing two cases * distinguished by the type of the input <form ... statement * The use of upload file controls needs the use of "multipart/form-data" * while the else condition of the isMultipartContent check manages the * standard input case. The multipart content needs a manual processing of * all <form items//from www. j a va2s . c o m * All form' input items are identified by the 'name' input property * inside the jsp file * * @param request ActionRequest instance (processAction) * @param appInput AppInput instance storing the jobSubmission data */ void getInputForm(ActionRequest request, AppInput appInput) { if (PortletFileUpload.isMultipartContent(request)) try { FileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload upload = new PortletFileUpload(factory); List items = upload.parseRequest(request); File repositoryPath = new File("/tmp"); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); diskFileItemFactory.setRepository(repositoryPath); Iterator iter = items.iterator(); String logstring = ""; while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); // Prepare a log string with field list logstring += LS + "field name: '" + fieldName + "' - '" + item.getString() + "'"; switch (inputControlsIds.valueOf(fieldName)) { case JobIdentifier: appInput.jobIdentifier = item.getString(); break; case CpuNumber: appInput.cpuNumber = item.getString(); break; //case notifyEmail: // appInput.notifyEmail=item.getString(); //break; //case notifyStart: // if (item.getString().equalsIgnoreCase("on")) // appInput.notifyStart=true; //break; //case notifyStop: // if (item.getString().equalsIgnoreCase("on")) // appInput.notifyStop=true; //break; default: // Should not happen unless inputControlsIds enum is not updated _log.warn("Unhandled input field: '" + fieldName + "' - '" + item.getString() + "'"); } // switch fieldName } // while iter.hasNext() _log.info(LS + "Reporting" + LS + "---------" + LS + logstring + LS); } // try catch (Exception e) { _log.info("Caught exception while processing files to upload: '" + e.toString() + "'"); } // The input form do not use the "multipart/form-data" else { // Retrieve from the input form the given application values appInput.cpuNumber = (String) request.getParameter("CpuNumber"); appInput.jobIdentifier = (String) request.getParameter("JobIdentifier"); //appInput.notifyEmail = (String)request.getParameter("notifyEmail" ); //appInput.notifyStart =((String)request.getParameter("notifyStart" )).equalsIgnoreCase("on")?true:false; //appInput.notifyStop =((String)request.getParameter("notifyStop" )).equalsIgnoreCase("on")?true:false; } // ! isMultipartContent // Show into the log the taken inputs _log.info(LS + "Taken input parameters:" + LS + "-----------------------" + LS + "CpuNumber : '" + appInput.cpuNumber + "'" + LS + "JobIdentifier: '" + appInput.jobIdentifier + "'" //+LS+"notifyEmail : '"+appInput.notifyEmail +"'" //+LS+"notifyStart : '"+appInput.notifyStart +"'" //+LS+"notifyStop : '"+appInput.notifyStop +"'" + LS); }
From source file:it.infn.ct.code_rade_portlet.java
/** * This method manages the user input fields managing two cases * distinguished by the type of the input <form ... statement * The use of upload file controls needs the use of "multipart/form-data" * while the else condition of the isMultipartContent check manages the * standard input case. The multipart content needs a manual processing of * all <form items//from w w w .ja v a2 s. c o m * All form' input items are identified by the 'name' input property * inside the jsp file * * @param request ActionRequest instance (processAction) * @param appInput AppInput instance storing the jobSubmission data */ void getInputForm(ActionRequest request, AppInput appInput) { if (PortletFileUpload.isMultipartContent(request)) try { FileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload upload = new PortletFileUpload(factory); List items = upload.parseRequest(request); File repositoryPath = new File("/tmp"); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); diskFileItemFactory.setRepository(repositoryPath); Iterator iter = items.iterator(); String logstring = ""; while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); // Prepare a log string with field list logstring += LS + "field name: '" + fieldName + "' - '" + item.getString() + "'"; switch (inputControlsIds.valueOf(fieldName)) { case file_inputFile: appInput.inputFileName = item.getString(); processInputFile(item, appInput); break; case inputFile: appInput.inputFileText = item.getString(); break; case JobIdentifier: appInput.jobIdentifier = item.getString(); break; default: _log.warn("Unhandled input field: '" + fieldName + "' - '" + item.getString() + "'"); } // switch fieldName } // while iter.hasNext() _log.info(LS + "Reporting" + LS + "---------" + LS + logstring + LS); } // try catch (Exception e) { _log.info("Caught exception while processing files to upload: '" + e.toString() + "'"); } // The input form do not use the "multipart/form-data" else { // Retrieve from the input form the given application values appInput.inputFileName = (String) request.getParameter("file_inputFile"); appInput.inputFileText = (String) request.getParameter("inputFile"); appInput.jobIdentifier = (String) request.getParameter("JobIdentifier"); } // ! isMultipartContent // Show into the log the taken inputs _log.info(LS + "Taken input parameters:" + LS + "-----------------------" + LS + "inputFileName: '" + appInput.inputFileName + "'" + LS + "inputFileText: '" + appInput.inputFileText + "'" + LS + "jobIdentifier: '" + appInput.jobIdentifier + "'" + LS); }
From source file:it.infn.ct.R_portlet.java
void getInputForm(ActionRequest request, App_Input appInput) { if (PortletFileUpload.isMultipartContent(request)) try {//from w w w . j av a2 s .c o m FileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload upload = new PortletFileUpload(factory); List items = upload.parseRequest(request); File repositoryPath = new File("/tmp"); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); diskFileItemFactory.setRepository(repositoryPath); Iterator iter = items.iterator(); String logstring = ""; while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); // Prepare a log string with field list logstring += LS + "field name: '" + fieldName + "' - '" + item.getString() + "'"; switch (inputControlsIds.valueOf(fieldName)) { case file_inputFile: appInput.inputFileName = item.getString(); processInputFile(item, appInput); break; case inputFile: appInput.inputFileText = item.getString(); break; case JobIdentifier: appInput.jobIdentifier = item.getString(); break; default: _log.warn("Unhandled input field: '" + fieldName + "' - '" + item.getString() + "'"); } // switch fieldName } // while iter.hasNext() _log.info(LS + "Reporting" + LS + "---------" + LS + logstring + LS); } // try catch (Exception e) { _log.info("Caught exception while processing files to upload: '" + e.toString() + "'"); } // The input form do not use the "multipart/form-data" else { // Retrieve from the input form the given application values appInput.inputFileName = (String) request.getParameter("file_inputFile"); appInput.inputFileText = (String) request.getParameter("inputFile"); appInput.jobIdentifier = (String) request.getParameter("JobIdentifier"); } // ! isMultipartContent // Show into the log the taken inputs _log.info(LS + "Taken input parameters:" + LS + "-----------------------" + LS + "inputFileName: '" + appInput.inputFileName + "'" + LS + "inputFileText: '" + appInput.inputFileText + "'" + LS + "jobIdentifier: '" + appInput.jobIdentifier + "'" + LS); }
From source file:it.infn.ct.corsika_portlet.java
/** * This method manages the user input fields managing two cases * distinguished by the type of the input <form ... statement The use of * upload file controls needs the use of "multipart/form-data" while the * else condition of the isMultipartContent check manages the standard input * case. The multipart content needs a manual processing of all <form items * All form' input items are identified by the 'name' input property inside * the jsp file//from w w w .j ava 2 s .c o m * @param request ActionRequest instance (processAction) * @param appInput AppInput instance storing the jobSubmission data */ void getInputForm(ActionRequest request, corsika_portlet.AppInput appInput) { if (PortletFileUpload.isMultipartContent(request)) { try { FileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload upload = new PortletFileUpload(factory); List items = upload.parseRequest(request); File repositoryPath = new File("/tmp"); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); diskFileItemFactory.setRepository(repositoryPath); Iterator iter = items.iterator(); String logstring = ""; int i = 0; int j = 0; while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); // Prepare a log string with field list switch (corsika_portlet.inputControlsIds.valueOf(fieldName)) { case jobPID: logstring += LS + "FIELD IS A JOB PID WITH NAME : '" + item.getString() + "'"; appInput.jobPID = item.getString(); break; case JobIdentifier: logstring += LS + "FIELD IS A JOB IDENTIFIER WITH NAME : '" + item.getString() + "'"; appInput.jobIdentifier = item.getString(); break; case file_inputFile: if (fileName == "") break; logstring += LS + "FIELD IS A FILE WITH NAME : '" + fileName + "'"; appInput.inputFile = appInput.path + "/" + fileName; logstring += LS + "COPYING IT TO PATH : '" + appInput.path + "'"; copyFile(item, appInput.inputFile); break; case corsikaVersion: appInput.corsikaVersion = item.getString(); logstring += LS + "FIELD IS A CORSIKA VERSION WITH NAME : '" + appInput.corsikaVersion + "'"; break; case parametricJob: if (item.getString().trim().equals("yes")) appInput.parametricJob = true; else appInput.parametricJob = false; logstring += LS + "FIELD IS A PARAMETRIC JOB SPECIFICATION : '" + appInput.parametricJob.toString() + "'"; break; case emailWhenFinished: if (item.getString().trim().equals("yes")) appInput.emailWhenFinished = true; else appInput.emailWhenFinished = false; logstring += LS + "FIELD IS A EMAIL WHEN FINISHED SPECIFICATION : '" + appInput.emailWhenFinished.toString() + "'"; break; case useStorageElement: if (item.getString().trim().equals("yes")) appInput.useStorageElement = true; else appInput.useStorageElement = false; logstring += LS + "FIELD IS A STORAGE ELEMENT SPECIFICATION : '" + appInput.useStorageElement.toString() + "'"; break; default: _log.warn("Unhandled input field: '" + fieldName + "' - '" + item.getString() + "'"); } // switch fieldName } // while iter.hasNext() _log.info(LS + "Reporting" + LS + "---------" + LS + logstring + LS); } // try catch (Exception e) { _log.info("Caught exception while processing files to upload: '" + e.toString() + "'"); } } // The input form do not use the "multipart/form-data" else { // Retrieve from the input form the given application values // appInput.inputFileName = (String) request.getParameter("file_inputFile"); // appInput.inputFileText = (String) request.getParameter("inputFile"); appInput.jobIdentifier = (String) request.getParameter("JobIdentifier"); } // ! isMultipartContent // Show into the log the taken inputs _log.info(LS + "Taken input parameters:" + LS + "-----------------------" // + LS + "inputFileName: '" + appInput.inputFileName + "'" // + LS + "inputFileText: '" + appInput.inputFileText + "'" + LS + "jobIdentifier: '" + appInput.jobIdentifier + "'" + LS + "jobPID: '" + appInput.jobPID + "'" + LS + "inputFile: '" + appInput.inputFile + "'" + LS); }
From source file:com.xpn.xwiki.plugin.fileupload.FileUploadPlugin.java
/** * Loads the list of uploaded files in the context if there are any uploaded files. * /* w ww .j a v a 2 s .c o m*/ * @param uploadMaxSize Maximum size of the uploaded files. * @param uploadSizeThreashold Threashold over which the file data should be stored on disk, and not in memory. * @param tempdir Temporary directory to store the uploaded files that are not kept in memory. * @param context Context of the request. * @throws XWikiException if the request could not be parsed, or the maximum file size was reached. * @see FileUploadPluginApi#loadFileList(long, int, String) */ public void loadFileList(long uploadMaxSize, int uploadSizeThreashold, String tempdir, XWikiContext context) throws XWikiException { LOGGER.debug("Loading uploaded files"); // If we already have a file list then loadFileList was already called // Continuing would empty the list.. We need to stop. if (context.get(FILE_LIST_KEY) != null) { LOGGER.debug("Called loadFileList twice"); return; } // Get the FileUpload Data // Make sure the factory only ever creates file items which will be deleted when the jvm is stopped. DiskFileItemFactory factory = new DiskFileItemFactory() { public FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName) { try { final DiskFileItem item = (DiskFileItem) super.createItem(fieldName, contentType, isFormField, fileName); // Needed to make sure the File object is created. item.getOutputStream(); item.getStoreLocation().deleteOnExit(); return item; } catch (IOException e) { String path = System.getProperty("java.io.tmpdir"); if (super.getRepository() != null) { path = super.getRepository().getPath(); } throw new RuntimeException("Unable to create a temporary file for saving the attachment. " + "Do you have write access on " + path + "?"); } } }; factory.setSizeThreshold(uploadSizeThreashold); if (tempdir != null) { File tempdirFile = new File(tempdir); if (tempdirFile.mkdirs() && tempdirFile.canWrite()) { factory.setRepository(tempdirFile); } } // TODO: Does this work in portlet mode, or we must use PortletFileUpload? FileUpload fileupload = new ServletFileUpload(factory); RequestContext reqContext = new ServletRequestContext(context.getRequest().getHttpServletRequest()); fileupload.setSizeMax(uploadMaxSize); // context.put("fileupload", fileupload); try { @SuppressWarnings("unchecked") List<FileItem> list = fileupload.parseRequest(reqContext); if (list.size() > 0) { LOGGER.info("Loaded " + list.size() + " uploaded files"); } // We store the file list in the context context.put(FILE_LIST_KEY, list); } catch (FileUploadBase.SizeLimitExceededException e) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE, "Exception uploaded file"); } catch (Exception e) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_UPLOAD_PARSE_EXCEPTION, "Exception while parsing uploaded file", e); } }
From source file:com.ultrapower.eoms.common.plugin.ajaxupload.AjaxMultiPartRequest.java
/** * Creates a new request wrapper to handle multi-part data using methods * adapted from Jason Pell's multipart classes (see class description). * /*from w w w . j a v a 2 s.c o m*/ * @param saveDir * the directory to save off the file * @param servletRequest * the request containing the multipart * @throws java.io.IOException * is thrown if encoding fails. * @throws */ public void parse(HttpServletRequest servletRequest, String saveDir) throws IOException { Integer delay = 3; UploadListener listener = null; DiskFileItemFactory fac = null; // Parse the request try { if (maxSize >= 0L && servletRequest.getContentLength() > maxSize) { servletRequest.setAttribute("error", "size"); FileItemIterator it = new ServletFileUpload(fac).getItemIterator(servletRequest); // handle with each file: while (it.hasNext()) { FileItemStream item = it.next(); if (item.isFormField()) { List<String> values; if (params.get(item.getFieldName()) != null) { values = params.get(item.getFieldName()); } else { values = new ArrayList<String>(); } InputStream stream = item.openStream(); values.add(Streams.asString(stream)); params.put(item.getFieldName(), values); } } return; } else { listener = new UploadListener(servletRequest, delay); fac = new MonitoredDiskFileItemFactory(listener); } // Make sure that the data is written to file fac.setSizeThreshold(0); if (saveDir != null) { fac.setRepository(new File(saveDir)); } ServletFileUpload upload = new ServletFileUpload(fac); upload.setSizeMax(maxSize); List items = upload.parseRequest(createRequestContext(servletRequest)); for (Object item1 : items) { FileItem item = (FileItem) item1; if (log.isDebugEnabled()) log.debug("Found item " + item.getFieldName()); if (item.isFormField()) { log.debug("Item is a normal form field"); List<String> values; if (params.get(item.getFieldName()) != null) { values = params.get(item.getFieldName()); } else { values = new ArrayList<String>(); } String charset = servletRequest.getCharacterEncoding(); if (charset != null) { values.add(item.getString(charset)); } else { values.add(item.getString()); } params.put(item.getFieldName(), values); } else { log.debug("Item is a file upload"); // Skip file uploads that don't have a file name - meaning // that no file was selected. if (item.getName() == null || item.getName().trim().length() < 1) { log.debug("No file has been uploaded for the field: " + item.getFieldName()); continue; } String targetFileName = item.getName(); if (!targetFileName.contains(":")) item.write(new File(targetDirectory + targetFileName)); //?Action List<FileItem> values; if (files.get(item.getFieldName()) != null) { values = files.get(item.getFieldName()); } else { values = new ArrayList<FileItem>(); } values.add(item); files.put(item.getFieldName(), values); } } } catch (Exception e) { log.error(e); errors.add(e.getMessage()); } }
From source file:csiro.pidsvc.mappingstore.Manager.java
@SuppressWarnings("unchecked") protected String unwrapCompressedBackupFile(HttpServletRequest request, ICallback callback) { java.util.List<FileItem> fileList = null; GZIPInputStream gis = null;//from ww w .j a v a 2 s .co m String ret = null; try { DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); // Set the size threshold, above which content will be stored on disk. fileItemFactory.setSizeThreshold(1 * 1024 * 1024); // 1 MB // fileItemFactory.setSizeThreshold(100 * 1024); // 100 KB // Set the temporary directory to store the uploaded files of size above threshold. fileItemFactory.setRepository(new File(System.getProperty("java.io.tmpdir"))); ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory); fileList = uploadHandler.parseRequest(request); for (FileItem item : fileList) { if (item.isFormField()) continue; try { // Try to restore the backup file as it was in binary format. gis = new GZIPInputStream(item.getInputStream()); ret = callback.process(gis); gis.close(); } catch (IOException ex) { String msg = ex.getMessage(); if (msg != null && msg.equalsIgnoreCase("Not in GZIP format")) { // Try to restore the backup file as it was unzipped. ret = callback.process(item.getInputStream()); } else throw ex; } // Process the first uploaded file only. return ret; } } catch (Exception ex) { String msg = ex.getMessage(); Throwable linkedException = ex.getCause(); _logger.warn(msg); if (linkedException != null) _logger.warn(linkedException.getMessage()); if (msg != null && msg.equalsIgnoreCase("Not in GZIP format")) return "ERROR: Unknown file format."; else return "ERROR: " + (msg == null ? "Something went wrong." : msg + (linkedException == null ? "" : " " + linkedException.getMessage())); } finally { try { // Close the stream. gis.close(); } catch (Exception ex) { } if (fileList != null) { // Delete all uploaded files. for (FileItem item : fileList) { if (!item.isFormField() && !item.isInMemory()) ((DiskFileItem) item).delete(); } } } _logger.trace("No file found."); return "ERROR: No file."; }