使用 JERSEY 输入和输出二进制流?
我正在使用 Jersey 来实现 RESTful API,该 API 主要检索和提供 JSON 编码数据。但在某些情况下,我需要完成以下任务:
- 导出可下载文档,例如 PDF、XLS、ZIP 或其他二进制文件。
- 检索多部分数据,例如一些 JSON 加上上传的 XLS 文件
我有一个基于 JQuery 的单页 Web 客户端,它创建对此 Web 服务的 AJAX 调用。目前,它不进行表单提交,而是使用 GET 和 POST(带有 JSON 对象)。我应该使用表单发布来发送数据和附加的二进制文件,还是可以使用 JSON 加二进制文件创建多部分请求?
我的应用程序的服务层当前在生成 PDF 文件时创建一个 ByteArrayOutputStream。通过 Jersey 将此流输出到客户端的最佳方式是什么?我创建了一个 MessageBodyWriter,但我不知道如何从 Jersey 资源使用它。这是正确的做法吗?
我一直在查看 Jersey 中包含的示例,但还没有找到任何说明如何执行这些操作的内容。如果重要的话,我正在使用 Jersey 和 Jackson 来执行 Object->JSON,而不需要 XML 步骤,并且没有真正使用 JAX-RS。
I'm using Jersey to implement a RESTful API that is primarily retrieve and serve JSON encoded data. But I have some situations where I need to accomplish the following:
- Export downloadable documents, such as PDF, XLS, ZIP, or other binary files.
- Retrieve multipart data, such some JSON plus an uploaded XLS file
I have a single-page JQuery-based web client that creates AJAX calls to this web service. At the moment, it doesn't do form submits, and uses GET and POST (with a JSON object). Should I utilize a form post to send data and an attached binary file, or can I create a multipart request with JSON plus binary file?
My application's service layer currently creates a ByteArrayOutputStream when it generates a PDF file. What is the best way to output this stream to the client via Jersey? I've created a MessageBodyWriter, but I don't know how to use it from a Jersey resource. Is that the right approach?
I've been looking through the samples included with Jersey, but haven't found anything yet that illustrates how to do either of these things. If it matters, I'm using Jersey with Jackson to do Object->JSON without the XML step and am not really utilizing JAX-RS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我设法通过扩展 StreamingOutput 对象来获取 ZIP 文件或 PDF 文件。下面是一些示例代码:
PDFGenerator 类(我自己的用于创建 PDF 的类)从 write 方法获取输出流并写入该输出流,而不是写入新创建的输出流。
不知道这是否是最好的方法,但它确实有效。
I managed to get a ZIP file or a PDF file by extending the
StreamingOutput
object. Here is some sample code:The PDFGenerator class (my own class for creating the PDF) takes the output stream from the write method and writes to that instead of a newly created output stream.
Don't know if it's the best way to do it, but it works.
我必须返回一个 rtf 文件,这对我有用。
I had to return a rtf file and this worked for me.
我使用此代码将球衣中的 excel (xlsx) 文件 ( Apache Poi ) 作为附件导出。
I'm using this code to export excel (xlsx) file ( Apache Poi ) in jersey as an attachement.
这是另一个例子。我正在通过 ByteArrayOutputStream 创建 QRCode 作为 PNG。资源返回一个 Response 对象,流的数据是实体。
为了说明响应代码处理,我添加了缓存标头的处理(
If-modified-since
、If-none-matches
等)。请不要打败我,以防
stream.toByteArray()
内存不足 :) 它适用于我的 <1KB PNG 文件...Here's another example. I'm creating a QRCode as a PNG via a
ByteArrayOutputStream
. The resource returns aResponse
object, and the stream's data is the entity.To illustrate the response code handling, I've added handling of cache headers (
If-modified-since
,If-none-matches
, etc).Please don't beat me up in case
stream.toByteArray()
is a no-no memory wise :) It works for my <1KB PNG files...我一直按以下方式编写 Jersey 1.17 服务:
FileStreamingOutput
GET
还有客户端,如果您需要的话:
客户端
I have been composing my Jersey 1.17 services the following way:
FileStreamingOutput
GET
And the client, if you need it:
Client
此示例展示了如何通过 REST 资源在 JBoss 中发布日志文件。请注意,get 方法使用 StreamingOutput 接口来流式传输日志文件的内容。
}
This example shows how to publish log files in JBoss through a rest resource. Note the get method uses the StreamingOutput interface to stream the content of the log file.
}
使用 Jersey 2.16 文件下载非常简单。
以下是 ZIP 文件的示例
Using Jersey 2.16 File download is very easy.
Below is the example for the ZIP file
我发现以下内容对我很有帮助,我想分享一下,以防它对您或其他人有帮助。我想要类似 MediaType.PDF_TYPE 的东西,它不存在,但这段代码做了同样的事情:
参见
http://jersey.java.net/nonav/apidocs/1.1.0-ea/contribs/jersey-multipart/com/sun/jersey/multipart/file/DefaultMediaTypePredictor.CommonMediaTypes.html
在我的如果我将 PDF 文档发布到另一个站点:
然后 p 作为第二个参数传递给 post()。
此链接对我将这段代码片段放在一起很有帮助:
http://jersey.576304.n2.nabble.com/Multipart-Post -td4252846.html
I found the following helpful to me and I wanted to share in case it helps you or someone else. I wanted something like MediaType.PDF_TYPE, which doesn't exist, but this code does the same thing:
See
http://jersey.java.net/nonav/apidocs/1.1.0-ea/contribs/jersey-multipart/com/sun/jersey/multipart/file/DefaultMediaTypePredictor.CommonMediaTypes.html
In my case I was posting a PDF document to another site:
Then p gets passed as the second parameter to post().
This link was helpful to me in putting this code snippet together:
http://jersey.576304.n2.nabble.com/Multipart-Post-td4252846.html
这对我来说效果很好
url:http://example.com/rest/muqsith/get-file?filePath =C:\Users\I066807\Desktop\test.xml
This worked fine with me
url:http://example.com/rest/muqsith/get-file?filePath=C:\Users\I066807\Desktop\test.xml
另一个示例代码,您可以将文件上传到 REST 服务,REST 服务压缩文件,客户端从服务器下载 zip 文件。
这是使用 Jersey 使用二进制输入和输出流的一个很好的例子。
https://stackoverflow.com/a/32253028/15789
这个答案是我在另一个帖子中发布的。希望这有帮助。
Another sample code where you can upload a file to the REST service, the REST service zips the file, and the client downloads the zip file from the server.
This is a good example of using binary input and output streams using Jersey.
https://stackoverflow.com/a/32253028/15789
This answer was posted by me in another thread. Hope this helps.