Java后端SSM框架圖片上傳功能實(shí)現(xiàn)方法解析
一、技術(shù)概述
(1)這個(gè)技術(shù)是做什么
這個(gè)技術(shù)是上傳圖片到服務(wù)器上,并且把地址存在數(shù)據(jù)庫(kù)中。前端調(diào)用的時(shí)候之間通過(guò)地址即可調(diào)用。
(2)學(xué)習(xí)該技術(shù)的原因
由于用戶在寫日記的時(shí)候也可以進(jìn)行圖片的上傳,同時(shí)還有用戶頭像的上傳。
二、技術(shù)詳述
以上傳用戶的頭像為例
(1)接口代碼
@RequestMapping(value = 'user/profilePhoto', produces = 'application/json; charset=utf-8')@ResponseBodypublic boolean imageUphold(@RequestParam('photo') MultipartFile file, Long phone) throws IOException {String filePath = ducumentBase;// 保存圖片的路徑// String filePath = '/image';//保存圖片的路徑// 獲取原始圖片的拓展名String originalFilename = file.getOriginalFilename();System.out.println('originalFilename: ' + originalFilename);// 新的文件名字String newFileName = UUID.randomUUID() + originalFilename;// 封裝上傳文件位置的全路徑filePath += '/' + phone;System.out.println('filePath: ' + filePath);File targetFile = new File(filePath, newFileName);if (!targetFile.exists()) {targetFile.mkdirs();}// 把本地文件上傳到封裝上傳文件位置的全路徑System.out.println('newFileName: ' + newFileName);System.out.println('targetFile: ' + targetFile.getName());System.out.println('phone: ' + phone);//System.out.println('afterPhone');try {file.transferTo(targetFile);} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}String allPath=mappingPath + '/' + phone+ '/' + newFileName;System.out.println('存儲(chǔ)路徑為'+allPath);boolean result=onedayServiceImpl.updProfilePhoto(allPath, phone);//存在數(shù)據(jù)庫(kù)中,其中allPath的數(shù)據(jù)庫(kù)類型為varchar(1000)return result;}
其中的ducumentBase以及mappingPath
@Value('${ducument.base}')private String ducumentBase;@Value('${mapping.path}')private String mappingPath;
為全局變量
配置文件
ducument.base = D://oneday_upholdmapping.path = /images
(2)解釋
用MultipartFile來(lái)接收?qǐng)D片的二進(jìn)制碼,然后使用路徑+圖片名+隨機(jī)數(shù)保存圖片。
(3)測(cè)試jsp
<%@ page language='java' contentType='text/html; charset=UTF-8' pageEncoding='UTF-8'%><!DOCTYPE html><html><head><meta charset='UTF-8'><title>image/uphold</title></head><body> <form action='user/profilePhoto' method='post' enctype='multipart/form-data'> 圖片:<input type='file' name='photo'> 電話:<input type='text' name='phone' value='13225942005'> <input type='submit' value='提交'> </form></body></html>
(4)顯示圖片
<img alt='頭像' src='http://m.b3g6.com/mappingPath/路徑'>
三、技術(shù)使用中遇到的問(wèn)題和解決過(guò)程
(1)無(wú)法保存:
查看是否已進(jìn)行服務(wù)器的設(shè)置,以Eclipse為例
Servers->Modules->Add External Web Modules 進(jìn)行路徑的設(shè)置
(2)無(wú)法訪問(wèn)接口:
查看是否使用表單形式訪問(wèn):method='post' enctype='multipart/form-data'
同時(shí)上傳的名字是否與接口相對(duì)應(yīng)
四、總結(jié)
本來(lái)進(jìn)行圖片的上傳的時(shí)候考慮過(guò)直接上傳二進(jìn)制到數(shù)據(jù)庫(kù)用blob進(jìn)行保存,但覺(jué)得這樣不好,遂改為保存圖片地址的方式進(jìn)行上傳。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP驗(yàn)證碼工具-Securimage2. 一文帶你徹底理解Java序列化和反序列化3. JS實(shí)現(xiàn)前端動(dòng)態(tài)分頁(yè)碼代碼實(shí)例4. 用Spring JMS使異步消息變得簡(jiǎn)單5. 關(guān)于IDEA 2020.3 多窗口視圖丟失的問(wèn)題6. js實(shí)現(xiàn)碰撞檢測(cè)7. Python 下載Bing壁紙的示例8. 通過(guò)實(shí)例解析Python文件操作實(shí)現(xiàn)步驟9. Python 制作查詢商品歷史價(jià)格的小工具10. Python3 json模塊之編碼解碼方法講解

網(wǎng)公網(wǎng)安備