org.apache.commons.fileupload.FileUploadBase Java Examples

The following examples show how to use org.apache.commons.fileupload.FileUploadBase. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpExchangeRequestContext.java    From Mars-Java with MIT License 6 votes vote down vote up
/**
 * Retrieve the content length of the request.
 *
 * @return The content length of the request.
 * @since 1.3
 */
public long contentLength() {
    long size = 0;
    try {
        List<String> ctList = request.getRequestHeaders().get(FileUploadBase.CONTENT_LENGTH);
        if(ctList != null || ctList.size() > 0){
            size = Long.parseLong(ctList.get(0));
        }
    } catch (NumberFormatException e) {
        try {
            size = request.getRequestBody().available();
        } catch (Exception e2){
        }
    }
    return size;
}
 
Example #2
Source File: ServletRequestContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieve the content length of the request.
 *
 * @return The content length of the request.
 * @since 1.3
 */
public long contentLength() {
    long size;
    try {
        size = Long.parseLong(request.getHeader(FileUploadBase.CONTENT_LENGTH));
    } catch (NumberFormatException e) {
        size = request.getContentLength();
    }
    return size;
}
 
Example #3
Source File: PortletRequestContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieve the content length of the request.
 *
 * @return The content length of the request.
 * @since 1.3
 */
public long contentLength() {
    long size;
    try {
        size = Long.parseLong(request.getProperty(FileUploadBase.CONTENT_LENGTH));
    } catch (NumberFormatException e) {
        size = request.getContentLength();
    }
    return size;
}
 
Example #4
Source File: StandardMultipartResolver.java    From AndServer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isMultipart(HttpRequest request) {
    if (!request.getMethod().allowBody()) {
        return false;
    }

    RequestBody body = request.getBody();
    return body != null && FileUploadBase.isMultipartContent(new BodyContext(body));
}
 
Example #5
Source File: HttpServFileUpload.java    From AndroidWebServ with Apache License 2.0 5 votes vote down vote up
public static final boolean isMultipartContent(HttpRequest request) {
    String method = request.getRequestLine().getMethod();
    if (!POST_METHOD.equalsIgnoreCase(method)) {
        return false;
    }
    if (!(request instanceof HttpEntityEnclosingRequest)) {
        return false;
    }
    return FileUploadBase.isMultipartContent(new HttpServRequestContext(
            (HttpEntityEnclosingRequest) request));
}
 
Example #6
Source File: CommonsUploadMultipartObserverTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldValidateWhenSizeLimitExceededExceptionOccurs() throws Exception {
	when(event.getMethod()).thenReturn(uploadMethodController);

	when(observer.createServletFileUpload(config)).thenReturn(servletFileUpload);
	when(servletFileUpload.parseRequest(request)).thenThrow(new FileUploadBase.SizeLimitExceededException("", 3L, 2L));

	observer.upload(event, request, config, validator);

	verify(validator).add(any(I18nMessage.class));
}
 
Example #7
Source File: CommonsUploadMultipartObserverTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldValidateWhenSizeLimitExceededExceptionOccursFromAnnotation() throws Exception {
	when(event.getMethod()).thenReturn(uploadMethodControllerWithAnnotation);

	when(observer.createServletFileUpload(config)).thenReturn(servletFileUpload);
	when(servletFileUpload.parseRequest(request)).thenThrow(new FileUploadBase.SizeLimitExceededException("", 3L, 2L));

	observer.upload(event, request, config, validator);

	verify(servletFileUpload).setFileSizeMax(10);
	verify(servletFileUpload).setSizeMax(20);
}
 
Example #8
Source File: ParsingFormData.java    From Mars-Java with MIT License 4 votes vote down vote up
/**
 * 获取文件列表
 * @param request 请求
 * @param contentType 请求类型
 * @return 返回
 * @throws Exception 异常
 */
public static List<FileItem> getFileItem(HttpExchange request, String contentType) throws Exception {

    FileItemFactory factory = new DiskFileItemFactory();

    FileUploadConfig fileUploadConfig = MarsConfiguration.getConfig().fileUploadConfig();

    FileUploadBase fileUploadBase = new HttpExchangeFileUpload();
    fileUploadBase.setFileItemFactory(factory);

    fileUploadBase.setFileSizeMax(fileUploadConfig.getFileSizeMax());
    fileUploadBase.setSizeMax(fileUploadConfig.getSizeMax());

    List<FileItem> fileItemList = fileUploadBase.parseRequest(new HttpExchangeRequestContext(request,contentType));

    return fileItemList;
}
 
