List of usage examples for org.apache.commons.fileupload FileUploadBase MULTIPART_FORM_DATA
String MULTIPART_FORM_DATA
To view the source code for org.apache.commons.fileupload FileUploadBase MULTIPART_FORM_DATA.
Click Source Link
From source file:com.github.davidcarboni.encryptedfileupload.HttpServletRequestFactory.java
static public HttpServletRequest createValidHttpServletRequest(final String[] strFileNames) { // todo - provide a real implementation StringBuilder sbRequestData = new StringBuilder(); for (String strFileName : strFileNames) { sbRequestData.append(strFileName); }/*from w w w . j a va 2 s .c o m*/ byte[] requestData = null; requestData = sbRequestData.toString().getBytes(); return new MockHttpServletRequest(requestData, FileUploadBase.MULTIPART_FORM_DATA); }
From source file:com.github.davidcarboni.encryptedfileupload.HttpServletRequestFactory.java
static public HttpServletRequest createInvalidHttpServletRequest() { byte[] requestData = "foobar".getBytes(); return new MockHttpServletRequest(requestData, FileUploadBase.MULTIPART_FORM_DATA); }
From source file:com.silverpeas.util.web.servlet.FileUploadUtilTest.java
@Test public void testIsRequestMultipart() { MockHttpServletRequest request = new MockHttpServletRequest(); assertThat(FileUploadUtil.isRequestMultipart(request), is(false)); request.setContentType(FileUploadBase.MULTIPART_FORM_DATA); assertFalse(FileUploadUtil.isRequestMultipart(request)); request.setContentType(null);/*from ww w .j a va 2s .c o m*/ request.setMethod("POST"); assertFalse(FileUploadUtil.isRequestMultipart(request)); request.setContentType("text/html"); assertFalse(FileUploadUtil.isRequestMultipart(request)); request.setContentType(FileUploadBase.MULTIPART_FORM_DATA); assertTrue(FileUploadUtil.isRequestMultipart(request)); request.setContentType(FileUploadBase.MULTIPART_MIXED); assertTrue(FileUploadUtil.isRequestMultipart(request)); request.setContentType(FileUploadBase.MULTIPART); assertTrue(FileUploadUtil.isRequestMultipart(request)); }
From source file:com.silverpeas.util.web.servlet.FileUploadUtilTest.java
@Test public void testGetFile() throws Exception { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); request.setMethod("POST"); request.setContentType(FileUploadBase.MULTIPART_FORM_DATA); request.addParameter("champ1", "valeur1"); byte[] content = IOUtils .toByteArray(this.getClass().getClassLoader().getResourceAsStream("FrenchScrum.odp")); assertNotNull(content);//w ww . j a v a2 s. co m request.addFile(new MockMultipartFile("FrenchScrum.odp", content)); assertNotNull(content); }
From source file:org.apache.click.servlet.MockRequest.java
/** * If useMultiPartContentType set as true return the correct content-type. * * @return The correct multipart content-type if useMultiPartContentType * is true. Else null./*from w w w . ja va2 s . co m*/ */ public String getContentType() { if (useMultiPartContentType) { return FileUploadBase.MULTIPART_FORM_DATA + "; boundary=abcdefgABCDEFG"; } return null; }
From source file:org.apache.myfaces.webapp.filter.portlet.PortletChacheFileSizeErrorsFileUpload.java
/** * Similar to {@link ServletFileUpload#parseRequest(RequestContext)} but will * catch and swallow FileSizeLimitExceededExceptions in order to return as * many usable items as possible./*from ww w . jav a 2 s .c om*/ * * @param fileUpload * @return List of {@link FileItem} excluding any that exceed max size. * @throws FileUploadException */ public List parseRequestCatchingFileSizeErrors(ActionRequest request, FileUpload fileUpload) throws FileUploadException { try { List items = new ArrayList(); // The line below throws a SizeLimitExceededException (wrapped by a // FileUploadIOException) if the request is longer than the max size // allowed by fileupload requests (FileUpload.getSizeMax) // But note that if the request does not send proper headers this check // just will not do anything and we still have to check it again. FileItemIterator iter = fileUpload.getItemIterator(new PortletRequestContext(request)); FileItemFactory fac = fileUpload.getFileItemFactory(); if (fac == null) { throw new NullPointerException("No FileItemFactory has been set."); } long maxFileSize = this.getFileSizeMax(); long maxSize = this.getSizeMax(); boolean checkMaxSize = false; if (maxFileSize == -1L) { //The max allowed file size should be approximate to the maxSize maxFileSize = maxSize; } if (maxSize != -1L) { checkMaxSize = true; } while (iter.hasNext()) { final FileItemStream item = iter.next(); FileItem fileItem = fac.createItem(item.getFieldName(), item.getContentType(), item.isFormField(), item.getName()); long allowedLimit = 0L; try { if (maxFileSize != -1L || checkMaxSize) { if (checkMaxSize) { allowedLimit = maxSize > maxFileSize ? maxFileSize : maxSize; } else { //Just put the limit allowedLimit = maxFileSize; } long contentLength = getContentLength(item.getHeaders()); //If we have a content length in the header we can use it if (contentLength != -1L && contentLength > allowedLimit) { throw new FileUploadIOException(new FileSizeLimitExceededException( "The field " + item.getFieldName() + " exceeds its maximum permitted " + " size of " + allowedLimit + " characters.", contentLength, allowedLimit)); } //Otherwise we must limit the input as it arrives (NOTE: we cannot rely //on commons upload to throw this exception as it will close the //underlying stream final InputStream itemInputStream = item.openStream(); InputStream limitedInputStream = new LimitedInputStream(itemInputStream, allowedLimit) { protected void raiseError(long pSizeMax, long pCount) throws IOException { throw new FileUploadIOException(new FileSizeLimitExceededException( "The field " + item.getFieldName() + " exceeds its maximum permitted " + " size of " + pSizeMax + " characters.", pCount, pSizeMax)); } }; //Copy from the limited stream long bytesCopied = Streams.copy(limitedInputStream, fileItem.getOutputStream(), true); // Decrement the bytesCopied values from maxSize, so the next file copied // takes into account this value when allowedLimit var is calculated // Note the invariant before the line is maxSize >= bytesCopied, since if this // is not true a FileUploadIOException is thrown first. maxSize -= bytesCopied; } else { //We can just copy the data Streams.copy(item.openStream(), fileItem.getOutputStream(), true); } } catch (FileUploadIOException e) { try { throw (FileUploadException) e.getCause(); } catch (FileUploadBase.FileSizeLimitExceededException se) { request.setAttribute("org.apache.myfaces.custom.fileupload.exception", "fileSizeLimitExceeded"); String fieldName = fileItem.getFieldName(); request.setAttribute("org.apache.myfaces.custom.fileupload." + fieldName + ".maxSize", new Integer((int) allowedLimit)); } } catch (IOException e) { throw new IOFileUploadException("Processing of " + FileUploadBase.MULTIPART_FORM_DATA + " request failed. " + e.getMessage(), e); } if (fileItem instanceof FileItemHeadersSupport) { final FileItemHeaders fih = item.getHeaders(); ((FileItemHeadersSupport) fileItem).setHeaders(fih); } if (fileItem != null) { items.add(fileItem); } } return items; } catch (FileUploadIOException e) { throw (FileUploadException) e.getCause(); } catch (IOException e) { throw new FileUploadException(e.getMessage(), e); } }
From source file:org.apache.myfaces.webapp.filter.servlet.ServletChacheFileSizeErrorsFileUpload.java
/** * Similar to {@link ServletFileUpload#parseRequest(RequestContext)} but will * catch and swallow FileSizeLimitExceededExceptions in order to return as * many usable items as possible.//from ww w.j a v a 2s . com * * @param fileUpload * @return List of {@link FileItem} excluding any that exceed max size. * @throws FileUploadException */ public List parseRequestCatchingFileSizeErrors(HttpServletRequest request, FileUpload fileUpload) throws FileUploadException { try { List items = new ArrayList(); // The line below throws a SizeLimitExceededException (wrapped by a // FileUploadIOException) if the request is longer than the max size // allowed by fileupload requests (FileUpload.getSizeMax) // But note that if the request does not send proper headers this check // just will not do anything and we still have to check it again. FileItemIterator iter = fileUpload.getItemIterator(new ServletRequestContext(request)); FileItemFactory fac = fileUpload.getFileItemFactory(); if (fac == null) { throw new NullPointerException("No FileItemFactory has been set."); } long maxFileSize = this.getFileSizeMax(); long maxSize = this.getSizeMax(); boolean checkMaxSize = false; if (maxFileSize == -1L) { //The max allowed file size should be approximate to the maxSize maxFileSize = maxSize; } if (maxSize != -1L) { checkMaxSize = true; } while (iter.hasNext()) { final FileItemStream item = iter.next(); FileItem fileItem = fac.createItem(item.getFieldName(), item.getContentType(), item.isFormField(), item.getName()); long allowedLimit = 0L; try { if (maxFileSize != -1L || checkMaxSize) { if (checkMaxSize) { allowedLimit = maxSize > maxFileSize ? maxFileSize : maxSize; } else { //Just put the limit allowedLimit = maxFileSize; } long contentLength = getContentLength(item.getHeaders()); //If we have a content length in the header we can use it if (contentLength != -1L && contentLength > allowedLimit) { throw new FileUploadIOException(new FileSizeLimitExceededException( "The field " + item.getFieldName() + " exceeds its maximum permitted " + " size of " + allowedLimit + " characters.", contentLength, allowedLimit)); } //Otherwise we must limit the input as it arrives (NOTE: we cannot rely //on commons upload to throw this exception as it will close the //underlying stream final InputStream itemInputStream = item.openStream(); InputStream limitedInputStream = new LimitedInputStream(itemInputStream, allowedLimit) { protected void raiseError(long pSizeMax, long pCount) throws IOException { throw new FileUploadIOException(new FileSizeLimitExceededException( "The field " + item.getFieldName() + " exceeds its maximum permitted " + " size of " + pSizeMax + " characters.", pCount, pSizeMax)); } }; //Copy from the limited stream long bytesCopied = Streams.copy(limitedInputStream, fileItem.getOutputStream(), true); // Decrement the bytesCopied values from maxSize, so the next file copied // takes into account this value when allowedLimit var is calculated // Note the invariant before the line is maxSize >= bytesCopied, since if this // is not true a FileUploadIOException is thrown first. maxSize -= bytesCopied; } else { //We can just copy the data Streams.copy(item.openStream(), fileItem.getOutputStream(), true); } } catch (FileUploadIOException e) { try { throw (FileUploadException) e.getCause(); } catch (FileUploadBase.FileSizeLimitExceededException se) { request.setAttribute("org.apache.myfaces.custom.fileupload.exception", "fileSizeLimitExceeded"); String fieldName = fileItem.getFieldName(); request.setAttribute("org.apache.myfaces.custom.fileupload." + fieldName + ".maxSize", new Integer((int) allowedLimit)); } } catch (IOException e) { throw new IOFileUploadException("Processing of " + FileUploadBase.MULTIPART_FORM_DATA + " request failed. " + e.getMessage(), e); } if (fileItem instanceof FileItemHeadersSupport) { final FileItemHeaders fih = item.getHeaders(); ((FileItemHeadersSupport) fileItem).setHeaders(fih); } if (fileItem != null) { items.add(fileItem); } } return items; } catch (FileUploadIOException e) { throw (FileUploadException) e.getCause(); } catch (IOException e) { throw new FileUploadException(e.getMessage(), e); } }
From source file:org.apache.wicket.protocol.http.mock.MockHttpServletRequest.java
/** * If useMultiPartContentType set as true return the correct content-type. * /*from w ww. j a v a 2 s . co m*/ * @return The correct multipart content-type if useMultiPartContentType is true. Else null. */ @Override public String getContentType() { if (useMultiPartContentType) { return FileUploadBase.MULTIPART_FORM_DATA + "; boundary=abcdefgABCDEFG"; } return null; }
From source file:org.jahia.tools.files.FileUpload.java
/** * Init the MultiPartReq object if it's actually null * * @exception IOException/* w w w . j av a2s . c o m*/ */ protected void init() throws IOException { params = new HashMap<String, List<String>>(); paramsContentType = new HashMap<String, String>(); files = new HashMap<String, DiskFileItem>(); filesByFieldName = new HashMap<String, DiskFileItem>(); parseQueryString(); if (checkSavePath(savePath)) { try { final ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iter = upload.getItemIterator(req); DiskFileItemFactory factory = null; while (iter.hasNext()) { FileItemStream item = iter.next(); InputStream stream = item.openStream(); if (item.isFormField()) { final String name = item.getFieldName(); final List<String> v; if (params.containsKey(name)) { v = params.get(name); } else { v = new ArrayList<String>(); params.put(name, v); } v.add(Streams.asString(stream, encoding)); paramsContentType.put(name, item.getContentType()); } else { if (factory == null) { factory = new DiskFileItemFactory(); factory.setSizeThreshold(1); factory.setRepository(new File(savePath)); } DiskFileItem fileItem = (DiskFileItem) factory.createItem(item.getFieldName(), item.getContentType(), item.isFormField(), item.getName()); try { Streams.copy(item.openStream(), fileItem.getOutputStream(), true); } catch (FileUploadIOException e) { throw (FileUploadException) e.getCause(); } catch (IOException e) { throw new IOFileUploadException("Processing of " + FileUploadBase.MULTIPART_FORM_DATA + " request failed. " + e.getMessage(), e); } final FileItemHeaders fih = item.getHeaders(); fileItem.setHeaders(fih); if (fileItem.getSize() > 0) { files.put(fileItem.getStoreLocation().getName(), fileItem); filesByFieldName.put(fileItem.getFieldName(), fileItem); } } } } catch (FileUploadException ioe) { logger.error("Error while initializing FileUpload class:", ioe); throw new IOException(ioe.getMessage()); } } else { logger.error("FileUpload::init storage path does not exists or can write"); throw new IOException("FileUpload::init storage path does not exists or cannot write"); } }