下载文件的问题 已经知道了一个文件的URL,如何用JAVA下载并保存这个文件? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用JAVA下载?response.sendRedirect(url);非html,gif等ie能打开的文件会自动提示下载。 URL url;BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(url)));用bis将文件保存。 上面这个例子,如果文件URL 是个远程的就不行了.要用 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; } } public void downloadFile_( String strTempDir , String httpUrl) { 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 ); } } 杨辉三角问题求助 java线程问题wait。希望高手指点一下。网上关于这方面的比较少 netbeans IDE 5.5.1 RUN問題 求一个正则表达式 Struts1中的ActionForm 对象为空,并且抛出 No origin bean specified JTree中调用UpdateUI()报错,请解决,谢谢 多进程锁问题 这个错误是为什么 请问,如何下载JDK1.4版本,保存在哪里比较好一点。 请高手解答!! 求助一个棘手问题! 关于截取定长中文字符串的问题
非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 );
}
}