简体   繁体   中英

Download file from HTTPS in browser on android

I'm trying to download a file using browser on android mobile from my web app (in my case I use spring mvc however despite on this because I think it's a common problem not framework specific). The thing is that trying with chrome or native browser in android device the file download never ends, always appears In progress or queue (depends on android version, I try with 4.1.1 and 5.1.1 ) on download app.

I trying changing typical http-headers involved: Content-Type , Content-Disposition with inline or attachment, specifying Content-Length with no luck.

The thing is that with desktop browsers in windows or linux the files download correctly in each case.

Some of my attempts:

  1. Using 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. Using directly org.springframework.web.bind.annotation.ResponseBody to return the docs bytes[] , and produces = "application/pdf" on @RequestMapping :

     @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. Or writing the file in the javax.servlet.http.HttpServletResponse outputstream and specifying the headers:

     @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(); } 

The thing is as I said that 3 cases work well on desktop browser on windows/linux however not on chrome and native browser for android devices.

I post the answer because I expend some time finding the cause of my problem and I hope that this can help someone.

The problem is finally caused by the server certificate where my web app is deployed, the thing is that my server certificate is issued by a pre production certificate authority which has an intermediate certificate chain hashed with a weak algorithm (md5). Also its CA is from pre production so surley it's not trusted for android.

In desktop the file downloads correctly because previously of the download I skip the page which is warning about the server certificate problem.

The thing is that on android, at least using chrome the warning page about server certificate also appears and I skip it, however then in download manager the file appears on queue or in progress indefinitely but there is no error message. I expend some time with this problem because I don't have any error message or indication about what is happening.

Finally I change my server certificate and I use a correct one issued by a trusted CA and files start to download correctly for the three code approaches specified on the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM