从 REST Web 服务向客户端发送文件的正确方法是什么? [英] what's the correct way to send a file from REST web service to client?
问题描述
我刚刚开始开发 REST 服务,但遇到了一个困难的情况:将文件从我的 REST 服务发送到我的客户端.到目前为止,我已经掌握了如何发送简单数据类型(字符串、整数等)的窍门,但发送文件是另一回事,因为文件格式太多,我什至不知道应该从哪里开始.我的 REST 服务是在 Java 上制作的,我使用的是 Jersey,我使用 JSON 格式发送所有数据.
I've just started to develop REST services, but I've come across a difficult situation: sending files from my REST service to my client. So far I've gotten the hang of how to send simple data types (strings, integers, etc) but sending a file is a different matter since there are so many file formats that I don't know where I should even begin. My REST service is made on Java and I'm using Jersey, I'm sending all the data using the JSON format.
我读过有关 base64 编码的文章,有人说这是一种很好的技术,也有人说这不是因为文件大小问题.正确的方法是什么?这是我项目中一个简单的资源类的外观:
I've read about base64 encoding, some people say it's a good technique, others say it isn't because of file size issues. What is the correct way? This is how a simple resource class in my project is looking:
import java.sql.SQLException;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;
import com.mx.ipn.escom.testerRest.dao.TemaDao;
import com.mx.ipn.escom.testerRest.modelo.Tema;
@Path("/temas")
public class TemaResource {
@GET
@Produces({MediaType.APPLICATION_JSON})
public List<Tema> getTemas() throws SQLException{
TemaDao temaDao = new TemaDao();
List<Tema> temas=temaDao.getTemas();
temaDao.terminarSesion();
return temas;
}
}
我猜发送文件的代码类似于:
I'm guessing the code for sending a file would be something like:
import java.sql.SQLException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/resourceFiles")
public class FileResource {
@GET
@Produces({application/x-octet-stream})
public File getFiles() throws SQLException{ //I'm not really sure what kind of data type I should return
// Code for encoding the file or just send it in a data stream, I really don't know what should be done here
return file;
}
}
我应该使用什么样的注解?我看到有些人推荐使用 @Produces({application/x-octet-stream})
的 @GET
,这是正确的方法吗?我发送的文件是特定的,因此客户端不需要浏览文件.谁能指导我如何发送文件?我应该使用 base64 对其进行编码以将其作为 JSON 对象发送吗?或者编码不需要将其作为 JSON 对象发送?感谢您提供的任何帮助.
What kind of annotations should I use? I've seen some people recommend for a @GET
using @Produces({application/x-octet-stream})
, is that the correct way? The files I'm sending are specific ones so the client doesn't need to browse through the files. Can anyone guide me into how am I supposed to send the file? Should I encode it using base64 to send it as a JSON object? or the encoding isn't necessary to send it as a JSON object? Thanks for any help you may give.
推荐答案
我不建议在 base64 中编码二进制数据并将其包装在 JSON 中.它只会不必要地增加响应的大小并减慢速度.
I don't recommend encoding binary data in base64 and wrapping it in JSON. It will just needlessly increase the size of the response and slow things down.
使用 GET 和 application/octect-stream
使用 javax.ws.rs.core.Response
(JAX-RS API 的一部分,因此您不会陷入困境泽西岛):
Simply serve your file data using GET and application/octect-stream
using one of the factory methods of javax.ws.rs.core.Response
(part of the JAX-RS API, so you're not locked into Jersey):
@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile() {
File file = ... // Initialize this to the File path you want to serve.
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename="" + file.getName() + """ ) //optional
.build();
}
如果您没有实际的 File
对象,而是一个 InputStream
,Response.ok(entity, mediaType)
应该也能处理.
If you don't have an actual File
object, but an InputStream
, Response.ok(entity, mediaType)
should be able to handle that as well.
这篇关于从 REST Web 服务向客户端发送文件的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!