Example #9
Source File: RpcServlet.java    From Brutusin-RPC with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @param req
 * @param rpcRequest
 * @param service
 * @return
 * @throws Exception
 */
private Map<String, InputStream> getStreams(HttpServletRequest req, RpcRequest rpcRequest, HttpAction service) throws Exception {
    if (!FileUploadBase.isMultipartContent(new ServletRequestContext(req))) {
        return null;
    }
    int streamsNumber = getInputStreamsNumber(rpcRequest, service);
    boolean isResponseStreamed = service.isBinaryResponse();
    FileItemIterator iter = (FileItemIterator) req.getAttribute(REQ_ATT_MULTIPART_ITERATOR);
    int count = 0;
    final Map<String, InputStream> map = new HashMap();
    final File tempDirectory;
    if (streamsNumber > 1 || streamsNumber == 1 && isResponseStreamed) {
        tempDirectory = createTempUploadDirectory();
        req.setAttribute(REQ_ATT_TEMPORARY_FOLDER, tempDirectory);
    } else {
        tempDirectory = null;
    }
    FileItemStream item = (FileItemStream) req.getAttribute(REQ_ATT_MULTIPART_CURRENT_ITEM);
    long availableLength = RpcConfig.getInstance().getMaxRequestSize();
    while (item != null) {
        count++;
        long maxLength = Math.min(availableLength, RpcConfig.getInstance().getMaxFileSize());
        if (count < streamsNumber || isResponseStreamed) { // if response is streamed all inputstreams have to be readed first
            File file = new File(tempDirectory, item.getFieldName());
            FileOutputStream fos = new FileOutputStream(file);
            try {
                Miscellaneous.pipeSynchronously(new LimitedLengthInputStream(item.openStream(), maxLength), fos);
            } catch (MaxLengthExceededException ex) {
                if (maxLength == RpcConfig.getInstance().getMaxFileSize()) {
                    throw new MaxLengthExceededException("Upload part '" + item.getFieldName() + "' exceeds maximum length (" + RpcConfig.getInstance().getMaxFileSize() + " bytes)", RpcConfig.getInstance().getMaxFileSize());
                } else {
                    throw new MaxLengthExceededException("Request exceeds maximum length (" + RpcConfig.getInstance().getMaxRequestSize() + " bytes)", RpcConfig.getInstance().getMaxRequestSize());
                }
            }
            map.put(item.getFieldName(), new MetaDataInputStream(new FileInputStream(file), item.getName(), item.getContentType(), file.length(), null));
            availableLength -= file.length();
        } else if (count == streamsNumber) {
            map.put(item.getFieldName(), new MetaDataInputStream(new LimitedLengthInputStream(item.openStream(), maxLength), item.getName(), item.getContentType(), null, null));
            break;
        }
        req.setAttribute(REQ_ATT_MULTIPART_CURRENT_ITEM, item);
        if (iter.hasNext()) {
            item = iter.next();
        } else {
            item = null;
        }
    }
    if (count != streamsNumber) {
        throw new IllegalArgumentException("Invalid multipart request received. Number of uploaded files (" + count + ") does not match expected (" + streamsNumber + ")");
    }
    return map;
}
 
Example #10
Source File: LocalizableApiError.java    From etf-webapp with European Union Public License 1.2 4 votes vote down vote up
public LocalizableApiError(final FileUploadBase.SizeLimitExceededException exception) {
    super("l.max.upload.size.exceeded");
    sensitiveInformation = false;
    sc = 413;
}
 
Example #11
Source File: HttpPartIteratorAdapter.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Determine if given request is multipart.
 */
public static boolean isMultipart(final HttpServletRequest httpRequest) {
  // We're circumventing ServletFileUpload.isMultipartContent as some clients (nuget) use PUT for multipart uploads
  return FileUploadBase.isMultipartContent(new ServletRequestContext(httpRequest));
}
 
Example #12
Source File: UploadHelper.java    From smart-framework with Apache License 2.0 4 votes vote down vote up
/**
 * 创建 multipart 请求参数列表
 */
