已经知道了一个文件的URL,如何用JAVA下载并保存这个文件?
解决方案 »
- 写一句你认为最有价值的代码,是时候考量下你的水平的时候咯
- 怎样计算出与当前给出日期后面的最近的星期六?
- 如何在win2k下安装javacomm20-win32.zip包?
- 如何编译处于不同文件中的两个互相引用的类,用jsdk命令行怎么编
- 请问一个问题!谢谢!!!
- Connection reset by peer: socket write error
- 新手求问java类的加载顺序,初始化顺序,继承关系中构造器的调用等一系列问题,能否给我详细讲讲程序的执行过程。
- applet本地打印问题
- 配置自己的tomcat目录的问题
- 关于volatile可见性的遇到的问题
- 求助一个棘手问题!
- 关于截取定长中文字符串的问题
非html,gif等ie能打开的文件会自动提示下载。
用bis将文件保存。
要用 url.openStream();
因为url 不一定是本地文件. file:/C:/abc.txt 这种形式,
也可能是 http://afwef.com/abc.txt 或者 ftp://username:password@host:port/path/file.txt
public final InputStream getResourceAsStream(String location) throws IOException {
try {
// try URL
URL url = new URL(location);
logger.debug("Opening as URL: " + location);
return url.openStream();
} catch (MalformedURLException ex) {
// no URL -> try (file) path
InputStream in = getResourceByPath(location);
if (in == null) {
throw new FileNotFoundException("Location '" + location + "' isn't a URL and cannot be interpreted as (file) path");
}
return in;
}
}
{
try
{
URL url = null; url = new URL( httpUrl ); BufferedInputStream input = new BufferedInputStream(url.openStream());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(strTempDir)); byte[] buf = new byte[1024*1024]; int len = input.read(buf, 0, buf.length);
while (len != -1)
{
out.write(buf, 0, len);
len = input.read(buf, 0, buf.length);
} input.close();
input = null;
out.close();
out = null;
}
catch(Exception e)
{
System.out.println(e);
}
} public void downloadFile() throws IOException
{
for( int i = 3290;i < 3292; i++ )
{
String strTempDir = "c:\\fonts\\" + String.valueOf(i) + ".rar"; String location = "http://font.flash8.net/download.aspx?app=font&id=";
location = location + String.valueOf(i); this.downloadFile_( strTempDir , location );
}
}