Return a file using Java Jersey(使用 Java Jersey 返回文件)
问题描述
我正在使用 Java Jersey 来实现 REST 服务.我的服务应该提供的一件事是文件下载选项.这些文件很大,是由 db 中的数据构成的.
I am using the Java Jersey to implement a REST service. One thing my service should provide is a file download option. These files are quite big and are constructed from data from db.
目前我正在从数据库中获取所有数据并将其保存到文件中并返回一个
Currently I am fetching all data from the db and saving it to a file and returning a
Response.ok().entity(new FileInputStream(file)).build();
有没有一种方法可以在不从 db 完全下载数据的情况下开始提供文件,但是由于数据来自 db,所以将其附加到输出流中?
Is there a way how I can start serving the file without fully downloading the data from db, but as the data comes from db append it to the Output stream ?
推荐答案
怎么样
File fileToSend = getFile();
return Response.ok(fileToSend, "application/zip").build();
可以设置媒体类型以匹配正在发送的文件.
The media type can be set to match the file being sent.
这看起来很简单,但更重要的是,阅读本文的 Java 专家是否发现该解决方案存在性能问题?
This looks pretty straight forward but more importantly, do java experts reading this see a performance problem with the solution?
这篇关于使用 Java Jersey 返回文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Java Jersey 返回文件


基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01