【问题标题】:download file from url in gwt从 gwt 中的 url 下载文件
【发布时间】:2016-05-10 21:15:17
【问题描述】:

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

public class DownloadServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String pathToDownload = request.getParameter("url");

        URL url = new URL(pathToDownload);
        URLConnection uc = url.openConnection();
        String contentType = uc.getContentType();
        int contentLength = uc.getContentLength();
        InputStream is = uc.getInputStream();

        response.setContentType(contentType);
        // resp.setHeader("Content-Disposition", "attachment; filename*=\"utf-8''" + filename + "");
        ServletOutputStream os = response.getOutputStream();
        byte[] b = new byte[2048];
        int length;

        while ((length = is.read(b)) != -1) {
            os.write(b, 0, length);
        }
        is.close();
        os.close();
    }
}

我想在用户点击文件时显示弹出窗口是否保存,所以有

resp.setHeader("Content-Disposition", "attachment; filename*=\"utf-8''" + filename + "");

但我希望文件名与内部文件名相同,所以上面的 sn-p 还需要什么?

【问题讨论】:

    标签: gwt download


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      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());
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-30
        • 2016-07-23
        • 2023-04-01
        • 1970-01-01
        • 2011-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多