简体   繁体   English

如何设置API(结构)以从服务器下载文件

[英]How to set up api(structure) for download a file from server

So I am making a program in java that would download a file from a server. 因此,我正在用Java编写一个程序,该程序将从服务器下载文件。 I have a server set up with bottle web framework. 我有一台装有Bottle Web框架的服务器。

Now I'm not sure how exactly I should transfer the data from the server to the client side. 现在,我不确定应该如何将数据从服务器传输到客户端。

Here are the two ideas I've thought of. 这是我想到的两个想法。

  1. Make the file I need to download as a static file on the server and just use the java to download it. 将我需要下载的文件制作为服务器上的静态文件,然后使用java进行下载。

  2. Send a post request to the server which replies with part of the data and I have to iterate through it until I have the full file. 向服务器发送一个发帖请求,该请求将回复部分数据,我必须遍历它直到获得完整的文件。

Number 1 would be the easiest but I guess anyone could access it that way so it might not be the best idea. 1号将是最简单的,但我想任何人都可以通过这种方式访问​​它,因此它可能不是最好的主意。

Do you guys have any suggestion on how I should structure it? 你们对我的结构有什么建议吗?

Assuming you are using Servlets or Servlet based frameworks. 假设您正在使用Servlet或基于Servlet的框架。

Write the file content to the response and set the Content-Disposition: attachment; filename=yourfilename 将文件内容写入响应并设置Content-Disposition: attachment; filename=yourfilename Content-Disposition: attachment; filename=yourfilename with the file name. Content-Disposition: attachment; filename=yourfilename的文件名以及文件名。 If needed you may need to set the Content-Transfer-Encoding: binary header as well. 如果需要,您可能还需要设置Content-Transfer-Encoding: binary标头。

For 2. A GET request can return the data. 对于2。GET请求可以返回数据。 More logical to use GET than POST as POST is used to upload files. 使用GET比使用POST更合乎逻辑,因为POST用于上传文件。 Then you can include the file content in the response with the content type also set to type. 然后,您可以将文件内容包括在响应中,并将内容类型也设置为type。

May be this will help you 也许这会帮助你

from bottle import static_file    
@route('/download/<filename:path>')
def download(filename):
   return static_file(filename, root='/path/to/static/files', download=filename)

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

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