我写了个方法.请问我要怎么改才能把指定的文件名通过IO.复制到指定的文件夹里啊谢谢...没分了
public void read(String fileName) {
try {
URL ur = new URL("http://localhost:8080/ftpdowns/MyServlet");
HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(fileName);
byte[] bs = new byte[1024];
int len;
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

解决方案 »

  1.   

    就是读到你fileName指定的路径,如果你的fileName指定的路径出错,
    他会报    FileNotFoundException
      

  2.   

    OutputStream os = new FileOutputStream(fileName);fileName  -- 这里呗
      

  3.   

    说错了....那里面有许多文件..我要指定一个文件名通过outputstream传输该怎么办啊
      

  4.   

    就是我要通过outputstream一个指定的文件..
      

  5.   

    如果像上面这样写是不是会把URL里的文件全部下载到本地..我的意思是说我要指定一个文件名下载到本地
      

  6.   

    首先,你应该去看下FileOutputStream的构造器
    肯定没有FileOutputStream(String)
    很明显,你需要这样FileOutputStream(new File(String))
      

  7.   

    OutputStream os = new FileOutputStream(new File(fileName, filePath));
    是这样吧...把名字为fileName的文件下载到filePath
      

  8.   

    public FileOutputStream(String name)
                     throws FileNotFoundException创建一个向具有指定名称的文件中写入数据的输出文件流。创建一个新 FileDescriptor 对象来表示此文件连接。 
    首先,如果有安全管理器,则用 name 作为参数调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它,则抛出 FileNotFoundException。 
    参数:
    name - 与系统有关的文件名 
    抛出: 
    FileNotFoundException - 如果文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它 
    SecurityException - 如果存在安全管理器,且其 checkWrite 方法拒绝对文件进行写入访问。
    另请参见:
    SecurityManager.checkWrite(java.lang.String)
      

  9.   

    public void read(String filePath,String fileName) {
    try {
    URL ur = new URL("http://localhost:8080/ftpdowns/MyServlet");
    HttpURLConnection conn = (HttpURLConnection) ur.openConnection();
    conn.connect();
    InputStream is = conn.getInputStream();
    OutputStream os = new FileOutputStream(new File(fileName, filePath));
    byte[] bs = new byte[1024];
    int len;
    while ((len = is.read(bs)) != -1) {
    os.write(bs, 0, len);
    }
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    现在可以了..但是又有一个问题了...
    比如我的URL里有两个文件 aa.txt bb.txt
    当我指定下载aa.txt的时候也可以下载到指定的目录..但是打开aa.txt里面的内容怎么也变成了aa.txt bb.txt
      

  10.   


    你的这个就相关于在网页上点  另存为 
    然后 fileName 就是保存的位置你到底有什么问题啊?