File file = new File(http://xxxxxx);
为什么最后输出file的路径为http:\xxxxx
File file = new File(http:////xxxxxx) 输出的路径还是http:\xxxx导致无法访问指定文件。
为什么,输出的总是“\”呢,有什么办法解决么,小弟在此先谢过大家了!
为什么最后输出file的路径为http:\xxxxx
File file = new File(http:////xxxxxx) 输出的路径还是http:\xxxx导致无法访问指定文件。
为什么,输出的总是“\”呢,有什么办法解决么,小弟在此先谢过大家了!
解决方案 »
- Ext 怎么实现类似百度,google的搜索,没分了不好意思。
- 有关Enterprice java bean
- 江湖救急!关于新建某一信息时,判断是否存在。
- applet和Servlet之间通讯抛java.io.EOFException异常
- javabean连接数据库的问题,老师提示这样的错误
- birt 修改每页显示的数据个数
- servlet中将查询结果存入ArrayList 再将此arraylist存入session ,是不是arraylist只能容纳1024个对象?我可是超过这个数字的!有其他的
- 有高手熟悉录入组件spreadsheet吗?
- 请帮帮我~~~
- tomcat启动过几分钟后报错
- jsp链接oracle的时候错误提示
- 求救:hibernate+spring+Struts1异常处理及其他问题?请大家帮忙!
File file = new File("c:/hello.txt");
这样就可以访问了. 你的http://xxxxxx 不需要用 " " 引起来?
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();
} }
}
File(URI uri)
file: URI 的具体形式与系统有关,因此,由此构造方法执行的转换也与系统有关。 对于某个给定抽象路径名 f,可以保证: new File( f.toURI()).equals( f.getAbsoluteFile())
只要原始抽象路径名、URI 和新的抽象路径名都是在同一 Java 虚拟机(或者它的不同调用)中创建的。但是,当在某一操作系统上的虚拟机中创建的 file: URI 在不同操作系统上的虚拟机中被转换成抽象路径名时,这种关系通常是不成立的。