spring boot專案讀取本地(伺服器)圖片顯示在頁面(二)
本文轉自:https://blog.csdn.net/CarryBest/article/details/80494433
1:在配置檔案中加上下面(windows,Linux類似)
我的專案是在F盤,所以這裡的虛擬路徑是 F:/hd/img/
web.upload-path=F:/hd/img/
spring.mvc.static-path-pattern=/**spring.resources.static-locations=classpath\:/META-INF/resources/,classpath\:/resources/,classpath\:/static/,classpath\:/public/,file\:${web.upload-path}
classpath:/static/,classpath:/public/,file:${web.upload-path}
2:上傳方法,這裡是serviceImp層,上傳圖片的jsp和controller省略了:
@Override
public int insertOrUpdateAdminInfo(AdminInfo admin, MultipartFile file) {
int result=0;
if(file!=null){
String newFileName = file.getOriginalFilename();
String path="/hd/img";
File fileRoot = new File(path);
if (!fileRoot.exists()) {
fileRoot.mkdirs();
}
File targetFile = new File(fileRoot.getAbsolutePath(), newFileName);
try {
file.transferTo(targetFile);
} catch (IOException e) {
return 0;
}
String savePath=path+"/"+newFileName;
admin.setADMIN_IMG(savePath);
result = adminInfoMapper.updateAdminInfo(admin);
}
return result;
}
3:上傳到資料庫中圖片的路徑:/hd/img/Koala.jpg
4:jsp讀取頁面:
<img src='${ctx}/getAdminPicture' style="width:50px"/>
5:controller讀取方法:
/*
* 獲取管理員頭像
* @return
* @throws SQLException
*/
@RequestMapping(value = "getAdminPicture")
public void getAdminPicture(HttpServletRequest request, HttpServletResponse response) {
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
AdminInfo admin = (AdminInfo) session.getAttribute("adminInfo");
try {
imgUtil.queryPic(admin.getADMIN_IMG(),request,response);
} catch (IOException e) {
e.printStackTrace();
}
}
6:ImgUtil公共類:
package com.hdys.www.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Component
public class ImgUtil {
/**
* 讀資料庫,獲取圖片輸入流
* @param id
* @return
* @throws SQLException
*/
public FileInputStream query_getPhotoImageBlob(String adress) {
FileInputStream is = null;
File filePic = new File(adress);
try {
is = new FileInputStream(filePic);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
/*
* 獲取圖片並顯示在頁面
* @return
* @throws SQLException
*/
@RequestMapping(value = "queryPic")
public void queryPic(@RequestParam(required=false) String adress,HttpServletRequest request, HttpServletResponse response) throws IOException {
if (adress != null){
response.setContentType("image/jpeg");
FileInputStream is =this.query_getPhotoImageBlob(adress);
if (is != null){
int i = is.available(); // 得到檔案大小
byte data[] = new byte[i];
is.read(data); // 讀資料
is.close();
response.setContentType("image/jpeg"); // 設定返回的檔案型別
OutputStream toClient = response.getOutputStream(); // 得到向客戶端輸出二進位制資料的物件
toClient.write(data); // 輸出資料
toClient.close();
}
}
}
}