/**
*
* @param urlStr 文件地址
* @param fileName 文件名
* @param filePath 保存路径
* @return
*/
public static String downLoadByUrl(String urlStr, String fileName, String filePath) {
try {
// 使用java.net.Proxy类设置代理IP
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("123.22.43.3", 8080));
URL url = new URL(urlStr);
//HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
//conn.setRequestMethod("POST");
//设置超时间为3秒
conn.setConnectTimeout(30 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);
//获取项目根目录地址
String propertiesFile = filePath;
//文件保存位置
File saveDir = new File(propertiesFile);
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
return propertiesFile + "/" + fileName;
}catch (Exception e){
e.printStackTrace();
return "";
}
}
06-22
1万+
08-14
1582
11-22
3157
08-03
3036
09-29
8418
11-24
1万+
09-25
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交