List of usage examples for org.apache.commons.fileupload.servlet ServletFileUpload getFileItemFactory
public FileItemFactory getFileItemFactory()
From source file:com.zimbra.cs.service.FileUploadServlet.java
public static Upload saveUpload(InputStream is, String filename, String contentType, String accountId, long limit) throws ServiceException, IOException { FileItem fi = null;// w ww.j av a 2 s . c o m boolean success = false; try { // store the fetched file as a normal upload ServletFileUpload upload = getUploader(limit); long sizeMax = upload.getSizeMax(); fi = upload.getFileItemFactory().createItem("upload", contentType, false, filename); // sizeMax=-1 means "no limit" long size = ByteUtil.copy(is, true, fi.getOutputStream(), true, sizeMax < 0 ? sizeMax : sizeMax + 1); if (upload.getSizeMax() >= 0 && size > upload.getSizeMax()) { mLog.info("Exceeded maximum upload size of " + upload.getSizeMax() + " bytes"); throw MailServiceException.UPLOAD_TOO_LARGE(filename, "upload too large"); } Upload up = new Upload(accountId, fi); mLog.info("saveUpload(): received %s", up); synchronized (mPending) { mPending.put(up.uuid, up); } success = true; return up; } finally { if (!success && fi != null) { mLog.debug("saveUpload(): unsuccessful attempt. Deleting %s", fi); fi.delete(); } } }
From source file:com.zimbra.cs.service.FileUploadServlet.java
/** * This is used when handling a POST request generated by {@link ZMailbox#uploadContentAsStream} * * @param req//from ww w. ja v a2 s . c om * @param resp * @param fmt * @param acct * @param limitByFileUploadMaxSize * @return * @throws IOException * @throws ServiceException */ List<Upload> handlePlainUpload(HttpServletRequest req, HttpServletResponse resp, String fmt, Account acct, boolean limitByFileUploadMaxSize) throws IOException, ServiceException { // metadata is encoded in the response's HTTP headers ContentType ctype = new ContentType(req.getContentType()); String contentType = ctype.getContentType(), filename = ctype.getParameter("name"); if (filename == null) { filename = new ContentDisposition(req.getHeader("Content-Disposition")).getParameter("filename"); } if (filename == null || filename.trim().equals("")) { mLog.info("Rejecting upload with no name."); drainRequestStream(req); sendResponse(resp, HttpServletResponse.SC_NO_CONTENT, fmt, null, null, null); return Collections.emptyList(); } // Unescape the filename so it actually displays correctly filename = StringEscapeUtils.unescapeHtml(filename); // store the fetched file as a normal upload ServletFileUpload upload = getUploader2(limitByFileUploadMaxSize); FileItem fi = upload.getFileItemFactory().createItem("upload", contentType, false, filename); try { // write the upload to disk, but make sure not to exceed the permitted max upload size long size = ByteUtil.copy(req.getInputStream(), false, fi.getOutputStream(), true, upload.getSizeMax() * 3); if ((upload.getSizeMax() >= 0 /* -1 would mean "no limit" */) && (size > upload.getSizeMax())) { mLog.debug("handlePlainUpload(): deleting %s", fi); fi.delete(); mLog.info("Exceeded maximum upload size of " + upload.getSizeMax() + " bytes: " + acct.getId()); drainRequestStream(req); sendResponse(resp, HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, fmt, null, null, null); return Collections.emptyList(); } } catch (IOException ioe) { mLog.warn("Unable to store upload. Deleting %s", fi, ioe); fi.delete(); drainRequestStream(req); sendResponse(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, fmt, null, null, null); return Collections.emptyList(); } List<FileItem> items = new ArrayList<FileItem>(1); items.add(fi); Upload up = new Upload(acct.getId(), fi, filename); mLog.info("Received plain: %s", up); synchronized (mPending) { mPending.put(up.uuid, up); } List<Upload> uploads = Arrays.asList(up); sendResponse(resp, HttpServletResponse.SC_OK, fmt, null, uploads, items); return uploads; }
From source file:ubic.gemma.web.util.upload.CommonsMultipartMonitoredResolver.java
@Override public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException { String enc = determineEncoding(request); ServletFileUpload fileUpload = this.newFileUpload(request); DiskFileItemFactory newFactory = (DiskFileItemFactory) fileUpload.getFileItemFactory(); fileUpload.setSizeMax(sizeMax);//from ww w. ja v a 2 s. co m newFactory.setRepository(this.uploadTempDir); fileUpload.setHeaderEncoding(enc); try { MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<>(); Map<String, String[]> multipartParams = new HashMap<>(); // Extract multipart files and multipart parameters. List<?> fileItems = fileUpload.parseRequest(request); for (Object fileItem1 : fileItems) { FileItem fileItem = (FileItem) fileItem1; if (fileItem.isFormField()) { String value; String fieldName = fileItem.getFieldName(); try { value = fileItem.getString(enc); } catch (UnsupportedEncodingException ex) { logger.warn("Could not decode multipart item '" + fieldName + "' with encoding '" + enc + "': using platform default"); value = fileItem.getString(); } String[] curParam = multipartParams.get(fieldName); if (curParam == null) { // simple form field multipartParams.put(fieldName, new String[] { value }); } else { // array of simple form fields String[] newParam = StringUtils.addStringToArray(curParam, value); multipartParams.put(fieldName, newParam); } } else { // multipart file field MultipartFile file = new CommonsMultipartFile(fileItem); multipartFiles.set(file.getName(), file); if (logger.isDebugEnabled()) { logger.debug("Found multipart file [" + file.getName() + "] of size " + file.getSize() + " bytes with original filename [" + file.getOriginalFilename() + "]"); } } } return new DefaultMultipartHttpServletRequest(request, multipartFiles, multipartParams, null); } catch (FileUploadException ex) { return new FailedMultipartHttpServletRequest(request, ex.getMessage()); } }