简体   繁体   中英

APK file cannot be downloaded completely in android browser ,but can be download successfully in PC from my same web server?

Question:
My apk file can not be downloaded completely from anyone android browser, but can downloaded successfully at PC's browser. Actually, My apk file has 5.9 MB, but it only can be downloaded 1.2KB in total. Therefore, I got the 'analyzed failed' error.

Web server: linux + tomcat 7.x + jdk1.7 , and it was set apk mime type in tomcat server web.xml.
Web app: spring 4.0.2 + spring mvc + mybatis,

test link: http://127.0.0.1:8080/testapk/appstore/download
download function :

 @RequestMapping(value = "/appstore/download", method = RequestMethod.GET)
public ResponseEntity<byte[]> download() throws IOException {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    //Linux env.
    File file = new File("/usr/appstore/test.apk");
    if (!file.exists()) {
        //test env. windows
        file = new File("D:/test.apk");
        if(!file.exists()){
            throw new FileNotFoundException("Oops! can not find app file.");
        }
    }
    String fileName = FilenameUtils.getName(file.getAbsolutePath());
    //
    fileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");
    headers.setContentDispositionFormData("attachment", fileName);
    //
    return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
            headers, HttpStatus.CREATED);
}

I solved it by following Bradford200's advice. I think the reason was that I had not add the annotation of produces="application/apk , or the reason was that I had not add the other headers, and my new code at below:

@RequestMapping(value = "/appstore/download", method = RequestMethod.GET, produces="application/apk")
public ResponseEntity<InputStreamResource> download() throws IOException {

    File file = new File("/usr/appstore/test.apk");
    if (!file.exists()) {
        file = new File("D:/test.apk");
        if(!file.exists()) {
            throw new FileNotFoundException("Oops! File not found");
        }
    }

    InputStreamResource isResource = new InputStreamResource(new FileInputStream(file));
    FileSystemResource fileSystemResource = new FileSystemResource(file);
    String fileName = FilenameUtils.getName(file.getAbsolutePath());
    fileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    headers.add("Pragma", "no-cache");
    headers.add("Expires", "0");
    headers.setContentLength(fileSystemResource.contentLength());
    headers.setContentDispositionFormData("attachment", fileName);
    return new ResponseEntity<InputStreamResource>(isResource, headers, HttpStatus.OK);
}

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