项目实例改编:利用structs2的action 实时显示图片、pdf和其他内容的框架抽取。(转)...
转自:http://www.verydemo.com/demo_c167_i1382.html
针对:预览文件(图片,PDF)文件来源为action中的inputStream
重点:
- structs2的action的配置
- action的写法和结果类型
- resulttype的写法
- 网页上实时显示
1 structs2的action的配置
首先在package的标签中加入自定义的结果类型<result-types>的名字displayResult,以及后面提到的自定义类DisplayFileResult,虽然不配置也不影响用户体验,但 structs默认的结果类型没有直接适合的,例如使用stream会打印结果异常,如果不想异常,就在代码中不要调用inputStream的close方法。
<packagename="file"extends="structs-default">......<result-types><result-typename="displayResult"class="ssc.net.cn.ecp.portal.bl.file.result.DisplayFileResult" /></result-types>......</package>
在 <result-types>后配置图片资源的action。见 <action name="showImageContent" > ,在成功结果类型的type属性上加上displayResult
<packagename="file"extends="structs-default">......<result-types><result-typename="displayResult"class="ssc.net.cn.ecp.portal.bl.file.result.DisplayFileResult" /></result-types><actionname="showImageContent"class="ssc.net.cn.ecp.portal.bl.file.action.ShowFileContent"method="showImage"><resultname="success"type="displayResult"></result><resultname="error" /></action><actionname="showPdfContent"class="ssc.net.cn.ecp.portal.bl.file.action.ShowFileContent"method="showPdf"><resultname="success"type="displayResult"></result><resultname="error"/></action>.</package>
2 action的写法和结果类型。
此处给出一个框架式的写法,加入最少两个方法getInputStream()和getContentType(),供DisplayFileResult类使用。
importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;public class ShowFileContent extendsActionSupport {privateInputStream inputStream;publicInputStream getInputStream() {returninputStream;}public voidsetInputStream(InputStream inputStream) {this.inputStream =inputStream;}privateString contentType;publicString getContentType() {returncontentType;}public voidsetContentType(String contentType) {this.contentType =contentType;}publicString showImage() {try{HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);String filePath= request.getParameter("filepath");this.setInputStream(newjava.io.FileInputSteam(filePath));this.setContentType("image/png");}catch(IOException e) {returnERROR}returnSUCCESS; }publicString showPdf() {try{HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);String filePath= request.getParameter("filepath");this.setInputStream(newjava.io.FileInputSteam(filePath));this.setContentType("application/pdf");}catch(IOException e){returnERROR}returnSUCCESS;}}
3 result type的写法
调用response相关方法,把输出流转换为资源方式。在这里,我再老生长谈一句话:一定要切记关闭流,如果不关闭流,在打开过多流后操作系统(Linux)会报类似“too many open files”之类的错误,导致无法访问文件。
packagessc.net.cn.ecp.portal.bl.file.result;importjava.io.IOException;importjavax.servlet.http.HttpServletResponse;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionInvocation;importcom.opensymphony.xwork2.Result;public class DisplayFileResult implementsResult {private static final long serialVersionUID = 4897546905647587338L;privateHttpServletResponse response;ShowFileContent action;public void execute(ActionInvocation invocation) throwsException {init(invocation);writeResponseOutputStream();}private voidinit(ActionInvocation invocation) {action=(ShowFileContent) invocation.getAction();response=ServletActionContext.getResponse();response.setContentType(action.getContentType());}private voidwriteResponseOutputStream() {java.io.InputStream is=action.getInputStream();java.io.BufferedInputStream bi= null;if (is == null) {return;}try{bi= newjava.io.BufferedInputStream(is);byte[] bytearray = new byte[1024];int size = 0;while ((size = bi.read(bytearray)) != -1) {response.getOutputStream().write(bytearray,0, size);}}catch(IOException e) {e.printStackTrace();}finally{try{response.flushBuffer();}catch(IOException e) {e.printStackTrace();}try{bi.close();}catch(IOException e) {e.printStackTrace();}try{is.close();}catch(IOException e) {e.printStackTrace();}}}}
4 网页上实时显示。
给个显示图片的例子:实时显示图片,参数需要加入new date(), 浏览器就会认为是一个新的图片地址,而不会调用浏览器缓存显示图片。
<img src="showImageContent.action?filepath=mypic.gif‘&date="+String(new date()) />
给个pdf显示的例子:
<iframe src="showPdfContent.action?filepath=mypic.gif‘&date="+String(new date()) />
转载于:https://www.cnblogs.com/tianhyapply/p/4394506.html
项目实例改编:利用structs2的action 实时显示图片、pdf和其他内容的框架抽取。(转)...相关推荐
- android怎么让图片显示在button上面_opencv怎么样可以实时显示图片HSV值
标题--opencv怎么样可以实时显示图片HSV值 我们在调试图像处理方面的程序时,经常要看HSV数值的范围,因为HSV做颜色阈值分割效果更好一些.下面就是一个能够实时将图片中某一点数据的HSV数据的 ...
- 利用opencv+QT打开并显示图片
``` // 利用opencv+QT打开并显示图片// 头文件在添加 #include "qlabel.h" // opencv #include <opencv2\core ...
- 利用Handler通信来实时显示系统时间
关键技术: 本程序实现了在屏幕中实时显示系统的时间,其中用到了Handler.Timer.TimerTask.Message等关键技术. 思路分析: 我们都知道,在Android中程序不能在主线程中( ...
- python实时显示图片_任何显示来自Cam的实时图像的快速Python GUI
我建议使用Tkinter,因为它已经是python的一部分了.我从来没有使用过PIL,但是一个快速的google显示在Tk小部件中使用PIL图像很容易(通过PIL.ImageTk.PhotoImage ...
- 【JS30-Wes Bos】实时显示的时钟网页 02
引言 本文利用javascript写一个实时显示时间的时钟特效网页. 网址为(https://janice143.github.io/realTImeClock/) 正文 1网页布局与功能 网页主体为 ...
- 深度相机的图像深度实时显示
最近有了一个intel 的 D435i 深度相机,网上找了一个实时显示图片和深度图的python 程序,最开始是一个博客,后来发现在intel 的github里也有.都找不到链接了,觉得不错,修改整理 ...
- PHP中利用EXIF函数集来显示单反照片的EXIF信息
要成功做到能用EXIF函数显示单反EXIF信息需要一下几个不步骤: 1.修改php.ini的exif相关信息 2.继续修改php.ini信息,找到以下模块,将带exif的配置属性前的;号去掉,然后重启 ...
- Jquery循环显示图片
因为在一个页面有好多图片需要上传 选择之后还要实时显示图片 这个是form的input框 $('input[type="file"]').change(function(e){va ...
- php显示网络图片,php中显示图片的方法
php中显示图片的方法 发布时间:2020-08-25 14:08:07 来源:亿速云 阅读:80 作者:小新 这篇文章给大家分享的是有关php中显示图片的方法的内容.小编觉得挺实用的,因此分享给大家 ...
最新文章
- Node.js基金会官方的开发者认证准备就绪
- linux格式化文件,无需格式化就可以转换文件系统
- 参数化的JUnit测试
- python中使用什么导入模块-python—模块导入和类
- C/C++ atol函数- C语言零基础入门教程
- datables自定义ajax,JQuery DataTables.net自定义列宽度在ajax加载后不起作用
- 计算机网络协议是网民们签订的合同,关于网络协议,下列__________选项是正确的。A.是网民们签订的合同B.是计算机之间的相互通信需...
- JavaScript(三)数据类型转换
- iOS入门培训还要钱?看博客,看视频都拿下
- Linux下压缩mp3文件
- 转载 刘鹏老师 计算广告学 人大笔记
- Android应用开发自定义图片剪辑头像设置
- Kubernetes init 提示 [ERROR ImagePull]: failed to pull image registry.aliyuncs.com/google_containers/c
- 迄今为止见过最好的职业规划
- gradle 设备未就绪。解决方法
- vue3结合ts开发,导入vue文件,页面报红色的波浪线
- Vue中文字只显示三行,超过部分收起的实现方法
- 通信系统原理[郭宇春]——二元数字频带传输——课后习题答案
- P163、面试题29:数组中出现次数超过一半的数字
- codeforces 1509 B. TMT Document