【问题标题】:URL.getContent wrong encodingURL.getContent 编码错误
【发布时间】:2012-12-25 03:32:00
【问题描述】:

我有一个 URL,我正试图将其写入 linux 机器上的文件。
我可以做一个

      wget http://localhost/fileIwant.text  

一切都很好。

当我尝试像这样使用 Java 的 URL 时:

    URL url = new URL("http://localhost/fileIwant.text");
    URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String inputLine;
    File file = new File(path+filename);

    if (!file.exists()) {
        file.createNewFile();
    }

    //use FileWriter to write file
    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);

    while ((inputLine = br.readLine()) != null) {
        bw.write(inputLine);
    }

我已经尝试了所有我能想到的阅读器、流、字节 [] 的排列,但我仍然得到一个带有菱形的文件?在他们里面。

有什么想法吗?

【问题讨论】:

    标签: java linux io


    【解决方案1】:
    InputStream in=(new URL("http://localhost/fileIwant.text")).openStream();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 2012-06-02
      • 2019-02-23
      • 2012-11-13
      • 2015-02-04
      • 1970-01-01
      相关资源
      最近更新 更多