【问题标题】:java.io.FileNotFoundException when retrieving a url with umlauts in the filename检索文件名中带有变音符号的 url 时出现 java.io.FileNotFoundException
【发布时间】:2009-09-21 16:16:02
【问题描述】:

我正在尝试检索文件名中带有变音符号的 url,例如“http://somesimpledomain.com/some/path/überfile.txt”,但它给了我一个 java.io.FileNotFoundException。我怀疑远程服务器上的文件名是用 latin1 编码的,尽管我的 url 是 utf8。但是我尝试更改 url 的编码没有成功,我不知道如何进一步调试它。请帮忙!

代码如下:

   HttpURLConnection conn = null;
    try {
       conn = (HttpURLConnection) new URL(uri).openConnection();
       conn.setRequestMethod("GET");
    } catch (MalformedURLException ex) {}
    } catch (IOException ex){}

    // Filter headers
    int i=1;
    String hKey;
    while ((hKey = conn.getHeaderFieldKey(i)) != null) {
        conn.getHeaderField(i);
        i++;
    }

    // Open the file and output streams
    InputStream in = null;
    OutputStream out = null;
    try {
        in = conn.getInputStream();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    try {
        out = response.getOutputStream();
    } catch (IOException ex) {
}

问候, 亨德里克

【问题讨论】:

  • 如果您尝试从浏览器访问相同的 URL,服务器是否会以 404 响应?

标签: java url encoding filenotfoundexception


【解决方案1】:

URL 需要正确编码。你必须知道你的服务器期望什么字符集/编码。你可以先试试这个,

 String uri = "http://somesimpledomain.com/some/path/" + 
     URLEncoder.encode(filename, "ISO-8859-1");

如果这不起作用,请将“ISO-8859-1”替换为“UTF-8”,然后重试。

如果这也不起作用,则文件不存在:)

【讨论】:

  • 我希望能解决这个问题,但你是对的:效果很好!非常感谢!
【解决方案2】:

您是否尝试过对其进行 urlencoding?例如。

%FCberfile

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多