av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn)

瀏覽:3日期:2023-05-20 13:42:10

一.背景

工作中接觸到需要采集并管理大量圖片的需求,本來(lái)是用的FastDFS,但是發(fā)現(xiàn)實(shí)際情況是在項(xiàng)目實(shí)施時(shí)難以找到linux服務(wù)器去安裝FastDFS,所以經(jīng)過(guò)調(diào)研,選擇了可以在windows服務(wù)器上安裝部署的Go-FastDFS文件服務(wù)器

二.Go-FastDFS簡(jiǎn)介

go-fastdfs是一個(gè)基于http協(xié)議的分布式文件系統(tǒng),它基于大道至簡(jiǎn)的設(shè)計(jì)理念,一切從簡(jiǎn)設(shè)計(jì),使得它的運(yùn)維及擴(kuò)展變得更加簡(jiǎn)單,它具有高性能、高可靠、無(wú)中心、免維護(hù)等優(yōu)點(diǎn)。

三.安裝Go-FastDFS文件服務(wù)器

1)下載地址:https://github.com/sjqzhang/go-fastdfs/releases

2)下載完成直接啟動(dòng)fileserver.exe

Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn)

3)驗(yàn)證是否安裝成功,訪問(wèn)localhost:8080

Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn)

4)驗(yàn)證上傳功能,點(diǎn)擊選擇文件選擇好文件后,點(diǎn)擊上傳

Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn)

5)在返回的url后加?download=0,查看圖片

Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn)

四.實(shí)例實(shí)現(xiàn)功能

1)圖片上傳2)圖片刪除3)圖片訪問(wèn)4)圖片水印添加

五.創(chuàng)建Spring boot項(xiàng)目,寫(xiě)代碼實(shí)現(xiàn)功能

1)pom.xml添加依賴

<!--工具包--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency>

2)核心代碼,使用go-fastdhs上傳圖片并添加水印及刪除圖片工具類

@Componentpublic 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'); // 場(chǎng)景 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 = '拍攝時(shí)間:' + date + '&攝像頭編碼:' + sixCode; // 設(shè)置水印坐標(biāo) 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é)果集對(duì)象,對(duì)象定義如下:

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)在實(shí)際應(yīng)用中編寫(xiě)控制層方法調(diào)用核心工具類的上傳,刪除方法即可

總結(jié):本次總結(jié)主要描述了spring boot集成go-fastdfs上傳圖片的核心方法,沒(méi)有具體的測(cè)試展示,其實(shí)go-fastdfs的使用很簡(jiǎn)單,接口編寫(xiě)也很簡(jiǎn)單

到此這篇關(guān)于Spring boot集成Go-FastDFS實(shí)現(xiàn)圖片上傳刪除等功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring boot集成Go-FastDFS圖片上傳刪除內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产日产精品一区二区三区的介绍 | 夜夜精品视频 | 久草福利在线观看 | 91女人18毛片水多国产 | 美女张开腿 | 国产h在线 | 三级在线观看 | 亚洲一级特黄 | 国产女人水真多18毛片18精品 | 成人免费小视频 | 欧美日韩激情视频 | 91福利视频导航 | www.国产精品.com | 一区在线观看视频 | 少妇视频在线观看 | 成人不卡视频 | 日韩一级在线观看 | 毛片在线观看网站 | 午夜在线国语中文字幕视频 | 在线观看日韩视频 | 国产成人精品一区二区三区视频 | 宅男噜噜噜66一区二区 | 欧美日韩免费在线 | 亚洲免费在线观看视频 | 免费看黄色录像 | 人人综合网 | 欧美成人精品欧美一级私黄 | 欧美亚洲国产日韩 | 日韩国产精品视频 | 亚洲成在线 | 人人爱人人草 | 欧美日韩激情视频 | 日韩精品一级 | 黄色1级片| 精品一区二区三区三区 | 中文字幕视频在线 | 日韩午夜片 | 国产福利视频在线 | 国内自拍偷拍 | 国产精品99久久久久久久久久久久 | 国产高潮在线 |