Java后端SSM框架圖片上傳功能實(shí)現(xiàn)方法解析
一、技術(shù)概述
(1)這個(gè)技術(shù)是做什么
這個(gè)技術(shù)是上傳圖片到服務(wù)器上,并且把地址存在數(shù)據(jù)庫中。前端調(diào)用的時(shí)候之間通過地址即可調(diào)用。
(2)學(xué)習(xí)該技術(shù)的原因
由于用戶在寫日記的時(shí)候也可以進(jìn)行圖片的上傳,同時(shí)還有用戶頭像的上傳。
二、技術(shù)詳述
以上傳用戶的頭像為例
(1)接口代碼
@RequestMapping(value = "user/profilePhoto", produces = "application/json; charset=utf-8") @ResponseBody public 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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String allPath=mappingPath + "/" + phone+ "/" + newFileName; System.out.println("存儲(chǔ)路徑為"+allPath); boolean result=onedayServiceImpl.updProfilePhoto(allPath, phone);//存在數(shù)據(jù)庫中,其中allPath的數(shù)據(jù)庫類型為varchar(1000) return result; }
其中的ducumentBase以及mappingPath
@Value("${ducument.base}")
private String ducumentBase;
@Value("${mapping.path}")
private String mappingPath;
為全局變量
配置文件
ducument.base = D://oneday_uphold
mapping.path = /images
(2)解釋
用MultipartFile來接收?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 id="images" alt="頭像" src="/mappingPath/路徑">
三、技術(shù)使用中遇到的問題和解決過程
(1)無法保存:
查看是否已進(jìn)行服務(wù)器的設(shè)置,以Eclipse為例
Servers->Modules->Add External Web Modules 進(jìn)行路徑的設(shè)置
(2)無法訪問接口:
查看是否使用表單形式訪問:method="post" enctype="multipart/form-data"
同時(shí)上傳的名字是否與接口相對(duì)應(yīng)
四、總結(jié)
本來進(jìn)行圖片的上傳的時(shí)候考慮過直接上傳二進(jìn)制到數(shù)據(jù)庫用blob進(jìn)行保存,但覺得這樣不好,遂改為保存圖片地址的方式進(jìn)行上傳。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot定時(shí)任務(wù)實(shí)現(xiàn)數(shù)據(jù)同步的方法
業(yè)務(wù)需求是,通過中臺(tái)調(diào)用api接口獲得,設(shè)備數(shù)據(jù),要求現(xiàn)實(shí)設(shè)備數(shù)據(jù)的同步,這篇文章主要介紹了SpringBoot定時(shí)任務(wù)實(shí)現(xiàn)數(shù)據(jù)同步的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08SpringMVC @ResponseBody 415錯(cuò)誤處理方式
這篇文章主要介紹了SpringMVC @ResponseBody 415錯(cuò)誤處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11springboot3環(huán)境隔離的實(shí)現(xiàn)
在開發(fā)中,環(huán)境很多,本文主要介紹了springboot3環(huán)境隔離的實(shí)現(xiàn),能夠快速切換開發(fā)、測(cè)試、生產(chǎn)環(huán)境,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03Java面試Socket編程常用參數(shù)設(shè)置源碼問題分析
這篇文章主要為大家介紹了Java編程中關(guān)于Socket結(jié)構(gòu)分析,常用參數(shù)設(shè)置源碼示例以及面試中的問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03spring security自定義認(rèn)證登錄的全過程記錄
這篇文章主要給大家介紹了關(guān)于spring security自定義認(rèn)證登錄的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12