日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

项目实例改编:利用structs2的action 实时显示图片、pdf和其他内容的框架抽取。(转)...

發(fā)布時(shí)間:2023/12/13 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 项目实例改编:利用structs2的action 实时显示图片、pdf和其他内容的框架抽取。(转)... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)自:http://www.verydemo.com/demo_c167_i1382.html

針對(duì):預(yù)覽文件(圖片,PDF)文件來(lái)源為action中的inputStream

重點(diǎn):

  • ??? structs2的action的配置
  • ??? action的寫(xiě)法和結(jié)果類(lèi)型
  • ??? resulttype的寫(xiě)法
  • ??? 網(wǎng)頁(yè)上實(shí)時(shí)顯示
  • ??1?structs2的action的配置

    ?

      首先在package的標(biāo)簽中加入自定義的結(jié)果類(lèi)型<result-types>的名字displayResult,以及后面提到的自定義類(lèi)DisplayFileResult,雖然不配置也不影響用戶(hù)體驗(yàn),但 structs默認(rèn)的結(jié)果類(lèi)型沒(méi)有直接適合的,例如使用stream會(huì)打印結(jié)果異常,如果不想異常,就在代碼中不要調(diào)用inputStream的close方法。

    <package name="file" extends="structs-default"> ......<result-types><result-type name="displayResult"class="ssc.net.cn.ecp.portal.bl.file.result.DisplayFileResult" /></result-types>......</package>

    ? ?在?? <result-types>后配置圖片資源的action。見(jiàn)??<action name="showImageContent" > ,在成功結(jié)果類(lèi)型的type屬性上加上displayResult

    <package name="file" extends="structs-default"> ......<result-types><result-type name="displayResult"class="ssc.net.cn.ecp.portal.bl.file.result.DisplayFileResult" /></result-types><action name="showImageContent"class="ssc.net.cn.ecp.portal.bl.file.action.ShowFileContent" method="showImage"><result name="success" type="displayResult"></result><result name="error" /></action><action name="showPdfContent"class="ssc.net.cn.ecp.portal.bl.file.action.ShowFileContent" method="showPdf"><result name="success" type="displayResult"></result><result name="error"/></action>.</package>

    ?

    2 action的寫(xiě)法和結(jié)果類(lèi)型。

      此處給出一個(gè)框架式的寫(xiě)法,加入最少兩個(gè)方法getInputStream()和getContentType(),供DisplayFileResult類(lèi)使用。

    import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport;public class ShowFileContent extends ActionSupport {private InputStream inputStream;public InputStream getInputStream() {return inputStream;}public void setInputStream(InputStream inputStream) {this.inputStream = inputStream;}private String contentType;public String getContentType() {return contentType;}public void setContentType(String contentType) {this.contentType = contentType;}public String showImage() {try {HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);String filePath = request.getParameter("filepath");this.setInputStream(new java.io.FileInputSteam(filePath));this.setContentType("image/png");}catch (IOException e) {return ERROR}return SUCCESS; }public String showPdf() {try {HttpServletRequest request= (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);String filePath = request.getParameter("filepath");this.setInputStream(new java.io.FileInputSteam(filePath));this.setContentType("application/pdf");}catch (IOException e){return ERROR}return SUCCESS;}}

    3 result type的寫(xiě)法

      調(diào)用response相關(guān)方法,把輸出流轉(zhuǎn)換為資源方式。在這里,我再老生長(zhǎng)談一句話(huà):一定要切記關(guān)閉流,如果不關(guān)閉流,在打開(kāi)過(guò)多流后操作系統(tǒng)(Linux)會(huì)報(bào)類(lèi)似“too many open files”之類(lèi)的錯(cuò)誤,導(dǎo)致無(wú)法訪(fǎng)問(wèn)文件。

    package ssc.net.cn.ecp.portal.bl.file.result;import java.io.IOException;import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.Result;public class DisplayFileResult implements Result {private static final long serialVersionUID = 4897546905647587338L;private HttpServletResponse response;ShowFileContent action;public void execute(ActionInvocation invocation) throws Exception {init(invocation);writeResponseOutputStream();}private void init(ActionInvocation invocation) {action = (ShowFileContent) invocation.getAction();response = ServletActionContext.getResponse();response.setContentType(action.getContentType());}private void writeResponseOutputStream() {java.io.InputStream is = action.getInputStream();java.io.BufferedInputStream bi = null;if (is == null) {return;}try {bi = new java.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 網(wǎng)頁(yè)上實(shí)時(shí)顯示

      給個(gè)顯示圖片的例子:實(shí)時(shí)顯示圖片,參數(shù)需要加入new date(), 瀏覽器就會(huì)認(rèn)為是一個(gè)新的圖片地址,而不會(huì)調(diào)用瀏覽器緩存顯示圖片

    <img src="showImageContent.action?filepath=mypic.gif‘&date="+String(new date()) />

    ?

    給個(gè)pdf顯示的例子:

    <iframe src="showPdfContent.action?filepath=mypic.gif‘&date="+String(new date()) />

    ?

    ?

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/tianhyapply/p/4394506.html

    總結(jié)

    以上是生活随笔為你收集整理的项目实例改编:利用structs2的action 实时显示图片、pdf和其他内容的框架抽取。(转)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。