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();  
     }  
    }  
 }


}