public DiskFileItem getObject ( String command ) throws Exception { String[] parts = command.split(";"); if ( parts.length == 3 && "copyAndDelete".equals(parts[ 0 ]) ) { return copyAndDelete(parts[ 1 ], parts[ 2 ]); } else if ( parts.length == 3 && "write".equals(parts[ 0 ]) ) { return write(parts[ 1 ], parts[ 2 ].getBytes("US-ASCII")); } else if ( parts.length == 3 && "writeB64".equals(parts[ 0 ]) ) { return write(parts[ 1 ], Base64.decodeBase64(parts[ 2 ])); } else if ( parts.length == 3 && "writeOld".equals(parts[ 0 ]) ) { return writePre131(parts[ 1 ], parts[ 2 ].getBytes("US-ASCII")); } else if ( parts.length == 3 && "writeOldB64".equals(parts[ 0 ]) ) { return writePre131(parts[ 1 ], Base64.decodeBase64(parts[ 2 ])); } else { throw new IllegalArgumentException("Unsupported command " + command + " " + Arrays.toString(parts)); } }
public DiskFileItem getObject ( CmdExecuteHelper cmdHelper ) throws Exception { String[] parts = cmdHelper.getCommand().split(";"); if ( parts.length == 3 && "copyAndDelete".equals(parts[ 0 ]) ) { return copyAndDelete(parts[ 1 ], parts[ 2 ]); } else if ( parts.length == 3 && "write".equals(parts[ 0 ]) ) { return write(parts[ 1 ], parts[ 2 ].getBytes("US-ASCII")); } else if ( parts.length == 3 && "writeB64".equals(parts[ 0 ]) ) { return write(parts[ 1 ], Base64.decodeBase64(parts[ 2 ])); } else if ( parts.length == 3 && "writeOld".equals(parts[ 0 ]) ) { return writePre131(parts[ 1 ], parts[ 2 ].getBytes("US-ASCII")); } else if ( parts.length == 3 && "writeOldB64".equals(parts[ 0 ]) ) { return writePre131(parts[ 1 ], Base64.decodeBase64(parts[ 2 ])); } else { throw new IllegalArgumentException("Unsupported command " + cmdHelper.getCommand() + " " + Arrays.toString(parts)); } }
public File[] getFile(String fieldName) { List<FileItem> items = files.get(fieldName); if (items == null) { return null; } List<File> fileList = new ArrayList<>(items.size()); for (FileItem fileItem : items) { File storeLocation = ((DiskFileItem) fileItem).getStoreLocation(); if (fileItem.isInMemory() && storeLocation != null && !storeLocation.exists()) { try { storeLocation.createNewFile(); } catch (IOException e) { LOG.error("Cannot write uploaded empty file to disk: {}", storeLocation.getAbsolutePath(), e); } } fileList.add(storeLocation); } return fileList.toArray(new File[fileList.size()]); }
public static PopupForm fromRequest(String uuid, HttpServletRequest request) { String descriptor = request.getParameter("descriptor"); boolean isOpenCampaign = "open-campaign".equals(request.getParameter("open-campaign")); long startTime = "".equals(request.getParameter("start_time")) ? 0 : parseTime(request.getParameter("start_time_selected_datetime")); long endTime = "".equals(request.getParameter("end_time")) ? 0 : parseTime(request.getParameter("end_time_selected_datetime")); List<String> assignToEids = new ArrayList<String>(); if (request.getParameter("distribution") != null) { for (String user : request.getParameter("distribution").split("[\r\n]+")) { if (!user.isEmpty()) { assignToEids.add(user); } } } Optional<DiskFileItem> templateItem = Optional.empty(); DiskFileItem dfi = (DiskFileItem) request.getAttribute("template"); if (dfi != null && dfi.getSize() > 0) { templateItem = Optional.of(dfi); } return new PopupForm(uuid, descriptor, startTime, endTime, isOpenCampaign, assignToEids, templateItem); }
private String getTextParameter(DiskFileItem diskFileItem, String characterEncoding) throws Exception { String encoding = diskFileItem.getCharSet(); if (encoding == null) { encoding = characterEncoding; } String textValue; if (encoding == null) { textValue = new String(diskFileItem.get()); } else { textValue = new String(diskFileItem.get(), encoding); } return textValue; }
@Override public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context) { try { final List<FileItem> files = RequestFileHandlingUtilities .handleFileUpload(request); final HashMap<String, String> fileMap = new HashMap<String, String>(); for (final FileItem fileItem : files) { fileMap.put(fileItem.getName(), Files .getCanonicalPath(((DiskFileItem) fileItem) .getStoreLocation())); } response.setEntity(new SerializableEntity(fileMap, false)); fileItems.addAll(files); } catch (Exception e) { response.setEntity(new StringEntity("ERROR", ContentType.DEFAULT_TEXT)); } }
/** * Determine whether the multipart content is still available. If a temporary * file has been moved, the content is no longer available. */ protected boolean isAvailable() { // If in memory, it's available. if (this.fileItem.isInMemory()) { return true; } // Check actual existence of temporary file. if (this.fileItem instanceof DiskFileItem) { return ( (DiskFileItem) this.fileItem ).getStoreLocation().exists(); } // Check whether current file size is different than original one. return ( this.fileItem.getSize() == this.size ); }
public static String urlForFileItem(FileItem fileItem) throws SAXException { // Only a reference to the file is output (xs:anyURI) final DiskFileItem diskFileItem = (DiskFileItem) fileItem; final String uriExpiringWithRequest; if (!fileItem.isInMemory()) { // File must exist on disk since isInMemory() returns false final File file = diskFileItem.getStoreLocation(); uriExpiringWithRequest = file.toURI().toString(); } else { // File does not exist on disk, must convert // NOTE: Conversion occurs every time this method is called. Not optimal. try { uriExpiringWithRequest = NetUtils.inputStreamToAnyURI(fileItem.getInputStream(), NetUtils.REQUEST_SCOPE); } catch (IOException e) { throw new OXFException(e); } } return uriExpiringWithRequest; }
private static DiskFileItem makePayload ( int thresh, String repoPath, String filePath, byte[] data ) throws IOException, Exception { // if thresh < written length, delete outputFile after copying to repository temp file // otherwise write the contents to repository temp file File repository = new File(repoPath); DiskFileItem diskFileItem = new DiskFileItem("test", "application/octet-stream", false, "test", 100000, repository); File outputFile = new File(filePath); DeferredFileOutputStream dfos = new DeferredFileOutputStream(thresh, outputFile); OutputStream os = (OutputStream) Reflections.getFieldValue(dfos, "memoryOutputStream"); os.write(data); Reflections.getField(ThresholdingOutputStream.class, "written").set(dfos, data.length); Reflections.setFieldValue(diskFileItem, "dfos", dfos); Reflections.setFieldValue(diskFileItem, "sizeThreshold", 0); return diskFileItem; }
/** * Determine whether the multipart content is still available. * If a temporary file has been moved, the content is no longer available. */ protected boolean isAvailable() { // If in memory, it's available. if (this.fileItem.isInMemory()) { return true; } // Check actual existence of temporary file. if (this.fileItem instanceof DiskFileItem) { return ((DiskFileItem) this.fileItem).getStoreLocation().exists(); } // Check whether current file size is different than original one. return (this.fileItem.getSize() == this.size); }
/** * Return a description for the storage location of the multipart content. * Tries to be as specific as possible: mentions the file location in case * of a temporary file. */ public String getStorageDescription() { if (this.fileItem.isInMemory()) { return "in memory"; } else if (this.fileItem instanceof DiskFileItem) { return "at [" + ((DiskFileItem) this.fileItem).getStoreLocation().getAbsolutePath() + "]"; } else { return "on disk"; } }
public static String getTextParameter(DiskFileItem diskFileItem, String characterEncoding) throws Exception { String encoding = diskFileItem.getCharSet(); if (encoding == null) { encoding = characterEncoding; } String textValue; if (encoding == null) { textValue = new String(diskFileItem.get()); } else { textValue = new String(diskFileItem.get(), encoding); } return textValue; }
public String[] getFilesystemName(String fieldName) { List<FileItem> items = files.get(fieldName); if (items == null) { return null; } List<String> fileNames = new ArrayList<>(items.size()); for (FileItem fileItem : items) { fileNames.add(((DiskFileItem) fileItem).getStoreLocation().getName()); } return fileNames.toArray(new String[fileNames.size()]); }
/** * Creates a PHP-style item in the $_FILE map. * * @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; }
/** * Determine whether the multipart content is still available. * If a temporary file has been moved, the content is no longer available. * * @return true, if the multipart content is still available */ private boolean isAvailable() { // If in memory, it's available. if (this.fileItem.isInMemory()) { return true; } // Check actual existence of temporary file. if (this.fileItem instanceof DiskFileItem) { return ((DiskFileItem)this.fileItem).getStoreLocation().exists(); } // Check whether current file size is different than original one. return (this.fileItem.getSize() == this.fileSize); }
/** * Return a description for the storage location of the multipart content. * Tries to be as specific as possible: mentions the file location in case * of a temporary file. * * @return a description for the storage location of the multipart content */ public String getStorageDescription() { if (this.fileItem.isInMemory()) { return "in memory"; } else if (this.fileItem instanceof DiskFileItem) { return "at [" + ((DiskFileItem)this.fileItem).getStoreLocation().getAbsolutePath() + "]"; } else { return "on disk"; } }
public static File multipartFile2File(MultipartFile file) { CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) file; DiskFileItem fileItem = (DiskFileItem) commonsMultipartFile.getFileItem(); File _file = fileItem.getStoreLocation(); return _file; }
private PopupForm(String uuid, String descriptor, long startTime, long endTime, boolean isOpenCampaign, List<String> assignToEids, Optional<DiskFileItem> templateItem) { this.uuid = uuid; this.descriptor = descriptor; this.startTime = startTime; this.endTime = endTime; this.isOpenCampaign = isOpenCampaign; this.assignToEids = assignToEids; this.templateItem = templateItem; }
private Iterator<DiskFileItem> getItems() throws FileUploadException { if (list == null) { ServletFileUpload upload = new ServletFileUpload(); upload.setFileItemFactory(new DiskFileItemFactory()); upload.setSizeMax(100 * EFile.TP_KILO); // Processa os itens do upload list = upload.parseRequest(request); } return list.iterator(); }
private MultipleEntryHashMap getParameterMap(RequestContext request, String charSetEncoding) throws FileUploadException { MultipleEntryHashMap parameterMap = new MultipleEntryHashMap(); List items = parseRequest(request); Iterator iter = items.iterator(); while (iter.hasNext()) { DiskFileItem diskFileItem = (DiskFileItem)iter.next(); boolean isFormField = diskFileItem.isFormField(); Object value; try { if (isFormField) { value = getTextParameter(diskFileItem, charSetEncoding); } else { value = getFileParameter(diskFileItem); } } catch (Exception ex) { throw new FileUploadException(ex.getMessage()); } parameterMap.put(diskFileItem.getFieldName(), value); } return parameterMap; }
private DataHandler getFileParameter(DiskFileItem diskFileItem) throws Exception { DataSource dataSource = new DiskFileDataSource(diskFileItem); DataHandler dataHandler = new DataHandler(dataSource); return dataHandler; }
/** * Returns an identifier that is unique within the class loader used to * load this class, but does not have random-like apearance. * * @return A String with the non-random looking instance identifier. */ private static String getUniqueId() { int current; synchronized (DiskFileItem.class) { current = counter++; } String id = Integer.toString(current); // If you manage to get more than 100 million of ids, you'll // start getting ids longer than 8 characters. if (current < 100000000) { id = ("00000000" + id).substring(id.length()); } return id; }
@Override public Collection<String> getHeaderNames() { if (fileItem instanceof DiskFileItem) { final Set<String> headerNames = new LinkedHashSet<>(); final Iterator<String> iter = fileItem.getHeaders().getHeaderNames(); while (iter.hasNext()) { headerNames.add(iter.next()); } return headerNames; } return Collections.emptyList(); }