Search in sources :

Example 71 with FileItem

use of org.apache.commons.fileupload.FileItem in project kie-wb-common by kiegroup.

the class FormsDocumentServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Map<String, Object> response = new HashMap<>();
    try {
        FileItem fileItem = getFileItem(req);
        String id = UUID.randomUUID().toString();
        String content = Base64.getEncoder().encodeToString(fileItem.get());
        DocumentUploadChunk chunk = new DocumentUploadChunk(id, fileItem.getName(), 0, 1, content);
        DocumentUploadSession session = new DocumentUploadSession(chunk.getDocumentId(), chunk.getDocumentName(), chunk.getMaxChunks());
        session.add(chunk);
        storage.uploadContentChunk(chunk);
        session.setState(DocumentUploadSession.State.MERGING);
        storage.merge(session);
        DocumentData data = new DocumentData(id, fileItem.getName(), fileItem.getSize(), "", System.currentTimeMillis());
        response.put("document", data);
    } catch (Exception e) {
        response.put("error", "error");
    } finally {
        writeResponse(resp, response);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) DocumentData(org.kie.workbench.common.forms.jbpm.model.document.DocumentData) DocumentUploadSession(org.kie.workbench.common.forms.jbpm.server.service.impl.documents.DocumentUploadSession) HashMap(java.util.HashMap) DocumentUploadChunk(org.kie.workbench.common.forms.jbpm.service.shared.documents.DocumentUploadChunk) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 72 with FileItem

use of org.apache.commons.fileupload.FileItem in project mica2 by obiba.

the class DraftStudiesImportResource method getUploadedFile.

/**
 * Returns the first {@code FileItem} that is represents a file upload field. If no such field exists, this method
 * returns null
 *
 * @param request
 * @return
 * @throws FileUploadException
 */
FileItem getUploadedFile(HttpServletRequest request) throws FileUploadException {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    for (FileItem fileItem : upload.parseRequest(request)) {
        if (!fileItem.isFormField()) {
            return fileItem;
        }
    }
    return null;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory)

Example 73 with FileItem

use of org.apache.commons.fileupload.FileItem in project mica2 by obiba.

the class TempFilesResource method upload.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Timed
public Response upload(@Context HttpServletRequest request, @Context UriInfo uriInfo) throws IOException, FileUploadException {
    FileItem fileItem = getUploadedFile(request);
    if (fileItem == null)
        throw new FileUploadException("Failed to extract file item from request");
    TempFile tempFile = tempFileService.addTempFile(fileItem.getName(), fileItem.getInputStream());
    URI location = uriInfo.getBaseUriBuilder().path(TempFilesResource.class).path(TempFilesResource.class, "file").build(tempFile.getId());
    return Response.created(location).build();
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) TempFile(org.obiba.mica.file.TempFile) URI(java.net.URI) FileUploadException(org.apache.commons.fileupload.FileUploadException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed)

Example 74 with FileItem

use of org.apache.commons.fileupload.FileItem in project leopard by tanhaichao.

the class LeopardMultipartResolver method parseFileItems.

protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String encoding) {
    // System.out.println("fileItems:" + fileItems);
    MultipartParsingResult result = super.parseFileItems(fileItems, encoding);
    for (String fieldName : NAMES) {
        if (result.getMultipartFiles().get(fieldName) != null) {
            break;
        }
        FileItem item = this.find(fileItems, fieldName);
        if (item == null) {
            continue;
        }
        String content = item.getString();
        // System.out.println("fieldName:" + fieldName + " content:" + content);
        if (StringUtils.isEmpty(content)) {
            continue;
        }
        if (!content.startsWith("data:image")) {
            continue;
        }
        List<MultipartFile> list = this.toList(result, fieldName, content);
        result.getMultipartFiles().put(fieldName, list);
    // break;
    }
    // multipartFiles.add(file.getName(), file);
    return result;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 75 with FileItem

use of org.apache.commons.fileupload.FileItem in project che by eclipse.

the class ProjectService method uploadProjectFromZip.

@POST
@Path("/upload/zipproject/{path:.*}")
@Consumes({ MediaType.MULTIPART_FORM_DATA })
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Upload zip project", notes = "Upload project from local zip", response = ProjectConfigDto.class)
@ApiResponses({ @ApiResponse(code = 200, message = ""), @ApiResponse(code = 401, message = "User not authorized to call this operation"), @ApiResponse(code = 403, message = "Forbidden operation"), @ApiResponse(code = 409, message = "Resource already exists"), @ApiResponse(code = 500, message = "Unsupported source type") })
public List<SourceEstimation> uploadProjectFromZip(@ApiParam(value = "Path in the project", required = true) @PathParam("path") String path, @ApiParam(value = "Force rewrite existing project", allowableValues = "true,false") @QueryParam("force") boolean force, Iterator<FileItem> formData) throws ServerException, IOException, ConflictException, ForbiddenException, NotFoundException, BadRequestException {
    // Not all importers uses virtual file system API. In this case virtual file system API doesn't get events and isn't able to set
    final FolderEntry baseProjectFolder = (FolderEntry) getVirtualFile(path, force);
    int stripNumber = 0;
    String projectName = "";
    String projectDescription = "";
    FileItem contentItem = null;
    while (formData.hasNext()) {
        FileItem item = formData.next();
        if (!item.isFormField()) {
            if (contentItem == null) {
                contentItem = item;
            } else {
                throw new ServerException("More then one upload file is found but only one is expected. ");
            }
        } else {
            switch(item.getFieldName()) {
                case ("name"):
                    projectName = item.getString().trim();
                    break;
                case ("description"):
                    projectDescription = item.getString().trim();
                    break;
                case ("skipFirstLevel"):
                    stripNumber = Boolean.parseBoolean(item.getString().trim()) ? 1 : 0;
                    break;
            }
        }
    }
    if (contentItem == null) {
        throw new ServerException("Cannot find zip file for upload.");
    }
    try (InputStream zip = contentItem.getInputStream()) {
        baseProjectFolder.getVirtualFile().unzip(zip, true, stripNumber);
    }
    return resolveSources(path);
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServerException(org.eclipse.che.api.core.ServerException) InputStream(java.io.InputStream) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

FileItem (org.apache.commons.fileupload.FileItem)165 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)78 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)72 FileUploadException (org.apache.commons.fileupload.FileUploadException)59 File (java.io.File)55 IOException (java.io.IOException)51 ArrayList (java.util.ArrayList)40 HashMap (java.util.HashMap)32 ServletException (javax.servlet.ServletException)30 List (java.util.List)27 InputStream (java.io.InputStream)24 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)23 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)16 Map (java.util.Map)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 ServletRequestContext (org.apache.commons.fileupload.servlet.ServletRequestContext)10 Test (org.junit.Test)10 Iterator (java.util.Iterator)9 FileItemWrap (com.github.bordertech.wcomponents.file.FileItemWrap)8 Locale (java.util.Locale)8