从gwt中的url下载文件

从gwt中的url下载文件,gwt,download,Gwt,Download,我想从互联网上下载一个文件,我有那个文件的url。所以我写了一个下载servlet: public类DownloadServlet扩展了HttpServlet{ public void doGet(HttpServletRequest请求、HttpServletResponse响应) 抛出IOException{ 字符串pathToDownload=request.getParameter(“url”); URL URL=新URL(路径下载); URLConnection uc=url.open

我想从互联网上下载一个文件,我有那个文件的url。所以我写了一个下载servlet:

public类DownloadServlet扩展了HttpServlet{
public void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出IOException{
字符串pathToDownload=request.getParameter(“url”);
URL URL=新URL(路径下载);
URLConnection uc=url.openConnection();
字符串contentType=uc.getContentType();
int contentLength=uc.getContentLength();
InputStream is=uc.getInputStream();
response.setContentType(contentType);
//相应的setHeader(“内容处置”、“附件;文件名*=\“utf-8”+”文件名+”);
ServletOutputStream os=response.getOutputStream();
字节[]b=新字节[2048];
整数长度;
而((长度=is.read(b))!=-1){
os.write(b,0,长度);
}
is.close();
os.close();
}
}
在这里,我想显示弹出窗口时,用户点击文件是否保存或不这样做

resp.setHeader(“内容处置”、“附件;文件名*=\“utf-8”+文件名+”);

但是我希望文件名与internate上的文件名相同,所以在上面的代码段中还需要什么?

将子字符串从最后一个“/”剪切到URL字符串的末尾-这是您的文件名

String disposition = httpConn.getHeaderField("Content-Disposition");
            if (disposition != null) {
                // extracts file name from header field

                int index = disposition.indexOf("filename=");
                if (index != 0) {
                    fileName = disposition.substring(index + 9,
                            disposition.length());
                }
            } else {
                // extracts file name from URL

                fileName = link.substring(link.lastIndexOf("/") + 1,
                        link.length());
            }