Spring boot集成Go-FastDFS實現(xiàn)圖片上傳刪除等功能實現(xiàn)
一.背景
工作中接觸到需要采集并管理大量圖片的需求,本來是用的FastDFS,但是發(fā)現(xiàn)實際情況是在項目實施時難以找到linux服務器去安裝FastDFS,所以經(jīng)過調(diào)研,選擇了可以在windows服務器上安裝部署的Go-FastDFS文件服務器
二.Go-FastDFS簡介
go-fastdfs是一個基于http協(xié)議的分布式文件系統(tǒng),它基于大道至簡的設計理念,一切從簡設計,使得它的運維及擴展變得更加簡單,它具有高性能、高可靠、無中心、免維護等優(yōu)點。
三.安裝Go-FastDFS文件服務器
1)下載地址:https://github.com/sjqzhang/go-fastdfs/releases
2)下載完成直接啟動fileserver.exe
3)驗證是否安裝成功,訪問localhost:8080
4)驗證上傳功能,點擊選擇文件選擇好文件后,點擊上傳
5)在返回的url后加?download=0,查看圖片
四.實例實現(xiàn)功能
1)圖片上傳
2)圖片刪除
3)圖片訪問
4)圖片水印添加
五.創(chuàng)建Spring boot項目,寫代碼實現(xiàn)功能
1)pom.xml添加依賴
<!--工具包--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency>
2)核心代碼,使用go-fastdhs上傳圖片并添加水印及刪除圖片工具類
@Component public class GoFastdfsClientUtil { @Value("${camera.upload.path}") private String uploadPath; @Value("${camera.delete.path}") private String deletePath; private final Logger logger = LoggerFactory.getLogger(GoFastdfsClientUtil.class); /** * 圖片上傳 * * @param file * @param sixCode * @return * @throws IOException */ public UploadResult upload(MultipartFile file, String sixCode) throws IOException { UploadResult uploadResult = new UploadResult(); ByteArrayOutputStream bos = addWatermark(file, sixCode); byte[] b = bos.toByteArray(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b); InputStreamResource isr = new InputStreamResource(byteArrayInputStream, file.getOriginalFilename()); Map<String, Object> params = new HashMap<>(); params.put("file", isr); params.put("path", "image"); params.put("output", "json"); // 場景 params.put("scene", "image"); String resp = HttpUtil.post(uploadPath, params); Console.log("resp: {}", resp); JSONObject exJson = JSONObject.parseObject(resp); uploadResult = JSON.toJavaObject(exJson, UploadResult.class); return uploadResult; } /** * 圖片刪除 * * @param fileUrl */ public void deleteImage(String md5) { if (StringUtils.isEmpty(md5)) { return; } try { Map<String, Object> params = new HashMap<>(); params.put("md5", md5); HttpUtil.post(deletePath, params); } catch (Exception e) { logger.warn(e.getMessage()); } } /** * 加水印 * * @param myfile * @param sixCode * @return * @throws IOException */ private ByteArrayOutputStream addWatermark(MultipartFile myfile, String sixCode) throws IOException { InputStream in = myfile.getInputStream(); BufferedInputStream bis = new BufferedInputStream(in); BufferedImage image = ImageIO.read(bis); int height = image.getHeight(); int width = image.getWidth(); // 加水印 Graphics2D g = image.createGraphics(); g.drawImage(image, 0, 0, width, height, null); g.setColor(new Color(128, 128, 128)); // 字體 int num = 0; if (width > height) { num = height / 30; } else { num = width / 30; } g.setFont(new Font("微軟雅黑", Font.PLAIN, num)); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = formatter.format(new Date()); String watermarkContent = "拍攝時間:" + date + "&攝像頭編碼:" + sixCode; // 設置水印坐標 String[] split = watermarkContent.split("&"); int x = 10; int y = height - 10; for (int i = 0; i < split.length; i++) { g.drawString(split[i], x, y -= g.getFontMetrics().getHeight()); } g.dispose(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", bos); return bos; } }
解釋:這里我們事先在配置文件中配置好了文件的上傳路徑以及刪除路徑,配置如下:
camera: upload: path: http://localhost:8080/group1/upload delete: path: http://localhost:8080/group1/delete visit: path: http://localhost:8080
3)上面的方法中我們將圖片上傳后的返回值轉(zhuǎn)換為結(jié)果集對象,對象定義如下:
public class UploadResult implements Serializable{ /** * */ private static final long serialVersionUID = 5534287808864118463L; private String url; private String md5; private String path; private String domain; private String scene; private BigInteger size; private BigInteger mtime; private String scenes; private String retmsg; private int retcode; private String src; ......get,set方法..... }
4)在實際應用中編寫控制層方法調(diào)用核心工具類的上傳,刪除方法即可
總結(jié):本次總結(jié)主要描述了spring boot集成go-fastdfs上傳圖片的核心方法,沒有具體的測試展示,其實go-fastdfs的使用很簡單,接口編寫也很簡單
到此這篇關(guān)于Spring boot集成Go-FastDFS實現(xiàn)圖片上傳刪除等功能實現(xiàn)的文章就介紹到這了,更多相關(guān)Spring boot集成Go-FastDFS圖片上傳刪除內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java面試突擊之sleep和wait有什么區(qū)別詳析
按理來說sleep和wait本身就是八竿子打不著的兩個東西,但是在實際使用中大家都喜歡拿他們來做比較,或許是因為它們都可以讓線程處于阻塞狀態(tài),這篇文章主要給大家介紹了關(guān)于java面試突擊之sleep和wait有什么區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-02-02Springboot如何根據(jù)實體類生成數(shù)據(jù)庫表
這篇文章主要介紹了Springboot如何根據(jù)實體類生成數(shù)據(jù)庫表的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09IntelliJ?IDEA?2023.2最新版激活方法及驗證ja-netfilter配置是否成功
隨著2023.2版本的發(fā)布,用戶們渴望了解如何激活這個最新版的IDE,本文將介紹三種可行的激活方案,包括許可證服務器、許可證代碼和idea?vmoptions配置,幫助讀者成功激活并充分利用IDEA的功能,感興趣的朋友參考下吧2023-08-08Java 中Json中既有對象又有數(shù)組的參數(shù)如何轉(zhuǎn)化成對象(推薦)
Gson庫是一個功能強大、易于使用的Java序列化/反序列化庫,它提供了豐富的API來支持Java對象和JSON之間的轉(zhuǎn)換,這篇文章主要介紹了Java 中Json中既有對象又有數(shù)組的參數(shù)如何轉(zhuǎn)化成對象,需要的朋友可以參考下2024-07-07