public static void test() {
String fileName = "D:\\222.png";
try {
HttpClientUtil.httpDownload("http://10.50.13.99:9017/image/202303291048_6b74e443-9d54-4b8e-a7cc-843a72a2608a",fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 功能描述: 下载网络文件
*
* @param httpUrl
* @param dest
* @return boolean
* @author
* @version 1.0
*/
public static boolean httpDownload(String httpUrl, String dest) throws IOException {
// 1.下载网络文件
int byteRead;
URL url;
try {
url = new URL(httpUrl);
} catch (MalformedURLException e1) {
e1.printStackTrace();
return false;
}
URLConnection conn = null;
InputStream inStream = null;
FileOutputStream fs = null;
try {
//2.获取链接
conn = url.openConnection();
//3.输入流
inStream = conn.getInputStream();
//3.写入文件
fs = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
while ((byteRead = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteRead);
}
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}finally {
try {
if (fs != null){
fs.close();
}
if (inStream != null){
inStream.close();
}
} catch (IOException ex) {
logger.error(ex.getMessage());
}
}
}
还没有评论,来说两句吧...