gpt4 book ai didi

java - 在 Android 上的浏览​​器中从 HTTPS 下载文件

转载 作者:行者123 更新时间:2023-12-01 11:37:27 26 4
gpt4 key购买 nike

我正在尝试使用 Android 移动设备上的浏览器从我的网络应用程序下载文件(在我的例子中,我使用 spring mvc,尽管如此,因为我认为这是一个常见问题,而不是特定于框架的问题)。问题是,尝试在 Android 设备中使用 Chrome 或 native 浏览器时,文件下载永远不会结束,总是出现 In Progressqueue (取决于 Android 版本,我尝试使用 4.1.1)。 1 和 5.1.1 ) 下载应用程序。

我尝试更改涉及的典型http-headers:Content-TypeContent-Disposition以及内联或附件,指定Content -长度但运气不好。

问题是,使用 Windows 或 Linux 中的桌面浏览器,文件在每种情况下都能正确下载。

我的一些尝试:

  1. 使用org.springframework.http.ResponseEntity:

    @RequestMapping(value= {"/evidenciaDownload"},method = RequestMethod.GET)
    public ResponseEntity<byte[]> getEvidencia(
    @RequestParam(value = "paramsCoded", required = true) String paramsCoded) throws IOException {

    String url = configPropsService.getEvidenciaUrlDownload() + URLEncoder.encode(paramsCoded,"UTF-8");
    logger.info("[getEvidencia] Recuperem l'evidencia de : " + url);

    // bytes are correct
    byte[] evidencia = downloadFile(url);

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("Content-Type", "application/pdf");
    //responseHeaders.set("Content-Disposition","attachment; filename=\"evidencia.pdf\";");
    responseHeaders.set("Content-Disposition","inline; filename=evidencia.pdf");
    responseHeaders.setContentLength(evidencia.length);
    return new ResponseEntity<byte[]>(evidencia, responseHeaders,HttpStatus.CREATED);
    }
  2. 直接使用org.springframework.web.bind.annotation.ResponseBody返回文档bytes[],并且产生=“application/@RequestMapping 上的 pdf":

    @RequestMapping(value= {"/evidenciaDownload"},method = RequestMethod.GET, produces = "application/pdf")
    public @ResponseBody byte[] getEvidencia(
    @RequestParam(value = "paramsCoded", required = true) String paramsCoded) throws IOException {

    String url = configPropsService.getEvidenciaUrlDownload() + URLEncoder.encode(paramsCoded,"UTF-8");
    logger.info("[getEvidencia] Recuperem l'evidencia de : " + url);

    return downloadFile(url);

    }
  3. 或者将文件写入 javax.servlet.http.HttpServletResponse 输出流并指定 header :

    @RequestMapping(value= {"/documentDownload"},method = RequestMethod.GET)
    public void getDocument(
    @RequestParam(value = "paramsCoded", required = true) String paramsCoded,
    HttpServletResponse response) throws IOException {

    String url = configPropsService.getDocumentsNotficacioUrlDownload() + URLEncoder.encode(paramsCoded,"UTF-8");
    logger.info("[getDocument] Recuperem el document de : " + url);

    // bytes are correct
    byte[] downloadFile = downloadFile(url);

    ServletOutputStream outputStream = response.getOutputStream();

    int fileLength = IOUtils.copy(new ByteArrayInputStream(downloadFile), outputStream);
    response.setHeader("Content-Disposition","attachment;filename=\"evidencia.pdf\"");
    response.setContentLength(fileLength);
    response.setContentType("application/pdf");
    outputStream.flush();
    outputStream.close();
    }

事情正如我所说,3 个案例在 Windows/Linux 上的桌面浏览器上运行良好,但在 Chrome 和 Android 设备的 native 浏览器上运行不佳。

最佳答案

我发布答案是因为我花了一些时间查找问题的原因,我希望这可以帮助别人。

问题最终是由部署我的网络应用程序的服务器证书引起的,问题是我的服务器证书是由预生产证书颁发机构颁发的,该机构具有使用弱算法(md5)散列的中间证书链。此外,它的 CA 来自预生产,因此 Android 不信任它。

在桌面中,文件下载正确,因为之前下载时我跳过了有关服务器证书问题的警告页面。

问题是,在 Android 上,至少使用 chrome,还会出现有关服务器证书的警告页面,我跳过它,但是在下载管理器中,该文件无限期地出现在队列中或正在进行中,但没有错误消息。我花了一些时间来解决这个问题,因为我没有任何错误消息或有关正在发生的情况的指示。

最后,我更改了服务器证书,并使用由受信任的 CA 颁发的正确证书,并且开始针对问题中指定的三种代码方法正确下载文件。

关于java - 在 Android 上的浏览​​器中从 HTTPS 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29825061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com