将文件从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;
}
}
我应该使用哪种注释?我见过一些人推荐 @GET
使用 @Produces({application / x-octet-stream})
,这是正确的方法吗?我发送的文件是特定文件,因此客户端不需要浏览文件。任何人都可以指导我如何发送文件?我应该使用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 <提供文件数据/ code>使用 <$ c的工厂方法之一$ c> 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();
}
如果你没有实际的文件
object,但是 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屋!