简体   繁体   English

下载的zip文件已损坏

[英]Downloaded zip file is corrupted

I'm trying to let user download a zip file from server using this code : SERVER: 我试图让用户使用以下代码从服务器下载zip文件:SERVER:

 @RequestMapping(value = "/get-shape-file", method = RequestMethod.GET)
public void getFile( HttpServletRequest request, HttpServletResponse response) throws IOException {
    BufferedReader rd = new BufferedReader (new FileReader("/shape/lastlyExportedFileName"));    
    String fileName = rd.readLine().trim();
    rd.close();
    try {            
        OutputStream myOut = null;
        FileInputStream fileInputStream = null;
        File downzip = new File("/shape/" + fileName);

        response.setContentType("TEXT/HTML");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        response.setContentLength((int) downzip.length());
        System.out.println("length  " + (int) downzip.length());
        //READ DATA FROM FILE

        byte[] dataRead = new byte[(int) downzip.length()];
        fileInputStream = new FileInputStream(downzip);
        fileInputStream.read(dataRead, 0, (int) downzip.length());
        //WRITE DATA TO OUTFILE
        myOut = response.getOutputStream();
        myOut.write(dataRead);

        if (fileInputStream != null) {
            fileInputStream.close();
        }

      } catch (IOException ex) {
        logger.error("Error writing file to output stream. Filename was '" + fileName + "'");
        throw new RuntimeException("IOError writing file to output stream");
      }
}

Client: 客户:

 $.ajax({
    url: 'url-to-the-method/get-shape-file',
    type: 'GET',
    success: function(shape) {
        console.log(shape);
        var a = document.createElement('a');
        a.href = 'data:attachment/zip,' + shape;
        a.target = '_blank';
        a.download = 'exported-shape-file.zip';
        document.body.appendChild(a);
        a.click();
    },
    error: function(data) {
        Message.error("Could not download shapefile");
    }
});       

But downloaded file is corrupted. 但是下载的文件已损坏。 Size is larger than it should be, also when trying to open the file in archive manager this message is shown: zipinfo: cannot find zipfile directory in one of /home/tengiz/Downloads/exported-shape-file (1).zip or /home/tengiz/Downloads/exported-shape-file (1).zip.zip, and cannot find /home/tengiz/Downloads/exported-shape-file (1).zip.ZIP, period. 大小大于应有的大小,当尝试在存档管理器中打开文件时,也会显示以下消息:zipinfo:在/ home / tengiz / Downloads / exported-shape-file(1).zip或其中之一中找不到zipfile目录。 / home / tengiz / Downloads / exported-shape-file(1).zip.zip,找不到/ home / tengiz / Downloads / exported-shape-file(1).zip.ZIP,句点。

I solved the problem, turns out that problem was on client side not on server side. 我解决了问题,结果发现问题出在客户端而非服务器端。 On client-side I was trying to attach the response to href but as response contained byte code it was loosing some important characters. 在客户端,我尝试将响应附加到href,但是由于响应包含字节码,因此失去了一些重要字符。 What you need to do is just create a tag with link to the controller method, so you don't need any ajax requests like this: 您需要做的只是创建一个带有controller方法链接的标签,因此您不需要任何像这样的ajax请求:

        var a = document.createElement('a');
        a.href = '/url-to/get-shape-file';
        document.body.appendChild(a);
        a.click();    

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

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