在Jersey中上传文件而不使用multipart [英] Uploading a file in Jersey without using multipart

查看:198
本文介绍了在Jersey中上传文件而不使用multipart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行一个Web服务,将文件从一种文件格式转换为另一种文件格式。转换逻辑已经运行,但现在,我想通过泽西查询这个逻辑。每当通过教程/问题解决通过泽西岛上传的文件时,人们都会描述如何使用多部分表单数据来完成此操作。不过,我只是想发送和返回一个文件,并跳过发送多个部分的开销。 (这个web服务是由另外一台我控制的机器触发的,所以没有涉及HTML表单。)

我的问题是如何实现类似下面的内容:

  @POST 
@Path({sessionId}
@Consumes(image / png)
$ b $ public $ response(put){@PathParam(sessionId)String sessionId,
@WhatToPutHere InputStream uploadedFileStream){
return BusinessLogic.convert(uploadedFile) ; //返回StreamingOutput - works!
}

如何获得 uploadedFileStream (它应该是一些注释,我猜这当然不是 @WhatToPutHere )我想出了如何直接通过 StreamingOutput

返回一个文件感谢您的帮助!

解决方案

您不必在函数的第二个参数中放置任何东西,只需将其取消注释
唯一你必须小心的是命名资源:

lockquote

资源应该有一个URI:someSite / someRESTEndPoint / myResourceId该函数应该是:



  @POST 
@Path({myResourceId})
@Consumes(image / png)
@Produces(application / pdf)
public Response put(@PathParam(myResourceId) String myResourceId,
InputStream uploadedFileStream){
返回BusinessLogic.convert(uploadedFileStream);




lock $
$ p $如果你想使用某种SessionID ,我宁愿使用Header Param ...类似于:



 < 
@Path({myResourceId})
@Consumes(image / png)
@Produces(application / pdf)
公共响应put(@HeaderParam(sessionId)字符串sessionId,
@PathParam(myResourceId)字符串myResourceId,
InputStream uploadedFileStream){
返回BusinessLogic.convert(uploadedFileStream);
}


I run a web service where I convert a file from one file format into another. The conversion logic is already functioning but now, I want to query this logic via Jersey. Whenever file upload via Jersey is addressed in tutorials / questions, people describe how to do this using multipart form data. I do however simply want to send and return a single file and skip the overhead of sending multiple parts. (The webservice is triggered by another machine which I control so there is no HTML form involved.)

My question is how would I achieve something like the following:

@POST
@Path("{sessionId"}
@Consumes("image/png")
@Produces("application/pdf")
public Response put(@PathParam("sessionId") String sessionId, 
                    @WhatToPutHere InputStream uploadedFileStream) {
  return BusinessLogic.convert(uploadedFile); // returns StreamingOutput - works!
}

How do I get hold of the uploadedFileStream (It should be some annotation, I guess which is of course not @WhatToPutHere). I figured out how to directly return a file via StreamingOutput.

Thanks for any help!

解决方案

You do not have to put anything in the second param of the function; just leave it un-annoted. The only thing you have to be carefull is to "name" the resource:

The resource should have an URI like: someSite/someRESTEndPoint/myResourceId so the function should be:

@POST
@Path("{myResourceId}")
@Consumes("image/png")
@Produces("application/pdf")
public Response put(@PathParam("myResourceId") String myResourceId, 
                                               InputStream uploadedFileStream) {
    return BusinessLogic.convert(uploadedFileStream); 
}

If you want to use some kind of SessionID, I'd prefer to use a Header Param... something like:

@POST
@Path("{myResourceId}")
@Consumes("image/png")
@Produces("application/pdf")
public Response put(@HeaderParam("sessionId") String sessionId,
                    @PathParam("myResourceId") String myResourceId, 
                                               InputStream uploadedFileStream) {
    return BusinessLogic.convert(uploadedFileStream); 
}

这篇关于在Jersey中上传文件而不使用multipart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