java 使用 Servlet 下载文件时如何使用 GWT?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2937126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use GWT when downloading Files with a Servlet?
提问by molleman
I am creating a simple project that will allow me to upload and download files using gwt. i am having trouble with the downloading of files that are on my server.
我正在创建一个简单的项目,它将允许我使用 gwt 上传和下载文件。我在下载服务器上的文件时遇到问题。
For the file upload i used http://code.google.com/p/gwtupload/and followed the instructions there. My file is stored on the server outside of the website container(on the hard drive),
对于文件上传,我使用了http://code.google.com/p/gwtupload/并按照那里的说明进行操作。我的文件存储在网站容器之外的服务器上(在硬盘上),
Now when it comes to the downloading of a file, i want a user to press a download button and whatever item is currently selected will download. i dont really know how this will be done
现在,在下载文件时,我希望用户按下下载按钮,当前选择的任何项目都将下载。我真的不知道这将如何完成
i know i need a download servlet
我知道我需要一个下载 servlet
public class DownloadAttachmentServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String fileName = (String) req.getSession().getAttribute("fileName");
YFUser user = (YFUser) req.getSession().getAttribute(TestServiceImpl.SESSION_USER);
if (user == null)
throw new ServletException("Invalid Session");
InputStream in = null;
OutputStream out = resp.getOutputStream();
FileInputStream fIn = new FileInputStream(fileName);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
}
for the moment i will just pass a fileName string to retrieve the file for testing
目前我将只传递一个文件名字符串来检索文件进行测试
now i am lost at what to do on the client side, i have a simple
现在我迷失了在客户端做什么,我有一个简单的
public class DownloadFilePanel extends Composite {
public DownloadFilePanel(final YFUser user , final String fileName){
final Element downloadIframe = RootPanel.get("__download").getElement();
VerticalPanel content = new VerticalPanel();
content.add(new Label("Download For this File : " + fileName));
Button button = new Button("Download");
button.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
// i do not know what to do here
});
content.add(button);
initWidget(content);
}
}
above is a simple widget that will supply a panel that will allow for the download of a file based on a fileName
上面是一个简单的小部件,它将提供一个面板,允许根据文件名下载文件
as you can see above, i do not know what to do to be able to download the file
正如你在上面看到的,我不知道该怎么做才能下载文件
is there any one that can point me in the right direction?
有没有人可以指出我正确的方向?
采纳答案by Sripathi Krishnan
On the client side, just create a regular <a href="path/to/servlet">tag. You can use the Anchorclass if you want to dynamically create it. When the user clicks the link, the browser will automatically download the file.
在客户端,只需创建一个常规<a href="path/to/servlet">标签。Anchor如果要动态创建它,可以使用该类。当用户点击链接时,浏览器会自动下载文件。

