亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java - gz 存檔下載為八位字節流

Java - gz 存檔下載為八位字節流

LEATH 2021-10-27 10:05:02
我在下載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;}
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號