List of usage examples for org.apache.commons.fileupload.disk DiskFileItem getStoreLocation
public File getStoreLocation()
FileItem
's data's temporary location on the disk. From source file:com.threecrickets.prudence.internal.lazy.LazyInitializationFile.java
/** * Creates a PHP-style item in the $_FILE map. * /*w w w .ja v a2 s. c o m*/ * @param fileItem * The file itme * @return A PHP-style $_FILE item */ private static Map<String, Object> createFileItemMap(FileItem fileItem) { Map<String, Object> exposedFileItem = new HashMap<String, Object>(); exposedFileItem.put("name", fileItem.getName()); exposedFileItem.put("type", fileItem.getContentType()); exposedFileItem.put("size", fileItem.getSize()); if (fileItem instanceof DiskFileItem) { DiskFileItem diskFileItem = (DiskFileItem) fileItem; exposedFileItem.put("tmp_name", diskFileItem.getStoreLocation().getAbsolutePath()); } // exposedFileItem.put("error", ); return exposedFileItem; }
From source file:edu.isi.karma.util.FileUtil.java
static public File downloadFileFromHTTPRequest(HttpServletRequest request, String destinationDirString) { // Download the file to the upload file folder File destinationDir = new File(destinationDirString); logger.debug("File upload destination directory: " + destinationDir.getAbsolutePath()); DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); // Set the size threshold, above which content will be stored on disk. fileItemFactory.setSizeThreshold(1 * 1024 * 1024); //1 MB //Set the temporary directory to store the uploaded files of size above threshold. fileItemFactory.setRepository(destinationDir); ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory); File uploadedFile = null;// www .ja v a 2 s . c om try { // Parse the request @SuppressWarnings("rawtypes") List items = uploadHandler.parseRequest(request); @SuppressWarnings("rawtypes") Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); // Ignore Form Fields. if (item.isFormField()) { // Do nothing } else { //Handle Uploaded files. Write file to the ultimate location. uploadedFile = new File(destinationDir, item.getName()); if (item instanceof DiskFileItem) { DiskFileItem t = (DiskFileItem) item; if (!t.getStoreLocation().renameTo(uploadedFile)) item.write(uploadedFile); } else item.write(uploadedFile); } } } catch (FileUploadException ex) { logger.error("Error encountered while parsing the request", ex); } catch (Exception ex) { logger.error("Error encountered while uploading file", ex); } return uploadedFile; }
From source file:httpUtils.HttpUtils.java
/** * Parse the httpServletRequest for the file item that was received within a * multipart/form-data POST request//from w w w. jav a 2s . c o m * @param httpServletRequest * @return the file item */ public static DiskFileItem parseRequestForFileItem(HttpServletRequest httpServletRequest) { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request for file List<FileItem> items = null; try { items = upload.parseRequest(httpServletRequest); } catch (FileUploadException fue) { log.error("Error reading/parsing file", fue); } if (items.size() != 1) { log.debug("there should only be one file here."); for (FileItem x : items) { log.debug("item: " + x.toString()); } } DiskFileItem dfi = (DiskFileItem) items.get(0); log.debug("parseRequestForFile - store location: `" + dfi.getStoreLocation() + "'"); log.debug("dfi content type: " + dfi.getContentType()); return dfi; }
From source file:com.bruce.gogo.utils.JakartaMultiPartRequest.java
public File[] getFile(String fieldName) { List items = (List) files.get(fieldName); if (items == null) { return null; }/*from ww w .j a va2 s. c o m*/ List<File> fileList = new ArrayList<File>(items.size()); for (int i = 0; i < items.size(); i++) { DiskFileItem fileItem = (DiskFileItem) items.get(i); fileList.add(fileItem.getStoreLocation()); } return (File[]) fileList.toArray(new File[fileList.size()]); }
From source file:com.bruce.gogo.utils.JakartaMultiPartRequest.java
public String[] getFilesystemName(String fieldName) { List items = (List) files.get(fieldName); if (items == null) { return null; }//from w ww .java 2 s . co m List<String> fileNames = new ArrayList<String>(items.size()); for (int i = 0; i < items.size(); i++) { DiskFileItem fileItem = (DiskFileItem) items.get(i); fileNames.add(fileItem.getStoreLocation().getName()); } return (String[]) fileNames.toArray(new String[fileNames.size()]); }
From source file:com.linwoodhomes.internal.DefaultTempFile.java
@Override public final FileItem getTempFile() { // create the new temp disk file. DiskFileItem tempFile = (DiskFileItem) fileFactory.createItem(null, null, false, null); try {//from ww w .java 2 s. c o m tempFile.getOutputStream(); // track the temp file. File storeLocation = tempFile.getStoreLocation(); if (storeLocation != null) { // track the temporary file, based on the DiskFileItem itself as // the marker. Oonce the marker is garbage collected it will be // cleaned up (deleted) by the cleaningTracker's reaper thread. cleaningTracker.track(storeLocation, tempFile); if (log.isTraceEnabled()) { log.trace("FileCleaningTracker added file: " + storeLocation + " marked by: " + tempFile); } } else { log.debug("storeLocation was null, not tracking temp file: " + tempFile); } numTempFilesCount++; } catch (IOException ioe) { log.error("Could not create temp file, is there write access to: " + fileFactory.getRepository()); } return tempFile; }
From source file:com.ultrapower.eoms.common.plugin.ajaxupload.AjaxMultiPartRequest.java
public File[] getFile(String fieldName) { List items = files.get(fieldName); if (items == null) { return null; }/* w w w .j av a 2s. c o m*/ List<File> fileList = new ArrayList<File>(items.size()); for (int i = 0; i < items.size(); i++) { DiskFileItem fileItem = (DiskFileItem) items.get(i); fileList.add(fileItem.getStoreLocation()); } return fileList.toArray(new File[fileList.size()]); }
From source file:com.ultrapower.eoms.common.plugin.ajaxupload.AjaxMultiPartRequest.java
public String[] getFilesystemName(String fieldName) { List items = files.get(fieldName); if (items == null) { return null; }/*from w w w. j a v a 2s.c om*/ List<String> fileNames = new ArrayList<String>(items.size()); for (int i = 0; i < items.size(); i++) { DiskFileItem fileItem = (DiskFileItem) items.get(i); fileNames.add(fileItem.getStoreLocation().getName()); } return fileNames.toArray(new String[fileNames.size()]); }
From source file:com.xpn.xwiki.doc.XWikiAttachmentContent.java
/** * Set a new FileItem for storage./* w w w .ja v a2 s . com*/ * * @since 2.6M1 */ private void newFileItem() { String tempFileLocation = System.getProperty("java.io.tmpdir"); // TODO try to get a different temp file location. try { final DiskFileItem dfi = new DiskFileItem(null, null, false, null, 10000, new File(tempFileLocation)); // This causes the temp file to be created. dfi.getOutputStream(); // Make sure this file is marked for deletion on VM exit because DiskFileItem does not. dfi.getStoreLocation().deleteOnExit(); this.file = dfi; } catch (IOException e) { throw new RuntimeException("Failed to create new attachment temporary file." + " Are you sure you have permission to write to " + tempFileLocation + "?", e); } }
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. * /*from w ww.j ava 2s .co 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); } }