压缩文件在服务器端正常,但在投放到客户端后已损坏

时间:2019-04-23 21:52:07

标签: java http zip nanohttpd

我正在尝试将一堆图像和csv文件压缩到服务器端的zip文件中,然后将其发送到浏览器中的客户端以进行下载。该zipfile在客户端可以正常工作,但在响应中提供给客户端的版本会给出错误21,这是解压缩后的目录。我在这里错过了什么?

尝试使用存档实用程序打开时遇到的错误是“错误21-是目录”

我在The Unarchiver中遇到的错误是存档文件不完整。

我在解压缩时遇到的错误是

jcl$ unzip compliment.zip 
Archive:  compliment.zip
??
caution:  zipfile comment truncated
error [compliment.zip]:  missing 3231972691 bytes in zipfile
  (attempting to process anyway)
error [compliment.zip]:  attempt to seek before beginning of zipfile
  (please check that you have transferred or created the zipfile in the
  appropriate BINARY mode and that you have compiled UnZip properly)

看起来它可能已损坏或未以二进制格式传输,因此我尝试将响应MIME类型设置为“ application / zip”和“ application / octet-stream”,并以相同的格式在Blob上接收它javascript客户端。但是我仍然遇到相同的错误。

服务器端

public Response serve(String uri, Method _method, Map<String, String> headers, Map<String, String> params, Map<String, String> formFields) {
    Method method = (Method) _method;
    Response response = null;
    try {
        if (Method.PUT.equals(method)) {
            if ("/".equals(uri)) {
                response = index();
            } else {
                File zipFile = getZipByName(params);
                FileInputStream fis = null;
                try {
                    if (zipFile.exists()) {
                        fis = new FileInputStream(zipFile);
                    } else
                        Log.d("FOF :", "File does not exist:");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                response = newChunkedResponse(Response.Status.OK, "application/zip", fis);
            }
            response.addHeader("Access-Control-Allow-Origin", "*");
            return response;
        }
    } catch (Exception e) {
        Log.d(TAG, "serve exception", e);
    }
}

客户端

function zip(name) {
    $.ajax({
        type: 'put',
        url: '/zip/name?' + $.param({name: name}),
        success: function (data) {
            save(name + ".zip", data, 'application/zip');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            reveal('ERROR: <br/><pre>' + jqXHR.responseText + '</pre>', 10000);
        }
    }).done(function(data){
        reveal(name + ".zip generated");
    });
}

function save(filename, data, dataType) {
    var blob = new Blob([data], {type: dataType});
    if(window.navigator.msSaveOrOpenBlob) { // IE10+
        window.navigator.msSaveBlob(blob, filename);
    }
    else{ // Others
        var elem = window.document.createElement('a');
        elem.href = window.URL.createObjectURL(blob);
        elem.download = filename;
        document.body.appendChild(elem);
        elem.click();
        document.body.removeChild(elem);
    }
}

预期结果是,在客户端下载zip文件后,可以将其解压缩且没有错误,并且内容与服务器端副本相同

0 个答案:

没有答案