com.blade.mvc.multipart.FileItem Java Examples

The following examples show how to use com.blade.mvc.multipart.FileItem. 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: HttpRequest.java    From blade with Apache License 2.0 6 votes vote down vote up
/**
 * Parse FileUpload to {@link FileItem}.
 *
 * @param fileUpload netty http file upload
 */
private void parseFileUpload(FileUpload fileUpload) throws IOException {
    if (!fileUpload.isCompleted()) {
        return;
    }
    FileItem fileItem = new FileItem();
    fileItem.setName(fileUpload.getName());
    fileItem.setFileName(fileUpload.getFilename());

    // Upload the file is moved to the specified temporary file,
    // because FileUpload will be release after completion of the analysis.
    // tmpFile will be deleted automatically if they are used.
    Path tmpFile = Files.createTempFile(
            Paths.get(fileUpload.getFile().getParent()), "blade_", "_upload");

    Path fileUploadPath = Paths.get(fileUpload.getFile().getPath());
    Files.move(fileUploadPath, tmpFile, StandardCopyOption.REPLACE_EXISTING);

    fileItem.setFile(tmpFile.toFile());
    fileItem.setPath(tmpFile.toFile().getPath());
    fileItem.setContentType(fileUpload.getContentType());
    fileItem.setLength(fileUpload.length());

    fileItems.put(fileItem.getName(), fileItem);
}
 
Example #2
Source File: ParameterInjectionExampleController.java    From tutorials with MIT License 6 votes vote down vote up
@PostRoute("/params-file") // DO NOT USE A SLASH WITHIN THE ROUTE OR IT WILL BREAK (?)
@JSON
public RestResponse<?> fileParam(@MultipartParam FileItem fileItem) throws Exception {
    try {
        byte[] fileContent = fileItem.getData();

        log.debug("Saving the uploaded file");
        java.nio.file.Path tempFile = Files.createTempFile("baeldung_tempfiles", ".tmp");
        Files.write(tempFile, fileContent, StandardOpenOption.WRITE);

        return RestResponse.ok();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return RestResponse.fail(e.getMessage());
    }
}
 
Example #3
Source File: RouteActionArguments.java    From blade with Apache License 2.0 5 votes vote down vote up
private static Object getAnnotationParam(Parameter parameter, String paramName, Request request) {
    Type  argType = parameter.getParameterizedType();
    Param param   = parameter.getAnnotation(Param.class);

    ParamStruct.ParamStructBuilder structBuilder = ParamStruct.builder().argType(argType).request(request);

    if (null != param) {
        ParamStruct paramStruct = structBuilder.param(param).paramName(paramName).build();
        return getQueryParam(paramStruct);
    }
    BodyParam bodyParam = parameter.getAnnotation(BodyParam.class);
    if (null != bodyParam) {
        return getBodyParam(structBuilder.build());
    }
    PathParam pathParam = parameter.getAnnotation(PathParam.class);
    if (null != pathParam) {
        return getPathParam(structBuilder.pathParam(pathParam).paramName(paramName).build());
    }
    HeaderParam headerParam = parameter.getAnnotation(HeaderParam.class);
    if (null != headerParam) {
        return getHeader(structBuilder.headerParam(headerParam).paramName(paramName).build());
    }
    // cookie param
    CookieParam cookieParam = parameter.getAnnotation(CookieParam.class);
    if (null != cookieParam) {
        return getCookie(structBuilder.cookieParam(cookieParam).paramName(paramName).build());
    }
    // form multipart
    MultipartParam multipartParam = parameter.getAnnotation(MultipartParam.class);
    if (null != multipartParam && argType == FileItem.class) {
        String name = StringKit.isBlank(multipartParam.value()) ? paramName : multipartParam.value();
        return request.fileItem(name).orElse(null);
    }
    return null;
}
 
Example #4
Source File: DemoController.java    From blade with Apache License 2.0 5 votes vote down vote up
@PostRoute("upload")
@JSON
public RestResponse upload(@MultipartParam FileItem fileItem) throws IOException {
    System.out.println(fileItem);
    fileItem.moveTo(new File(fileItem.getFileName()));
    return RestResponse.ok();
}
 
Example #5
Source File: HttpRequestTest.java    From blade with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileItems() {

    Request mockRequest = mockHttpRequest("GET");

    Map<String, FileItem> attr     = new HashMap<>();
    FileItem              fileItem = new FileItem();
    fileItem.setName("file");
    fileItem.setFileName("hello.png");
    fileItem.setPath("/usr/hello.png");
    fileItem.setContentType("image/png");
    fileItem.setLength(20445L);

    attr.put("img", fileItem);

    when(mockRequest.fileItems()).thenReturn(attr);

    Request request = new HttpRequest(mockRequest);

    FileItem img = request.fileItem("img").get();

    assertNotNull(img);

    assertNull(img.getFile());

    assertEquals("file", img.getName());
    assertEquals("hello.png", img.getFileName());
    assertEquals("/usr/hello.png", img.getPath());
    assertEquals(Long.valueOf(20445), Optional.of(img.getLength()).get());
    assertEquals("image/png", img.getContentType());

}
 
Example #6
Source File: HttpRequest.java    From blade with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, FileItem> fileItems() {
    return fileItems;
}
 
Example #7
Source File: Request.java    From blade with Apache License 2.0 2 votes vote down vote up
/**
 * Get current request all fileItems
 *
 * @return return request file items
 */
Map<String, FileItem> fileItems();
 
Example #8
Source File: Request.java    From blade with Apache License 2.0 2 votes vote down vote up
/**
 * get file item by request part name
 *
 * @param name
 * @return return Optional<FileItem>
 */
default Optional<FileItem> fileItem(@NonNull String name) {
    return Optional.ofNullable(fileItems().get(name));
}