关闭

spring读取指定路径图片显示到jsp页面

标签: JAVASpring MVC流 读取图片
1332人阅读 评论(0) 收藏 举报
分类:
package cn.fenghuait.controller;

import java.io.FileInputStream;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


//流读取图片

import cn.fenghuait.service.SysParameterService;


@Controller
@RequestMapping("/picture")
public class ShowPicController extends BaseController{
  private static Log logger = LogFactory.getLog(ShowPicController.class);
  @Autowired
  private SysParameterService parameterService;
 
  @RequestMapping(value="/showPic")
  public @ResponseBody Map<String,Object> showPic(HttpServletRequest request,HttpServletResponse response) throws Exception{
    try{
      String basePath = parameterService.getProblemPic();
      String str = request.getParameter("picName");
        String realName = basePath+"/"+str;
        FileInputStream fs = new FileInputStream(realName);
        int b=fs.available(); //得到文件大小   
        byte data[]=new byte[b];   
        fs.read(data);  //读数据   
        response.setContentType("image/*"); //设置返回的文件类型   
        ServletOutputStream toClient=response.getOutputStream(); //得到向客户端输出二进制数据的对象   
        toClient.write(data);  //输出数据   
        toClient.flush();
        toClient.close();
        fs.close();
    }catch(Exception e){
      e.printStackTrace();
      logger.error(e);
    }
    return null;
    
  }
}



//查询图片名称

info=problemInfoService.getProblemInfoById(problemInfo.getProblemId());
           List<String> list = new ArrayList<String>();
       if(info.getPicturePath()!=null&&!"".equals(info.getPicturePath().trim())){
       String str = info.getPicturePath();
       String[] strs = str.split(",");
       String pic = "";
       for(int i = 0;i<strs.length;i++){
          pic = strs[i];
         list.add(pic);
       }
       }
          mav.addObject("problem", info);
          mav.addObject("realName", list);


//jsp显示图片

    <c:forEach items="${realName }" var="item">
                <li><a  target="_blank"><img src="${basePath}/picture/showPic?picName=${item}" alt="民情图片" /></a></li>
            </c:forEach>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32615次
    • 积分:526
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:2篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论