更新時間:2021-04-29 09:10:06 來源:動力節點 瀏覽3292次
本文實例講述了java實現從網上下載圖片到本地的方法。分享給大家供大家參考。具體如下:
工具類代碼如下:
package util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.log4j.Logger;
/**
* 下載http對應URL的圖片資源
*
*/
public class DownloadImage{
/**
* 日志
*/
protected final Logger logger = Logger.getLogger(DownloadImage.class);
private String ImageRootPath = "/data/application/tempImg";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
DownloadImage downloadImage = new DownloadImage();
String httpUrlStr = "https://avatar.csdn.net/3/6/0/1_qq_25646191.jpg?1523447843";
String fileName = System.currentTimeMillis() + ".png";
downloadImage.download(httpUrlStr, fileName);
}
/**
* 下載圖片
* @param httpUrlStr
* @param subFolderName
* @param filename
* @return
*/
public String download(String httpUrlStr, String filename) {
return download(httpUrlStr,filename,ImageRootPath);
}
@SuppressWarnings("finally")
private String download(String httpUrlStr, String filename,String savePath){
InputStream is = null;
OutputStream os = null;
String filePath = null;
try {
filePath = savePath + File.separator + filename;
logger.info(String.format("下載圖片... url = {%s}, filePath = {%s} ", httpUrlStr, filePath));
// 構造URL
URL url = new URL(httpUrlStr);
// 打開連接
URLConnection con = url.openConnection();
//設置請求超時為5s
con.setConnectTimeout(5*1000);
// 輸入流
is = con.getInputStream();
// 1K的數據緩沖
byte[] bs = new byte[1024];
// 讀取到的數據長度
int len;
// 輸出的文件流
File sf=new File(savePath);
if(!sf.exists()){
sf.mkdirs();
}
os = new FileOutputStream(filePath);
// 開始讀取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
logger.info(String.format("下載圖片成功 圖片存放路徑 = {%s}", filePath));
} catch (MalformedURLException e) {
logger.error("下載圖片失敗 : " + e.getMessage());
e.printStackTrace();
} catch (Exception e){
logger.error("普通異常下載圖片失敗 : " + e.getMessage());
} finally {
// 完畢,關閉所有鏈接
try {
if(null != os){
os.close();
}
if(null != is){
is.close();
}
} catch (IOException e) {
logger.error("下載圖片關閉流失敗: " + e.getMessage());
}
return filePath;
}
}
/**
* 刪除臨時文件 注意,只能刪除指定目錄下面的臨時文件
* @param supplierId 商戶ID
* @param fileName 被刪除文件的文件名
* @return 單個文件刪除成功返回true,否則返回false
*/
public boolean deleteFile(String fileName) {
boolean flag = false;
try{
String sPath = getImageRootPath() + File.separator + fileName;
logger.info(String.format("刪除臨時文件 path = {%s} ", sPath));
File file = new File(sPath);
// 路徑為文件且不為空則進行刪除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
} catch(Exception e){
logger.info("刪除文件異常 " + e.getMessage());
}
return flag;
}
public String getImageRootPath() {
return ImageRootPath;
}
public void setImageRootPath(String ImageRootPath) {
this.ImageRootPath = ImageRootPath;
}
}
運行結果如下:
下載結果:
以上就是動力節點小編介紹的"Java下載圖片的方法"的內容,希望對大家有幫助,如有疑問,請在線咨詢,有專業老師隨時為您服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習