我写了个方法.请问我要怎么改才能把指定的文件名通过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();
}
}
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();
}
}
他会报 FileNotFoundException
肯定没有FileOutputStream(String)
很明显,你需要这样FileOutputStream(new File(String))
是这样吧...把名字为fileName的文件下载到filePath
throws FileNotFoundException创建一个向具有指定名称的文件中写入数据的输出文件流。创建一个新 FileDescriptor 对象来表示此文件连接。
首先,如果有安全管理器,则用 name 作为参数调用 checkWrite 方法。 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它,则抛出 FileNotFoundException。
参数:
name - 与系统有关的文件名
抛出:
FileNotFoundException - 如果文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它
SecurityException - 如果存在安全管理器,且其 checkWrite 方法拒绝对文件进行写入访问。
另请参见:
SecurityManager.checkWrite(java.lang.String)
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
你的这个就相关于在网页上点 另存为
然后 fileName 就是保存的位置你到底有什么问题啊?