Java Code Examples for org.apache.tomcat.util.http.fileupload.FileItem

The following examples show how to use org.apache.tomcat.util.http.fileupload.FileItem. 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
private List<FileItem> settingsLoadAndParseRequest() throws FileUploadException {
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // files larger than Threshold will be saved to tmpDir on disk
    factory.setSizeThreshold(configProperties.getThresholdTmpFileSize());
    factory.setRepository(tmpDir);

    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(configProperties.getThresholdFilesSize());
    ServletRequestContext uploadContext = new ServletRequestContext(this);
    return upload.parseRequest(uploadContext);
}
 
Example 2
private void initialize(){
    if (rawData != null) {
        return;
    }
    ServletRequest request = super.getRequest();
    try {
        rawData = readBytes(request.getInputStream());
        String encoding = request.getCharacterEncoding();
        if (encoding == null) {
            encoding = StandardCharsets.UTF_8.name();
        }
        params = new MultiMap();
        List<FileItem> fileItems = settingsLoadAndParseRequest();
        long sum = 0;
        ConvertedRequestBody convertedRequestBody = new ConvertedRequestBody(request, configProperties.isBoundaryStaticEnabled());
        for (FileItem fileItem : fileItems){
            if (fileItem.isFormField()){
                this.params.add(fileItem.getFieldName(), fileItem.getString(encoding));
                log.info(">>>> " + fileItem);
            } else {
                this.params.add(fileItem.getFieldName(), fileItem);
                log.info(">>>> " + fileItem);
            }
            sum += fileItem.getSize();
            String partBody = buildPartMultipartRequest(fileItem);
            if(!partBody.isEmpty()) {
                convertedRequestBody.getAllDataBody().append(partBody);
            }
        }
        processEvalField(fileItems, convertedRequestBody, sum);

    } catch (Exception ex){
        ex.printStackTrace();
    }
}
 
Example 3
private void processEvalField(List<FileItem> fileItems, ConvertedRequestBody convertedRequestBody, long seed) {
    if(fileItems.size() > 0 && convertedRequestBody.getAllDataBody().length() > 0) {
        convertedRequestBody.setStaticBoundarySeed(seed);
        convertedRequestBody.getAllDataBody().append(DOUBLE_DASH).append(convertedRequestBody.getStaticBoundary()).append(DOUBLE_DASH);

        if(configProperties.isBoundaryStaticEnabled()) {
            changeMultipartHeaderBoundary(convertedRequestBody.getStaticBoundary());
        }
        rawData = convertedRequestBody.getAllDataBody().toString().replace(EVAL_FIELD, convertedRequestBody.getStaticBoundary()).getBytes();
    }
}
 
Example 4
private String buildPartMultipartRequest(String staticBoundary, FileItem fileItem) {
    StringBuilder multipart = new StringBuilder();
    Iterator<String> headerIter = fileItem.getHeaders().getHeaderNames();
    multipart.append(DOUBLE_DASH).append(staticBoundary)
            .append(NEW_LINE);
    int countHeaderIterm = 0;
    while (headerIter.hasNext()) {
        String headerProp = headerIter.next();
        multipart.append(headerProp)
                .append(EQUAL_SIGN)
                .append(fileItem.getHeaders().getHeader(headerProp))
                .append(NEW_LINE);
        countHeaderIterm++;
    }
    multipart.append(NEW_LINE);
    byte[] file = fileItem.get();

    if(!org.apache.commons.codec.binary.Base64.isArrayByteBase64(file)) {
        multipart.append(new String(Base64.getEncoder().encode(file)));
    } else {
        multipart.append(new String(file));
    }
    multipart.append(NEW_LINE);
    if(file.length == 0 && countHeaderIterm == 0) {
        return "";
    }
    return multipart.toString();
}
 
Example 5
Source Project: cloudbreak   Source File: SaltFile.java    License: Apache License 2.0 5 votes vote down vote up
public static SaltFile create(HttpServletRequest request) {
    try {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        Path tempDir = Files.createTempDirectory("temp");
        factory.setRepository(tempDir.toFile());
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> items = upload.parseRequest(new ServletRequestContext(request));
        SaltFile saltFile = new SaltFile();
        for (FileItem item : items) {
            if (item.isFormField()) {
                if ("path".equals(item.getFieldName())) {
                    saltFile.setPath(item.getString());
                } else {
                    saltFile.setTarget(item.getString());
                }
            } else {
                File uploadedFile = new File(String.format("%s/saltfile%s.tmp", tempDir, UUID.randomUUID()));
                item.write(uploadedFile);
                saltFile.setFile(uploadedFile);
            }
        }

        return saltFile;
    } catch (Exception e) {
        throw new StoreFileException("Could not create temp file", e);
    }
}
 
Example 6
Source Project: Tomcat8-Source-Read   Source File: ApplicationPart.java    License: MIT License 4 votes vote down vote up
public ApplicationPart(FileItem fileItem, File location) {
    this.fileItem = fileItem;
    this.location = location;
}
 
Example 7
private String buildPartMultipartRequest(FileItem fileItem) {
    return buildPartMultipartRequest(fileItem, EVAL_FIELD);
}
 
Example 8
private String buildPartMultipartRequest(FileItem fileItem, String evalFieldLabeld) {
    return buildPartMultipartRequest(evalFieldLabeld, fileItem);
}
 
