package test;
import java.io.File;
import java.net.URL;
import org.apache.commons.io.FileUtils;
public class UploadImg {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String url="http://www.baidu.com/img/baidu_sylogo1.gif";
downloadFromUrl(url,"D:/aa/");
System.out.println("ok");
}
/**
* 文件下载的方法
* @param String url
* @param String dir
* @return String fileName
*/
public static String downloadFromUrl(String url, String dir) {
String fileName="";
try {
URL httpurl = new URL(url);
//fileName = getFileNameFromUrl(url);
String[] us=url.split("/");
fileName=us[us.length-1];
System.out.println("fileName:"+fileName);
File f = new File(dir + fileName);
FileUtils.copyURLToFile(httpurl, f);
} catch (Exception e) {
e.printStackTrace();
return "Fault!";
}
return fileName;
}
}
lib.rar (1.6 MB)
下载次数: 35
分享到:
2011-11-23 10:55
浏览 1914
评论