Java-gz归档文件作为八位字节流下载
我在下载gzip文件并用Java5将其保存到文件系统时遇到了一个问题。下载完成后,文件(具有正确的名称和扩展名)似乎具有不同的MIME类型。。。我无法从Linux服务器上解压它(使用Java-gz归档文件作为八位字节流下载,java,download,gzip,Java,Download,Gzip,我在下载gzip文件并用Java5将其保存到文件系统时遇到了一个问题。下载完成后,文件(具有正确的名称和扩展名)似乎具有不同的MIME类型。。。我无法从Linux服务器上解压它(使用gunzip),如果我尝试在Windows pc上用WinZip打开它,我会看到一个“递归”存档(像Matryoska玩偶)。如果我从服务器键入命令file*filename*.gz,它会将该文件识别为ascii文本。 相反,如果我尝试使用浏览器下载归档文件,一切都很顺利,我可以正确地打开和解压缩该文件(即使使用我的
gunzip
),如果我尝试在Windows pc上用WinZip打开它,我会看到一个“递归”存档(像Matryoska玩偶)。如果我从服务器键入命令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发送文件。它可以工作!我还需要将InputStream更改为GZIPInputStream干杯!很乐意帮忙。