当前位置: 代码迷 >> Java Web开发 >> 源读完了,页面图片不显示啊
  详细解决方案

源读完了,页面图片不显示啊

热度:7453   发布时间:2013-02-25 21:09:43.0
流读完了,页面图片不显示啊?
jsp页面代码:
<img src="<%=request.getContextPath()%>/user/showPictureAction.action?accountId=${uid}" width="120" height="120" class="img" />
Action代码:
public void showPictureAction(){
// 头像显示
HttpServletResponse response = null; //声明response对象
ServletOutputStream out = null; //声明输出流对象
InputStream in = null; //声明输入流对象
byte[] bytes = null; //声明字节对象
try {
response = ServletActionContext.getResponse();
// 二进制输出流
response.setContentType("multipart/form-data");
response.setContentType("image/jpg");
// 得到输出流
out = response.getOutputStream();

if (accountId == null){
accountId = (Long) ActionContext.getContext().getSession()
.get(NovaConstants.USER_ID);
}
DarenDto darenDto = new DarenDto();
darenDto.setAccountId(accountId);
//获取新的达人对象
darenDto = darenService.getDarenById(darenDto);

if(darenDto.getReverseCard() != null){
Blob image = darenDto.getReverseCard();
  // 以流的形式检索此 Blob 实例指定的 BLOB 值。
in = image.getBinaryStream();
}else{
//设置默认头像
String path = this.getClass().getClassLoader().getResource("/").getPath();
System.out.println("默认头像存储路径:"+path);
path = path.substring(0, path.length() - 17);
in = new FileInputStream(path+"/page/face/user.png");
System.out.println(path+"/page/face/");
}
//从输入流读取数据到输出流
bytes = new byte[1024];
while (in.read(bytes) != -1 ) {
//out.write(bytes);
out.write(bytes, 0, in.read(bytes));
}
// 强制刷新输出流
out.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
bytes = null;
}
}

}

------解决方案--------------------------------------------------------
建议你去这里看看,或许对你有帮助:
http://download.csdn.net/detail/s478853630/4064638
------解决方案--------------------------------------------------------
建议你去这里看看,或许对你有帮助:
http://download.csdn.net/detail/s478853630/4064638
也是img读取后台文件流的一个列子,图片可以正常显示的。
------解决方案--------------------------------------------------------
我看不出你是用struts2做的 还是用servlet做的 详细点 如果用struts做的 配置type为stream 然后写一个返回stream的方法 然后请求指向的方法直接返回succe就行。 用servlet就更好做啦 不管是用get 还是post 给你一个例子

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
response.setHeader("Content-Type", "image/jpg");
response.setContentType("image/jpg");
File file = new File("c:/afms/Alerting.jpg");
OutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
byte b[] = new byte[1024*8];
int len = -1;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(out);
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
}
bos.flush();
} catch (Exception e) {

} finally {
if(out != null) {
out.close();
}
if(bos != null) {
bos.close();
}
}

}
  相关解决方案