简体   繁体   English

APK文件无法在android浏览器中完全下载,但可以从同一Web服务器在PC中成功下载吗?

[英]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. 我的apk文件无法从任何Android浏览器中完全下载,但可以在PC浏览器中成功下载。 Actually, My apk file has 5.9 MB, but it only can be downloaded 1.2KB in total. 实际上,我的apk文件有5.9 MB,但总共只能下载1.2KB。 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服务器: linux + tomcat 7.x + jdk1.7,它是在tomcat服务器web.xml中设置的apk mime类型。
Web app: spring 4.0.2 + spring mvc + mybatis, Web应用程序: spring 4.0.2 + spring mvc + mybatis,

test link: http://127.0.0.1:8080/testapk/appstore/download 测试链接: http://127.0.0.1:8080/testapk/appstore/download : http://127.0.0.1:8080/testapk/appstore/download : 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. 我按照Bradford200的建议解决了。 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: 我认为原因是我没有添加produces="application/apk的注释,或者原因是我没有添加其他标头,以及下面的新代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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