gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:45:18 24 4
gpt4 key购买 nike

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

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

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

有人可以帮助我吗?谢谢!

最佳答案

写入文件时,您应该通过 java.util.zip.GZIPOutputStream 发送它。

关于Java - gz 存档作为八位字节流下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52627980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com