我在下載gzip文件并將其保存在 Java 5 的文件系統中時遇到問題。下載完成后,文件(具有正確的名稱和擴展名)似乎具有不同的 MIME 類型......我無法從我的 Linux 服務器(使用gunzip)解壓縮它,如果我嘗試在我的 Windows 電腦上使用 WinZip 打開它,我會看到一個“遞歸”存檔(如俄羅斯套娃)。如果我file *filename*.gz從服務器鍵入命令,它會將文件識別為ascii text. 相反,如果我嘗試使用瀏覽器下載存檔,一切順利,我可以正確打開和解壓縮文件(即使使用我的 Linux 服務器),現在它被識別為gzip compressed archive.這是我用來下載文件并保存的代碼。主.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;}
添加回答
舉報
0/150
提交
取消
