I have a website using Java Servlets.
我有一个使用Java Servlets的网站。
On my main html page I have a link that should start the download of a file from the server.
在我的主html页面上,我有一个链接,应该从服务器开始下载文件。
I have this in my html:
我的html中有这个:
<a href="/download(file_id=1)">Download</a>
And this in my download Controller:
这在我的下载控制器中:
Integer file_id = Integer.valueOf(request.getParameter("file_id"));
try{
if(file_id == 1){
File f = new File("/home/me/myfile.mp3");
// TODO
}
}catch(Exception){}
And I'm stuck here. How can I continue so that the client will receive the file and start downloading it?
我被困在这里。如何继续以便客户端收到文件并开始下载?
So far all I could find in search engines are php tutorials, which I'm not using.
到目前为止,我在搜索引擎中找到的只是php教程,我没有使用它。
1 个解决方案
#1
1
I have found some examples that should give you an idea of how to do this.
我找到了一些例子,可以让你知道如何做到这一点。
java-code-to-download-a-file-from-server This is from SO.
java-code-to-download-a-file-from-server这是来自SO。
example-of-downloading-file-from-the-server-in-servlet
This a more complex example: servlet-upload-file-and-download-file-example
这是一个更复杂的例子:servlet-upload-file-and-download-file-example
In all the examples you will find that some sort of stream class is used with a response object.
在所有示例中,您会发现某种流类与响应对象一起使用。
Furthermore I think your code should look more like:
此外,我认为您的代码看起来应该更像:
<a href="/download?file_id=1">Download</a>
Use an extension if needed after 'download'. If you are going to use the case statement to find the right file it would also be better to use a GUID to find the file, but if security is no concern why not use the proper file name?
如果需要,请在“下载”后使用扩展程序。如果您要使用case语句来查找正确的文件,最好使用GUID来查找文件,但如果不考虑安全性,为什么不使用正确的文件名呢?
BTW I used the following words to search on google:
顺便说一下,我使用以下单词在谷歌上搜索:
java download file from server example html page
来自服务器示例html页面的java下载文件
with the last two word only added later to see if I could find a better example, but it mainly returned the same results.
最后两个单词只是稍后添加,看看我是否能找到一个更好的例子,但它主要返回相同的结果。
I don't have a Java development environment so I can't create a working example for you. You should be able to work this out.
我没有Java开发环境,所以我无法为您创建一个工作示例。你应该能够解决这个问题。
#1
1
I have found some examples that should give you an idea of how to do this.
我找到了一些例子,可以让你知道如何做到这一点。
java-code-to-download-a-file-from-server This is from SO.
java-code-to-download-a-file-from-server这是来自SO。
example-of-downloading-file-from-the-server-in-servlet
This a more complex example: servlet-upload-file-and-download-file-example
这是一个更复杂的例子:servlet-upload-file-and-download-file-example
In all the examples you will find that some sort of stream class is used with a response object.
在所有示例中,您会发现某种流类与响应对象一起使用。
Furthermore I think your code should look more like:
此外,我认为您的代码看起来应该更像:
<a href="/download?file_id=1">Download</a>
Use an extension if needed after 'download'. If you are going to use the case statement to find the right file it would also be better to use a GUID to find the file, but if security is no concern why not use the proper file name?
如果需要,请在“下载”后使用扩展程序。如果您要使用case语句来查找正确的文件,最好使用GUID来查找文件,但如果不考虑安全性,为什么不使用正确的文件名呢?
BTW I used the following words to search on google:
顺便说一下,我使用以下单词在谷歌上搜索:
java download file from server example html page
来自服务器示例html页面的java下载文件
with the last two word only added later to see if I could find a better example, but it mainly returned the same results.
最后两个单词只是稍后添加,看看我是否能找到一个更好的例子,但它主要返回相同的结果。
I don't have a Java development environment so I can't create a working example for you. You should be able to work this out.
我没有Java开发环境,所以我无法为您创建一个工作示例。你应该能够解决这个问题。