springmvc怎么读取本地图片显示到JSP呢?我现在用的流读取。请各位大拿帮忙看下!
JSP代码:<imgsrc=“controller的请求路径”/>controller中的代码:@RequestMapping("showImage")publicvoi...
JSP代码:
<img src=“controller的请求路径” />
controller中的代码:
@RequestMapping("showImage")
public void showImage(HttpServletRequest re,HttpServletResponse response,String pic_addr){//pic_addr:图片路径(d:\\upload\a.jpg)
response.setContentType("text/html; charset=UTF-8");
response.setContentType("image/*");
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(pic_addr);
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024*8];
while ( (count = fis.read(buffer)) != -1 ){
os.write(buffer, 0, count);
os.flush();
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在的情况是方法进来了,但是好像流没返回到页面,还是返回了没起作用呢? 展开
<img src=“controller的请求路径” />
controller中的代码:
@RequestMapping("showImage")
public void showImage(HttpServletRequest re,HttpServletResponse response,String pic_addr){//pic_addr:图片路径(d:\\upload\a.jpg)
response.setContentType("text/html; charset=UTF-8");
response.setContentType("image/*");
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(pic_addr);
os = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024*8];
while ( (count = fis.read(buffer)) != -1 ){
os.write(buffer, 0, count);
os.flush();
}
}catch(Exception e){
e.printStackTrace();
}finally {
try {
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
现在的情况是方法进来了,但是好像流没返回到页面,还是返回了没起作用呢? 展开
1个回答
2014-11-03
展开全部
fis = new FileInputStream(pic_addr);
不应该是
fis = new FileInputStream(new File(pic_addr));
不应该是
fis = new FileInputStream(new File(pic_addr));
追问
你好,改成这样了也不行!请帮忙看看有没有其他问题,先谢谢!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询