APK文件不能在android浏览器中完全下载,但可以从我的同一个网络服务器在PC中成功下载



问题:
我的apk文件不能完全从任何android浏览器下载,但可以在PC的浏览器上成功下载。事实上,我的apk文件有5.9 MB,但它总共只能下载1.2 KB。因此,我得到了"分析失败"的错误。

Web服务器:linux+tomcat 7.x+jdk1.7,并在tomcat服务器Web.xml中设置了apk-mime类型。
Web应用程序:spring4.0.2+springmvc+mybatis,

测试链接:http://127.0.0.1:8080/testapk/appstore/download
下载功能

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

我按照Bradford200的建议解决了这个问题。我认为原因是我没有添加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);
}

最新更新