jsp中显示服务器上的图片,并使其居中显示

在WEB应用中是不能直接访问本地文件,通常都是放到工程之中。

但是如果项目上线,从服务器上获取文件,那就不得不用io流去进行传输读取了。

最近,碰到一个问题,就是虽然用io流将图片读取到jsp页面了,但是无法使其居中。

我的java代码写在jsp,然后没办法了,将代码放入action中,最终图片读取出来并且居中显示了,但是总是报:Stream Closed     io异常!


最后,全部删除了,回过头来还是jsp里边搞吧!!!

1.创建展示图片的jsp-->ShowPicture.jsp
<body>
<div align="center">
<img src="<%=request.getContextPath()%>/jsp/picture.jsp?filepath=<%=request.getParameter("filepath")%>&filetype=<%=request.getParameter("filetype")%>" />
</div>

2.ShowPicture.jsp调用的picture.jsp
<%@ page import="java.util.*,java.io.*">
<%
out.clear();
out = pageContext.pushBody();
//response.setContentType("application/pdf");

 try {
	 String strPdfPath = new String(request.getParameter("filepath"));
	 String strfiletype = new String(request.getParameter("filetype"));
	 //判断是pdf文件还是jpg文件
	 if(strfiletype!=null && !strfiletype.equals("")){
		 if(strfiletype.equals("pdf")){
			 response.setContentType("application/pdf;charset=GB2312");
		 }else if(strfiletype.equals("jpg")){
			 response.setContentType("image/gif;charset=GB2312");
		 }
	 }
	//判断该路径下的文件是否存在
	 File file = new File(strPdfPath);
	 if (file.exists()) {
	 DataOutputStream temps = new DataOutputStream(response
	.getOutputStream());
	 DataInputStream in = new DataInputStream(
	 new FileInputStream(file));
	
	 byte[] b = new byte[2048];
	 while ((in.read(b)) != -1) {
		temps.write(b);
		temps.flush();
	 }
	in.close();
	temps.close();
	 } else {
	 out.print(strPdfPath +"文件不存在!");
	}

 } catch (Exception e) {
out.println(e.getMessage());
}
%>



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QQ719872578

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值