简体   繁体   English

发送文件更好或输出流

[英]Sending a File is better or a Output Stream

In my project we are saving some files. 在我的项目中,我们正在保存一些文件。 When a file is requested, I fetch the file from the HBase table and then send it to the browser. 请求文件时,我从HBase表中获取文件,然后将其发送到浏览器。

Data is saved in Bytes in HBase. 数据保存在HBase中的字节中。 So when I fetch the file I am getting it in Bytes. 因此,当我获取文件时,我会在字节中获取它。

Now I have to convert these Bytes into either File object or in Output Stream. 现在我必须将这些字节转换为File对象或Output Stream。

What I want to know is whether send the file via a File object is better or Output Stream? 我想知道的是,通过File对象发送文件是更好还是输出流?

The file can vary from 1 MB to 10 MS's. 该文件可以从1 MB到10 MS不等。

I am using REST API to send data. 我正在使用REST API发送数据。

It is not entirely clear what you are doing, but the truth is that a java.io.File is just an abstraction over a path, and therefore it contains no data whatsoever. 目前还不完全清楚你在做什么,但事实是java.io.File只是一个路径上的抽象,因此它不包含任何数据。 As such, I do not think that sending a java.io.File object would help you in any way if what you want to do is sending the actual file contents. 因此,我不认为发送java.io.File对象会以任何方式帮助您,如果您要做的是发送实际的文件内容。

Neither it makes sense to send an OutputStream which is only a class to control the flow of bytes into a given sink. 发送一个OutputStream是不合理的,它只是一个类来控制字节流到给定的接收器中。 What you send are the bytes, not the stream (ie if your sink were a socket, then it makes sense to send the bytes of the contents of the file through the socket using the OutputStream associated with this given sink). 您发送的是字节,而不是流(即,如果您的接收器是套接字,那么使用与此给定接收器关联的OutputStream通过套接字发送文件内容的字节是有意义的)。

Sending a java.io.File object wont help as it would be a very java specific binary object and it will mandate that receiving system is in java. 发送java.io.File对象不会有帮助,因为它将是一个非常特定于java的二进制对象 ,它将强制接收系统在java中。

You mentioned, you are using REST . 你提到过,你正在使用REST Now this calls for something which is independent of any technology/platform . 现在,这需要一些独立于任何技术/平台的东西

One feasible approach would be as follows. 一种可行的方法如下。

  1. Let the rest API return a URL which points to the file user wish to download. 让其余的API返回一个指向用户希望下载的文件的URL。
  2. Write/Configure a Servlet for this URL. 为此URL 编写/配置Servlet
  3. When this servlet receives the request, it reads the database and writes to the response stream (output stream). 当此servlet收到请求时,它会读取数据库并写入响应流(输出流)。
  4. The user will get a prompt asking for file download. 用户将收到提示要求下载文件的提示。

Here is an example of PDF file download using a servlet . 以下是使用servlet 下载PDF文件示例

I'm assuming that the question is asking whether to write directly to an OuputStream or to save the byte[] to a local file first. 我假设问题是询问是直接写入OuputStream还是先将byte []保存到本地文件。 The only reason I could think of to save to a local file first would be to decouple the retrieval from HBase and the user's download. 我想到首先保存到本地文件的唯一原因是将检索与HBase和用户的下载分离。 If you expect each request that returns a byte[] from HBase to return the whole file to the user in the response, use OutputStream. 如果您希望每个从HBase返回byte []的请求将整个文件返回给响应中的用户,请使用OutputStream。

Having said that, retrieving 10 MB as a byte[] seems like it might consume a lot of memory per request. 话虽如此,检索10 MB作为byte []似乎每次请求可能会占用大量内存。 You might want to see if you can get HBase to give you an InputStream so you don't need up to 10 MB RAM per request. 您可能想知道是否可以让HBase为您提供一个InputStream,因此每个请求最多不需要10 MB RAM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM