Java - gz 存档作为八位字节流下载
Java - gz archive downloaded as octet stream
我在下载 gzip 文件并使用 Java 将其保存在文件系统中时遇到问题 5. 下载完成后,文件 (具有正确的名称和扩展名)似乎具有不同的 MIME 类型...我无法从我的 Linux 服务器(使用 gunzip
)解压缩它,如果我尝试使用 WinZip 打开它在我的 Windows 电脑上,我看到一个 "recursive" 档案(就像一个俄罗斯套娃)。如果我从服务器键入命令 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;
}
有人可以帮助我吗?谢谢!
写入文件时,应通过 java.util.zip.GZIPOutputStream 发送。
我在下载 gzip 文件并使用 Java 将其保存在文件系统中时遇到问题 5. 下载完成后,文件 (具有正确的名称和扩展名)似乎具有不同的 MIME 类型...我无法从我的 Linux 服务器(使用 gunzip
)解压缩它,如果我尝试使用 WinZip 打开它在我的 Windows 电脑上,我看到一个 "recursive" 档案(就像一个俄罗斯套娃)。如果我从服务器键入命令 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;
}
有人可以帮助我吗?谢谢!
写入文件时,应通过 java.util.zip.GZIPOutputStream 发送。