haishangfeiji
2013-04-08 15:42
浏览 1.1k
已采纳

java中File的声明使用URI报错

想用网络资源写服务器

[list]
[*][code="java"]URI ui = new URI("http://xxx.xxxxxxx.com/xxxxxxxx/文件名.xml");
[*] File f = new File(ui);
[*]

[*] FileInputStream is = new FileInputStream(f) ;
[*] BufferedReader reader = new BufferedReader(new java.io.InputStreamReader(is,"utf-8"));
[*]

[*] String inputLine = null;
[*] while ((inputLine = reader.readLine()) != null) {
[*] System.out.println(inputLine);
[*] }[/code]
[/list]

报错如下

[code="java"]Exception in thread "main" java.lang.IllegalArgumentException: URI scheme is not "file"
at java.io.File.(Unknown Source)
at something.Test1.main(Test1.java:19)[/code]

这是和解?我直接在网页输入该URI可以访问到

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • yunzhu666 2013-04-08 15:55
    已采纳

    用这种方式:
    [code="java"]URL url = new URL("http://127.0.0.1:8080/cfStruts2Ex2/");
    URLConnection urlConn = url.openConnection();

    System.out.println("Date: " + new Date(urlConn.getDate()));
    System.out.println("Content-Type: " + urlConn.getContentType());

    int length = urlConn.getContentLength();
    System.out.println("Content-Lentgth: " + length);

    if (length > 0) {
    System.out.println("========== Content ==========");
    InputStream input = urlConn.getInputStream();

    int i = length;
    int c;
    while ((c = input.read()) != -1 && --i > 0) {
        System.out.print((char) c);
    }
    input.close();
    

    } else {
    System.out.println("No Content.");
    }[/code]

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • jinnianshilongnian 2013-04-08 16:00

    File f = new File(ui); 访问的是file协议

    你现在URL("http://……") 怎么能拿到文件呢?

    直接使用new HttpURLConnection().getInputStream()

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题