Java Code Examples for org.apache.commons.fileupload.FileUploadBase

The following examples show how to use org.apache.commons.fileupload.FileUploadBase. These examples are extracted from open source projects. 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 Project: Mars-Java   Source File: HttpExchangeRequestContext.java    License: 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 Project: lams   Source File: ServletRequestContext.java    License: 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 Project: lams   Source File: PortletRequestContext.java    License: 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 Project: AndServer   Source File: StandardMultipartResolver.java    License: 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 Project: AndroidWebServ   Source File: HttpServFileUpload.java    License: 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
@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
@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 Project: Mars-Java   Source File: ParsingFormData.java    License: 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 Project: Brutusin-RPC   Source File: RpcServlet.java    License: 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
public LocalizableApiError(final FileUploadBase.SizeLimitExceededException exception) {
    super("l.max.upload.size.exceeded");
    sensitiveInformation = false;
    sc = 413;
}
 
Example 11
/**
 * 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 Project: smart-framework   Source File: UploadHelper.java    License: 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 Project: smart-framework   Source File: UploadHelper.java    License: 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 Project: lams   Source File: ServletFileUpload.java    License: 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 Project: lams   Source File: PortletFileUpload.java    License: 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));
}