Java 讀取網(wǎng)絡(luò)圖片存儲(chǔ)到本地并生成縮略圖
之前使用 Python 爬蟲抓取電影網(wǎng)站信息作為自己網(wǎng)站的數(shù)據(jù)來源,其中包含的圖片都是網(wǎng)絡(luò)圖片,會(huì)存在這樣一個(gè)問題:
當(dāng)原始網(wǎng)站訪問速度比較慢時(shí),網(wǎng)站圖片加載時(shí)間也會(huì)變得很慢,而且如果原始網(wǎng)站掛了,圖片就直接訪問不到了。
此時(shí)的用戶體驗(yàn)就很不好,所以對(duì)此進(jìn)行了優(yōu)化:
每次后端啟動(dòng)時(shí)會(huì)默認(rèn)開啟任務(wù)先將未轉(zhuǎn)換的網(wǎng)絡(luò)圖片存儲(chǔ)到本地,再把網(wǎng)頁中圖片列表改為訪問本地圖片,這樣就解決了加載慢的問題,也降低了和原始網(wǎng)站的耦合性,具體步驟如下:
1.創(chuàng)建用于保存圖片的文件夾
我的保存路徑:F:\images
2.新建 createLocalImage 類用于圖片轉(zhuǎn)換
package com.cn.beauty.task;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class createLocalImage {
// 需要保存到本地的根路徑
private static String basePath = "F:/";
public static void main(String[] args) {
// 網(wǎng)頁圖片路徑
String destUrl = "http://5b0988e595225.cdn.sohucs.com/images/20200215/349bb3cb88b744dcb67f37dba2f71abf.jpeg";
String filePath = createLocalImageMethod(destUrl);
System.out.println("生成的相對(duì)文件路徑為" + filePath);
}
private static String createLocalImageMethod(String destUrl) {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
int BUFFER_SIZE = 1024;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
String filePath = "";
try {
System.out.println("原始圖片URL為:" + destUrl);
String[] fileNameArray = destUrl.split("\\/");
if (fileNameArray.length > 1) {
String fileName = fileNameArray[fileNameArray.length - 1];
filePath = "images/" + fileName;
File file = new File(basePath + filePath);
if (!file.exists()) {
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
fos = new FileOutputStream(basePath + filePath);
while ((size = bis.read(buf)) != -1) {
fos.write(buf, 0, size);
}
fos.flush();
}
// 后續(xù)對(duì)圖片進(jìn)行縮略圖處理,見后面代碼
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassCastException e) {
e.printStackTrace();
} finally {
try {
fos.close();
bis.close();
httpUrl.disconnect();
} catch (IOException e) {
} catch (NullPointerException e) {
}
}
return filePath;
}
}
運(yùn)行后發(fā)現(xiàn)圖片已經(jīng)成功生成:

3.生成縮略圖
使用工具類Thumbnails
如果是圖片列表的展示,原始圖片過大還是會(huì)影響加載速度,此時(shí)我們可以將圖片處理為縮略圖進(jìn)行顯示。
我們使用了一個(gè)很強(qiáng)大的圖片處理工具類:Thumbnails,它支持的功能包括:
- 按指定大小進(jìn)行縮放;
- 按照比例進(jìn)行縮放;
- 不按照比例,指定大小進(jìn)行縮放;
- 旋轉(zhuǎn),水印,裁剪;
- 轉(zhuǎn)化圖像格式;
- 輸出到 OutputStream;
- 輸出到 BufferedImage;
這里的需求比較簡(jiǎn)單,只用到了按指定大小進(jìn)行縮放的功能。
引入對(duì)應(yīng) jar 包:
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
在 createLocalImage 方法中添加縮略圖生成的代碼實(shí)現(xiàn):
String thumbName = fileName.split("\\.")[0] + "_thumb." + fileName.split("\\.")[1];
String thumbPath = basePath + filePath.replace(fileName, thumbName);
//將要轉(zhuǎn)換出的小圖文件
File fo = new File(thumbPath);
if (fo.exists()) {
return thumbPath;
}
// 第一個(gè)參數(shù)是原始圖片的路徑,第二個(gè)是縮略圖的路徑
Thumbnails.of(basePath + filePath).size(120, 120).toFile(thumbPath);
System.out.println("生成的縮略圖路徑為:" + thumbPath);
再次運(yùn)行,發(fā)現(xiàn)縮略圖已經(jīng)成功生成:

另一種方法
直接將下面的代碼封裝成一個(gè)util即可,調(diào)用示例在main方法中,調(diào)用的地方需要引入import java.awt.image.BufferedImage;,還要確保舊文件是存在的
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ResizeImage {
public static void main(String[] args) throws IOException {
//windows路徑,linux環(huán)境下相應(yīng)修改
String outputFolder = "D:\\test\\";
String fileName = "D:\\test\\test.jpg";
ResizeImage r = new ResizeImage();
int toWidth=220,toHeight=220;
BufferedImage imageList = r.getImageList(fileName,new String[] {"jpg","png","gif"});
r.writeHighQuality("newFile.jpg",r.zoomImage(imageList,toWidth,toHeight),outputFolder);
}
/**
* @Description: 取得圖片對(duì)象
* @param 要轉(zhuǎn)化的圖像的文件夾,就是存放圖像的文件夾路徑
* @date 2017年5月7日10:48:27
*/
public BufferedImage zoomImage(BufferedImage im, int toWidth , int toHeight) {
BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
return result;
}
/**
* @Description: 取得圖片對(duì)象
* @param 要轉(zhuǎn)化的圖像的文件夾,就是存放圖像的文件夾路徑
* @date 2017年5月7日10:48:27
*/
public BufferedImage getImageList(String ImgList, String[] type) throws IOException{
Map<String,Boolean> map = new HashMap<String, Boolean>();
for(String s : type) {
map.put(s,true);
}
BufferedImage imageList = null;
File file = null;
file = new File(ImgList);
try{
if(file.length() != 0 && map.get(getExtension(file.getName())) != null ){
imageList = javax.imageio.ImageIO.read(file);
}
}catch(Exception e){
imageList = null;
}
return imageList;
}
/**
* 把圖片寫到磁盤上
* @param im
* @param path 圖片寫入的文件夾地址
* @param fileName 寫入圖片的名字
* @date 2017年5月7日10:48:27
*/
public boolean writeToDisk(BufferedImage im, String path, String fileName) {
File f = new File(path + fileName);
String fileType = getExtension(fileName);
if (fileType == null)
return false;
try {
ImageIO.write(im, fileType, f);
im.flush();
return true;
} catch (IOException e) {
return false;
}
}
/**
* @Description: 生成圖片
* @param String path , BufferedImage im, String fileFullPath
* @date 2017年5月7日10:48:27
*/
public boolean writeHighQuality(String path , BufferedImage im, String fileFullPath) throws IOException {
FileOutputStream newimage = null;
try {
// 輸出到文件流
newimage = new FileOutputStream(fileFullPath+path);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
// 壓縮質(zhì)量
jep.setQuality(1f, true);
encoder.encode(im, jep);
//近JPEG編碼
newimage.close();
return true;
} catch (Exception e) {
return false;
}
}
/**
* @Description: 取文件名的后綴
* @param String fileName 格式如:cn1100000213EA_1_xnl.jpg
* @date 2017年5月7日10:48:27
*/
public String getExtension(String fileName) {
try {
return fileName.split("\\.")[fileName.split("\\.").length - 1];
} catch (Exception e) {
return null;
}
}
以上就是Java 讀取網(wǎng)絡(luò)圖片存儲(chǔ)到本地并生成縮略圖的詳細(xì)內(nèi)容,更多關(guān)于Java 圖片存儲(chǔ)到本地并生成縮略圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Java 截取視頻資料中的某一幀作為縮略圖
- 詳解Java實(shí)現(xiàn)批量壓縮圖片裁剪壓縮多種尺寸縮略圖一鍵批量上傳圖片
- Java實(shí)現(xiàn)的不同圖片居中剪裁生成同一尺寸縮略圖功能示例
- java生成縮略圖的方法示例
- Java圖片裁剪和生成縮略圖的實(shí)例方法
- java實(shí)現(xiàn)創(chuàng)建縮略圖、伸縮圖片比例生成的方法
- java根據(jù)url抓取并生成縮略圖的示例
- 用java實(shí)現(xiàn)的獲取優(yōu)酷等視頻縮略圖的實(shí)現(xiàn)代碼
- Java縮略圖生成庫之Thumbnailator應(yīng)用說明
- Java實(shí)現(xiàn)自動(dòng)生成縮略圖片
相關(guān)文章
springboot框架中如何整合mybatis框架思路詳解
這篇文章主要介紹了springboot框架中如何整合mybatis框架,本文通過示例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
mybatis-generator-gui 工具使用(圖形化工具)
基于 mybatis generator 開發(fā)一款界面工具, 本工具可以使你非常容易及快速生成 Mybatis 的 Java POJO 文件及數(shù)據(jù)庫 Mapping 文件。本文重點(diǎn)給大家介紹mybatis-generator-gui 工具使用,感興趣的朋友一起看看吧2022-03-03
Java中的權(quán)限修飾符(protected)示例詳解
這篇文章主要給大家介紹了關(guān)于Java中權(quán)限修飾符(protected)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringCache 分布式緩存的實(shí)現(xiàn)方法(規(guī)避redis解鎖的問題)
這篇文章主要介紹了SpringCache 分布式緩存的實(shí)現(xiàn)方法(規(guī)避redis解鎖的問題),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot可視化監(jiān)控的具體應(yīng)用
最近越發(fā)覺得,任何一個(gè)系統(tǒng)上線,運(yùn)維監(jiān)控都太重要了,本文介紹了SpringBoot可視化監(jiān)控的具體應(yīng)用,分享給大家,有興趣的同學(xué)可以參考一下2021-06-06

