如何通过JAX-RS(Jersey)从客户端获取JSON数据和fileupload的请求?

时间:2014-03-21 11:56:04

标签: java rest jersey jax-rs jersey-2.0

我需要通过JAX-R在单个请求中通过JSON数据和Image文件从客户端获取请求。如何在服务器端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) {
}

我有上面的代码,但它没有按预期工作。

1 个答案:

答案 0 :(得分:0)

这是来自工作项目的代码。发送文件数据和一些元数据。尝试相同。

    @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");
        }
    }
相关问题