public static List<Object> createMultipartParamList(HttpServletRequest request) throws Exception {
    // 定义参数列表
    List<Object> paramList = new ArrayList<Object>();
    // 创建两个对象,分别对应 普通字段 与 文件字段
    Map<String, Object> fieldMap = new HashMap<String, Object>();
    List<Multipart> multipartList = new ArrayList<Multipart>();
    // 获取并遍历表单项
    List<FileItem> fileItemList;
    try {
        fileItemList = fileUpload.parseRequest(request);
    } catch (FileUploadBase.FileSizeLimitExceededException e) {
        // 异常转换(抛出自定义异常)
        throw new UploadException(e);
    }
    for (FileItem fileItem : fileItemList) {
        // 分两种情况处理表单项
        String fieldName = fileItem.getFieldName();
        if (fileItem.isFormField()) {
            // 处理普通字段
            String fieldValue = fileItem.getString(FrameworkConstant.UTF_8);
            fieldMap.put(fieldName, fieldValue);
        } else {
            // 处理文件字段
            String fileName = FileUtil.getRealFileName(fileItem.getName());
            if (StringUtil.isNotEmpty(fileName)) {
                long fileSize = fileItem.getSize();
                String contentType = fileItem.getContentType();
                InputStream inputSteam = fileItem.getInputStream();
                // 创建 Multipart 对象,并将其添加到 multipartList 中
                Multipart multipart = new Multipart(fieldName, fileName, fileSize, contentType, inputSteam);
                multipartList.add(multipart);
            }
        }
    }
    // 初始化参数列表
    paramList.add(new Params(fieldMap));
    paramList.add(new Multiparts(multipartList));
    // 返回参数列表
    return paramList;
}
 
Example #13
Source File: UploadHelper.java    From smart-framework with Apache License 2.0 4 votes vote down vote up
/**
 * 创建 multipart 请求参数列表
 */
public static List<Object> createMultipartParamList(HttpServletRequest request) throws Exception {
    // 定义参数列表
    List<Object> paramList = new ArrayList<Object>();
    // 创建两个对象,分别对应 普通字段 与 文件字段
    Map<String, Object> fieldMap = new HashMap<String, Object>();
    List<Multipart> multipartList = new ArrayList<Multipart>();
    // 获取并遍历表单项
    List<FileItem> fileItemList;
    try {
        fileItemList = fileUpload.parseRequest(request);
    } catch (FileUploadBase.FileSizeLimitExceededException e) {
        // 异常转换(抛出自定义异常)
        throw new UploadException(e);
    }
    for (FileItem fileItem : fileItemList) {
        // 分两种情况处理表单项
        String fieldName = fileItem.getFieldName();
        if (fileItem.isFormField()) {
            // 处理普通字段
            String fieldValue = fileItem.getString(FrameworkConstant.UTF_8);
            fieldMap.put(fieldName, fieldValue);
        } else {
            // 处理文件字段
            String fileName = FileUtil.getRealFileName(fileItem.getName());
            if (StringUtil.isNotEmpty(fileName)) {
                long fileSize = fileItem.getSize();
                String contentType = fileItem.getContentType();
                InputStream inputSteam = fileItem.getInputStream();
                // 创建 Multipart 对象,并将其添加到 multipartList 中
                Multipart multipart = new Multipart(fieldName, fileName, fileSize, contentType, inputSteam);
                multipartList.add(multipart);
            }
        }
    }
    // 初始化参数列表
    paramList.add(new Params(fieldMap));
    paramList.add(new Multiparts(multipartList));
    // 返回参数列表
    return paramList;
}
 
Example #14
Source File: ServletFileUpload.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Utility method that determines whether the request contains multipart
 * content.
 *
 * @param request The servlet request to be evaluated. Must be non-null.
 *
 * @return <code>true</code> if the request is multipart;
 *         <code>false</code> otherwise.
 */
public static final boolean isMultipartContent(
        HttpServletRequest request) {
    if (!POST_METHOD.equalsIgnoreCase(request.getMethod())) {
        return false;
    }
    return FileUploadBase.isMultipartContent(new ServletRequestContext(request));
}
 
Example #15
Source File: PortletFileUpload.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Utility method that determines whether the request contains multipart
 * content.
 *
 * @param request The portlet request to be evaluated. Must be non-null.
 *
 * @return <code>true</code> if the request is multipart;
 *         <code>false</code> otherwise.
 */
public static final boolean isMultipartContent(ActionRequest request) {
    return FileUploadBase.isMultipartContent(
            new PortletRequestContext(request));
}