File file = new File(http://xxxxxx);
为什么最后输出file的路径为http:\xxxxx
File file = new File(http:////xxxxxx) 输出的路径还是http:\xxxx导致无法访问指定文件。
为什么,输出的总是“\”呢,有什么办法解决么,小弟在此先谢过大家了!

解决方案 »

  1.   

    http:////xxxxxx   ??????
      

  2.   

    比如我D盘下有一个hello.txt文件.
    File file = new File("c:/hello.txt");
    这样就可以访问了.  你的http://xxxxxx 不需要用 " " 引起来?
      

  3.   

    我看了一下api,File貌似只支持file的schema
      

  4.   


    package test;import java.io.BufferedInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;public class DownloadFile { public static void getSong(String _path, String _savePath) {
    String savePath = _savePath;
    String path = _path;
    int BYTE_SIZE = 1;
    int SAVE_SIZE = 1024;
    byte[] buff = new byte[BYTE_SIZE]; // 每次读的缓存
    byte[] save = new byte[SAVE_SIZE]; // 保存前缓存
    BufferedInputStream bf = null;
    FileOutputStream file;
    URL url = null;
    HttpURLConnection httpUrl;
    try {
    url = new URL(path);
    httpUrl = (HttpURLConnection) url.openConnection();
    System.out.println("已经打开连接....");
    bf = new BufferedInputStream(httpUrl.getInputStream());
    System.out.println("已经获取资源......");
    file = new FileOutputStream(savePath);
    System.out.println("准备保存到:" + savePath); System.out.println("开始读入......");
    int i = 0;
    while (bf.read(buff) != -1) { // 一个字节一个字节读
    save[i] = buff[0];
    if (i == SAVE_SIZE - 1) { // 达到保存长度时开始保存
    file.write(save, 0, SAVE_SIZE);
    save = new byte[SAVE_SIZE];
    i = 0;
    } else {
    i++;
    }
    }
    // 最后这段如果没达到保存长度,需要把前面的保存下来
    if (i > 0) {
    file.write(save, 0, i - 1);
    }
    System.out.println("下载成功!!!");
    httpUrl.disconnect();
    file.close();
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try { bf.close(); } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } public static void main(String[] args) {
    try {
    DownloadFile.getSong(
    "http://wap.sonyericsson.com/UAprof/K700cR201.xml",
    "D://1.xml");
    DownloadFile.getSong(
    "http://nds1.nds.nokia.com/uaprof/NN78-1r100.xml",
    "D://2.xml");
    DownloadFile.getSong("http://www.sohu.com", "D://3.xml");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }
    }
      

  5.   

    是说构造,构建File不能http schema吧(水平有限,个人理解)io当然任何url都可以了
      

  6.   

    用这个构造方法
    File(URI uri) 
      

  7.   

    public File(URI uri)通过将给定的 file: URI 转换成一个抽象路径名来创建一个新的 File 实例。 
    file: URI 的具体形式与系统有关,因此,由此构造方法执行的转换也与系统有关。 对于某个给定抽象路径名 f,可以保证: new File( f.toURI()).equals( f.getAbsoluteFile())
    只要原始抽象路径名、URI 和新的抽象路径名都是在同一 Java 虚拟机(或者它的不同调用)中创建的。但是,当在某一操作系统上的虚拟机中创建的 file: URI 在不同操作系统上的虚拟机中被转换成抽象路径名时,这种关系通常是不成立的。