Java-GZ存档下载为八位字节流

时间:2018-10-03 13:18:44

标签: java download gzip gz

我在下载 gzip 文件并将其保存在Java 5文件系统中时遇到问题。下载完成后,出现文件(具有正确的名称和扩展名)具有不同的MIME类型...我无法从Linux服务器上解压缩(使用gunzip),如果尝试在Windows PC上使用WinZip打开它,则会看到“递归”存档(例如一个俄罗斯套娃)。如果我从服务器键入命令file *filename*.gz,它将把文件识别为ascii text。 相反,如果我尝试使用浏览器下载档案,一切都会顺利进行,并且可以正确打开和解压缩文件(即使使用我的Linux服务器),现在将其识别为gzip compressed archive

这是我用来下载文件并保存的代码。

Main.java:

public class Main {

public static void main(String[] args) {

    String filePath = "";
    HttpOutgoingCall httpOngoingCall = null;
    httpOngoingCall = new HttpOutgoingCall();
    String endpointUrl = "https://myurl/myfile.gz";
    try {

        InputStream inputStream = httpOngoingCall.callHttps(endpointUrl);
        //I also tried with ZipInputStream and GZIPInputStream

        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

        filePath = parseAndWriteResponse(br, "myfile.gz", "C:\\");

        System.out.println(filePath);

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}


private static String parseAndWriteResponse(BufferedReader br, String fileName,
                                     String destPath) {
    String outputFileName = null;

    outputFileName = destPath + File.separator + fileName;

    String line;
    File outputFile = new File(outputFileName);
    FileWriter fileWriter = null;
    BufferedWriter bw = null;

    try {
        if (!outputFile.exists()) {
            outputFile.createNewFile();
        }
    } catch (IOException e1) {

    }

    try {
        fileWriter = new FileWriter(outputFile);
        bw = new BufferedWriter(fileWriter);
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.write("\n");
        }
    } catch (IOException e) {

    } finally {
        try {
            bw.close();
            fileWriter.close();
        } catch (IOException e) {

        }
    }
    return outputFileName;
}

HttpOutgoingCall.java:

public class HttpOutgoingCall {

private InputStream inStream = null;

private HttpsURLConnection httpsConnection = null;


private final static int CONNECTION_TIMEOUT = 20000;


public InputStream callHttps(String endpointUrl) throws Exception {

    String socksServer = "";
    String socksPort = "";

    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    Properties properties = System.getProperties();

    System.setProperty("java.protocol.handler.pkgs", "javax.net.ssl");
    java.security.Security
            .addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    if (!socksServer.equals("")) {
        if (System.getProperty("socksProxyHost") == null) {
            properties.put("socksProxyHost", socksServer);
        }
        if (!socksPort.equals("")) {
            if (System.getProperty("socksProxyPort") == null) {
                properties.put("socksProxyPort", socksPort);
            }
        }
    }
    System.setProperties(properties); 

    System.setProperty("java.protocol.handler.pkgs", "javax.net.ssl");
    java.security.Security
            .addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {

            return null;
        }

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };

    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection
                .setDefaultSSLSocketFactory(sc.getSocketFactory());

        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                return true;
            }
        };

        HttpsURLConnection.setDefaultHostnameVerifier(hv);

        httpsConnection = (HttpsURLConnection) (new URL(endpointUrl)).openConnection();

        httpsConnection.setDoOutput(true);
        httpsConnection.setUseCaches(false);

        httpsConnection.setConnectTimeout(CONNECTION_TIMEOUT);
        httpsConnection.setReadTimeout(CONNECTION_TIMEOUT);

        inStream = httpsConnection.getInputStream();

    } catch (Exception e) {}

    return inStream;
}

有人可以帮我吗?谢谢!

1 个答案:

答案 0 :(得分:1)

编写文件时,应通过java.util.zip.GZIPOutputStream发送文件。