简体   繁体   English

如何通过JAX-RS(Jersey)从客户端获取带有JSON数据和文件上传的请求?

[英]How can I get request from the client with JSON data and fileupload via JAX-RS(Jersey)?

I need to get the request from the client with JSON data and an Image file in a single request via JAX-Rs. 我需要通过JAX-R在单个请求中从客户端获得带有JSON数据和图像文件的请求。 How can I receive it in the server side Java application to process it further. 如何在服务器端Java应用程序中接收它以进一步处理它。

@POST
@Path("/path")
@Consumes({MediaType.APPLICATION_JSON, MediaType.MULTIPART_FORM_DATA})
public String get(@Context UriInfo uriInfo,
        final MyDTO myDTO,
        @FormDataParam("file") final InputStream inputStream,
        @FormDataParam("file") final FormDataContentDisposition fileDetail) {
}

I am having the above code but it is not working as expected. 我有上面的代码,但未按预期工作。

This is code from working project. 这是工作项目中的代码。 Send file data and some meta. 发送文件数据和一些元数据。 Try the same. 尝试相同。

    @POST
    @Path("import")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON + "; charset=UTF-8")
    @RolesAllowed(SystemRoles.ADMIN)
    public void importScenario(@FormDataParam("importScenario") final InputStream is,
            @FormDataParam("complectId") final Long complectId) {
        LOG.debug("start import file");
        if (is == null) {
            throw new IllegalArgumentException(MessageBundle.get(InnerMessage.NO_FILE));
        }
        try {
            LOG.debug("start execute import bean");
            importService.importScenarioOrComplect(is, complectId);
            LOG.debug("end execute import bean");
        } catch (FileStorageException e) {
            throw new IllegalArgumentException(e);
        } catch (STCoreException e) {
            throw new EJBException(MessageBundle.get(InnerMessage.CRITICAL_ERROR), e);
        } finally {
            LOG.debug("end import file");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM