List of usage examples for org.apache.commons.fileupload FileItemStream getName
String getName();
From source file:org.tangram.components.servlet.ServletRequestParameterAccess.java
/** * Weak visibility to avoid direct instanciation. *//* w w w . j a v a 2 s . com*/ @SuppressWarnings("unchecked") ServletRequestParameterAccess(HttpServletRequest request, long uploadFileMaxSize) { final String reqContentType = request.getContentType(); LOG.debug("() uploadFileMaxSize={} request.contentType={}", uploadFileMaxSize, reqContentType); if (StringUtils.isNotBlank(reqContentType) && reqContentType.startsWith("multipart/form-data")) { ServletFileUpload upload = new ServletFileUpload(); upload.setFileSizeMax(uploadFileMaxSize); try { for (FileItemIterator itemIterator = upload.getItemIterator(request); itemIterator.hasNext();) { FileItemStream item = itemIterator.next(); String fieldName = item.getFieldName(); InputStream stream = item.openStream(); if (item.isFormField()) { String[] value = parameterMap.get(item.getFieldName()); int i = 0; if (value == null) { value = new String[1]; } else { String[] newValue = new String[value.length + 1]; System.arraycopy(value, 0, newValue, 0, value.length); i = value.length; value = newValue; } // if value[i] = Streams.asString(stream, "UTF-8"); LOG.debug("() request parameter {}='{}'", fieldName, value[0]); parameterMap.put(item.getFieldName(), value); } else { try { LOG.debug("() item {} :{}", item.getName(), item.getContentType()); final byte[] bytes = IOUtils.toByteArray(stream); if (bytes.length > 0) { originalNames.put(fieldName, item.getName()); blobs.put(fieldName, bytes); } // if } catch (IOException ex) { LOG.error("()", ex); if (ex.getCause() instanceof FileUploadBase.FileSizeLimitExceededException) { throw new RuntimeException(ex.getCause().getMessage()); // NOPMD we want to lose parts of our stack trace! } // if } // try/catch } // if } // for } catch (FileUploadException | IOException ex) { LOG.error("()", ex); } // try/catch } else { parameterMap = request.getParameterMap(); } // if }
From source file:org.ut.biolab.medsavant.MedSavantServlet.java
private Upload[] handleUploads(FileItemIterator iter) throws FileUploadException, IOException, InterruptedException { List<Upload> uploads = new ArrayList<Upload>(); FileItemStream streamToUpload = null; long filesize = -1; String sn = null;// w w w. j a va 2 s . com String fn = null; while (iter.hasNext()) { FileItemStream item = iter.next(); String name = item.getFieldName(); InputStream stream = item.openStream(); // System.out.println("Got file " + name); if (item.isFormField()) { if (name.startsWith("size_")) { sn = name.split("_")[1]; filesize = Long.parseLong(Streams.asString(stream)); } } else if (name.startsWith("file_")) { streamToUpload = item; } else { throw new IllegalArgumentException("Unrecognized file detected with field name " + name); } if (streamToUpload != null) { // Do the upload int streamId = copyStreamToServer(streamToUpload.openStream(), streamToUpload.getName(), (sn != null && fn != null && sn.equals(fn)) ? filesize : -1); if (streamId >= 0) { uploads.add(new Upload(name, streamId)); } } } return uploads.toArray(new Upload[uploads.size()]); }
From source file:org.vosao.servlet.FileUploadServlet.java
/** * //from www .j a v a 2 s . c o m * Process uploaded file. * * @param request * - Http request. * @param item * - multipart item. * @return Status string. */ private FileEntity processResourceFile(FileItemStream imageItem, byte[] data, FolderEntity folder) throws UploadException { String path = imageItem.getName(); String filename = FilenameUtils.getName(path); String cacheUrl = getBusiness().getFolderBusiness().getFolderPath(folder) + "/" + filename; getBusiness().getSystemService().getFileCache().remove(cacheUrl); String ext = FilenameUtils.getExtension(path); logger.debug("path " + path + " filename " + filename + " ext " + ext); FileEntity file = getDao().getFileDao().getByName(folder.getId(), filename); if (file == null) { file = new FileEntity(filename, filename, folder.getId(), MimeType.getContentTypeByExt(ext), new Date(), data.length); logger.debug("created file " + file.getFilename()); } getDao().getFileDao().save(file, data); return file; }
From source file:org.vosao.servlet.FileUploadServlet.java
private String processImportFile(FileItemStream fileItem, byte[] data) throws UploadException { String ext = FolderUtil.getFileExt(fileItem.getName()); if (!ext.toLowerCase().equals("zip") && !ext.toLowerCase().equals("vz")) { throw new UploadException(Messages.get("wrong_file_extension.")); }//from w w w .j a v a2 s . c o m getSystemService().getCache().putBlob(fileItem.getName(), data); getMessageQueue().publish(new ImportMessage.Builder().setStart(1).setFilename(fileItem.getName()).create()); return createMessage("success", Messages.get("saved_for_import")); }
From source file:org.vosao.servlet.FileUploadServlet.java
private String processPluginFile(FileItemStream fileItem, byte[] data) throws UploadException { String ext = FolderUtil.getFileExt(fileItem.getName()); if (!ext.toLowerCase().equals("war")) { throw new UploadException(Messages.get("wrong_file_extension")); }//from w w w .j a v a 2 s. com try { getBusiness().getPluginBusiness().install(fileItem.getName(), data); } catch (Exception e) { e.printStackTrace(); throw new UploadException(e.getMessage()); } return createMessage("success", Messages.get("saved_for_import")); }
From source file:org.vosao.servlet.FileUploadServlet.java
private String processPicasaFile(FileItemStream fileItem, byte[] data, Map<String, String> parameters) throws UploadException { try {/*from w w w. j a va 2 s . c o m*/ String albumId = parameters.get("albumId"); if (StringUtils.isEmpty(albumId)) { throw new UploadException(Messages.get("album_not_found", albumId)); } getBusiness().getPicasaBusiness().upload(albumId, data, fileItem.getName()); return createMessage("success", Messages.get("photo_uploaded")); } catch (Exception e) { throw new UploadException(e.getMessage()); } }
From source file:org.vosao.utils.FileItem.java
public FileItem(FileItemStream item, byte[] aData) { super();//from ww w . jav a2s. co m data = aData; filename = FilenameUtils.getName(item.getName()); fieldName = item.getFieldName(); }
From source file:org.wahlzeit.servlets.MainServlet.java
/** * Searches for files in the request and puts them in the resulting map with the key "fileName". When a file is * found, you can access its path by searching for elements with the key "fileName". */// w w w . ja v a2 s . c om protected Map getMultiPartRequestArgs(HttpServletRequest request, UserSession us) throws IOException, ServletException { Map<String, String> result = new HashMap<String, String>(); result.putAll(request.getParameterMap()); try { ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iterator = upload.getItemIterator(request); while (iterator.hasNext()) { FileItemStream fileItemStream = iterator.next(); String filename = fileItemStream.getName(); if (!fileItemStream.isFormField()) { InputStream inputStream = fileItemStream.openStream(); Image image = getImage(inputStream); User user = (User) us.getClient(); user.setUploadedImage(image); result.put("fileName", filename); log.config( LogBuilder.createSystemMessage().addParameter("Uploaded image", filename).toString()); } else { String key = fileItemStream.getFieldName(); InputStream is = fileItemStream.openStream(); String value = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)); result.put(key, value); log.config(LogBuilder.createSystemMessage().addParameter("Key of uploaded parameter", key) .addParameter("value", value).toString()); } } } catch (Exception ex) { throw new ServletException(ex); } return result; }
From source file:org.waterforpeople.mapping.app.web.PhotoUpload.java
public void doPost(HttpServletRequest req, HttpServletResponse resp) { Properties props = System.getProperties(); String bucket = props.getProperty("s3bucket"); ServletFileUpload upload = new ServletFileUpload(); try {//from w ww . java 2 s . c o m FileItemIterator iterator = upload.getItemIterator(req); while (iterator.hasNext()) { FileItemStream item = iterator.next(); InputStream in = item.openStream(); ByteArrayOutputStream out = null; try { in = item.openStream(); out = new ByteArrayOutputStream(); byte[] buffer = new byte[8096]; int size; while ((size = in.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, size); } } catch (IOException e) { log.log(Level.SEVERE, "Could not rotate image", e); } S3Driver s3 = new S3Driver(); s3.uploadFile(bucket, "images/" + item.getName(), out.toByteArray()); } } catch (Exception e) { log.log(Level.SEVERE, "Could not save image", e); } }
From source file:password.pwm.http.PwmRequest.java
public Map<String, FileUploadItem> readFileUploads(final int maxFileSize, final int maxItems) throws IOException, ServletException, PwmUnrecoverableException { final Map<String, FileUploadItem> returnObj = new LinkedHashMap<>(); try {/* w w w. j a va 2 s . co m*/ if (ServletFileUpload.isMultipartContent(this.getHttpServletRequest())) { final ServletFileUpload upload = new ServletFileUpload(); final FileItemIterator iter = upload.getItemIterator(this.getHttpServletRequest()); while (iter.hasNext() && returnObj.size() < maxItems) { final FileItemStream item = iter.next(); final InputStream inputStream = item.openStream(); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final long length = IOUtils.copyLarge(inputStream, baos, 0, maxFileSize + 1); if (length > maxFileSize) { final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, "upload file size limit exceeded"); LOGGER.error(this, errorInformation); respondWithError(errorInformation); return Collections.emptyMap(); } final byte[] outputFile = baos.toByteArray(); final FileUploadItem fileUploadItem = new FileUploadItem(item.getName(), item.getContentType(), outputFile); returnObj.put(item.getFieldName(), fileUploadItem); } } } catch (Exception e) { LOGGER.error("error reading file upload: " + e.getMessage()); } return Collections.unmodifiableMap(returnObj); }