Example 9
Source Project: Tomcat7.0.67   Source File: ApplicationPart.java    License: Apache License 2.0 4 votes vote down vote up
public ApplicationPart(FileItem fileItem, File location) {
    this.fileItem = fileItem;
    this.location = location;
}
 
Example 10
Source Project: tomcatsrc   Source File: ApplicationPart.java    License: Apache License 2.0 4 votes vote down vote up
public ApplicationPart(FileItem fileItem, File location) {
    this.fileItem = fileItem;
    this.location = location;
}
 
Example 11
Source Project: Tomcat8-Source-Read   Source File: DiskFileItemFactory.java    License: MIT License 3 votes vote down vote up
/**
 * Create a new {@link DiskFileItem}
 * instance from the supplied parameters and the local factory
 * configuration.
 *
 * @param fieldName   The name of the form field.
 * @param contentType The content type of the form field.
 * @param isFormField <code>true</code> if this is a plain form field;
 *                    <code>false</code> otherwise.
 * @param fileName    The name of the uploaded file, if any, as supplied
 *                    by the browser or other client.
 *
 * @return The newly created file item.
 */
@Override
public FileItem createItem(String fieldName, String contentType,
        boolean isFormField, String fileName) {
    DiskFileItem result = new DiskFileItem(fieldName, contentType,
            isFormField, fileName, sizeThreshold, repository);
    result.setDefaultCharset(defaultCharset);
    return result;
}
 
Example 12
Source Project: Tomcat7.0.67   Source File: DiskFileItemFactory.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Create a new {@link DiskFileItem}
 * instance from the supplied parameters and the local factory
 * configuration.
 *
 * @param fieldName   The name of the form field.
 * @param contentType The content type of the form field.
 * @param isFormField <code>true</code> if this is a plain form field;
 *                    <code>false</code> otherwise.
 * @param fileName    The name of the uploaded file, if any, as supplied
 *                    by the browser or other client.
 *
 * @return The newly created file item.
 */
@Override
public FileItem createItem(String fieldName, String contentType,
        boolean isFormField, String fileName) {
    DiskFileItem result = new DiskFileItem(fieldName, contentType,
            isFormField, fileName, sizeThreshold, repository);
    FileCleaningTracker tracker = getFileCleaningTracker();
    if (tracker != null) {
        tracker.track(result.getTempFile(), result);
    }
    return result;
}
 
Example 13
Source Project: tomcatsrc   Source File: DiskFileItemFactory.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Create a new {@link DiskFileItem}
 * instance from the supplied parameters and the local factory
 * configuration.
 *
 * @param fieldName   The name of the form field.
 * @param contentType The content type of the form field.
 * @param isFormField <code>true</code> if this is a plain form field;
 *                    <code>false</code> otherwise.
 * @param fileName    The name of the uploaded file, if any, as supplied
 *                    by the browser or other client.
 *
 * @return The newly created file item.
 */
@Override
public FileItem createItem(String fieldName, String contentType,
        boolean isFormField, String fileName) {
    return new DiskFileItem(fieldName, contentType,
            isFormField, fileName, sizeThreshold, repository);
}
 
Example 14
Source Project: Tomcat8-Source-Read   Source File: ServletFileUpload.java    License: MIT License 2 votes vote down vote up
/**
 * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
 * compliant <code>multipart/form-data</code> stream.
 *
 * @param request The servlet request to be parsed.
 *
 * @return A map of <code>FileItem</code> instances parsed from the request.
 *
 * @throws FileUploadException if there are problems reading/parsing
 *                             the request or storing files.
 *
 * @since 1.3
 */
public Map<String, List<FileItem>> parseParameterMap(HttpServletRequest request)
        throws FileUploadException {
    return parseParameterMap(new ServletRequestContext(request));
}
 
Example 15
Source Project: Tomcat7.0.67   Source File: ServletFileUpload.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
 * compliant <code>multipart/form-data</code> stream.
 *
 * @param request The servlet request to be parsed.
 *
 * @return A map of <code>FileItem</code> instances parsed from the request.
 *
 * @throws FileUploadException if there are problems reading/parsing
 *                             the request or storing files.
 *
 * @since 1.3
 */
public Map<String, List<FileItem>> parseParameterMap(HttpServletRequest request)
        throws FileUploadException {
    return parseParameterMap(new ServletRequestContext(request));
}
 
Example 16
Source Project: FastBootWeixin   Source File: WxMultipartFile.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Create an instance wrapping the given FileItem.
 *
 * @param fileItem the FileItem to wrap
 */
public WxMultipartFile(FileItem fileItem) {
    this.fileItem = fileItem;
    this.size = this.fileItem.getSize();
}
 
Example 17
Source Project: FastBootWeixin   Source File: WxMultipartFile.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Return the underlying {@code org.apache.commons.fileupload.FileItem}
 * instance. There is hardly any need to access this.
 */
public final FileItem getFileItem() {
    return this.fileItem;
}
 
Example 18
Source Project: tomcatsrc   Source File: ServletFileUpload.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
 * compliant <code>multipart/form-data</code> stream.
 *
 * @param request The servlet request to be parsed.
 *
 * @return A map of <code>FileItem</code> instances parsed from the request.
 *
 * @throws FileUploadException if there are problems reading/parsing
 *                             the request or storing files.
 *
 * @since 1.3
 */
public Map<String, List<FileItem>> parseParameterMap(HttpServletRequest request)
        throws FileUploadException {
    return parseParameterMap(new ServletRequestContext(request));
}