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);
}
}
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;
}
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();
}
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;
}
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);
}
Aggregations