Struts2 随笔1
生活随笔
收集整理的這篇文章主要介紹了
Struts2 随笔1
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
配置攔截器
<interceptors><interceptor-stack name="mystack"><!-- 默認的攔截器 --><interceptor-ref name="struts-default"></interceptor-ref><!--查看每個運行action時間 --><interceptor-ref name="timer"></interceptor-ref></interceptor-stack></interceptors><!-- 修改默認攔截器(要在action之前) --><default-interceptor-ref name="mystack"></default-interceptor-ref>
//文件類型<param name="contentType">${fileType}</param>
//文件大小<param name="contentLength">${fileLength}</param><param name="contentDisposition">attachment;filename="${fileName}"</param><param name="contentCharSet">UTF-8</param>
//指向getInputstream<param name="inputName">inputstream</param></result></action>//actionpublic String download() {return SUCCESS;}public InputStream getInputstream() throws FileNotFoundException {fileName = "zhangqingfeng.doc";File file = new File("E:/Download/",fileName);fileType = map.get(file.getName().substring(file.getName().trim().lastIndexOf(".")));fileLength = file.length();return new FileInputStream(file);}
?
在struts.xml文件中,有時候打一個 < 卻沒有提示,原因是因為沒有連接互聯網,訪問不到"http://struts.apache.org/dtds/struts-2.3.dtd"這個路徑 解決方法:1、連接到互聯網,工具會自動下載使用2、在MyEclipse中配置一下 配置步驟如下: Window---?Preferences--?MyEclipse Enterprise …. ---?XML-----?XML Catalog --? 然后點擊Add按鈕,添加文件。Location 添加的是struts-2.3.dtd文件的路徑,Key Type選擇URL,KEY中的內容為http://struts.apache.org/dtds/struts-2.3.dtd,選擇OK. 注意:在配置完成之后,struts.xml文件中可能會有一個紅叉,按一下回車鍵,然后在保存一下就可以了?
<!--公共的跳轉 --><global-results><result name="logins" type="redirectAction"><param name="actionName">login</param><!-- login?code=1001 --><param name="code">1001</param></result></global-results>?
文件下載 <action name="download" class="com.peng.action.UserAction" method="download"><result type="stream">//文件類型<param name="contentType">${fileType}</param>
//文件大小<param name="contentLength">${fileLength}</param><param name="contentDisposition">attachment;filename="${fileName}"</param><param name="contentCharSet">UTF-8</param>
//指向getInputstream<param name="inputName">inputstream</param></result></action>//actionpublic String download() {return SUCCESS;}public InputStream getInputstream() throws FileNotFoundException {fileName = "zhangqingfeng.doc";File file = new File("E:/Download/",fileName);fileType = map.get(file.getName().substring(file.getName().trim().lastIndexOf(".")));fileLength = file.length();return new FileInputStream(file);}
?
//文件上傳 <action name="upload" class="com.peng.action.UserAction" method="upload"><result type="redirectAction"><param name="actionName">home</param></result></action>//action public String upload() throws Exception{System.out.println("Desc: " + desc);//獲取文件名稱為臨時文件的名稱System.out.println("Pic Name: " + pic.getName());System.out.println("Pic length :" + pic.length());//獲取文件真正的文件名System.out.println("File Name:" + picFileName);System.out.println("文件類型:" + picContentType);InputStream input = new FileInputStream(pic);OutputStream out = new FileOutputStream(new File("E:/Download/",UUID.randomUUID().toString()+picFileName.substring(picFileName.lastIndexOf("."))));byte[] buffer = new byte[1024]; int len = -1;while ((len=input.read(buffer))!=-1) {out.write(buffer,0,len);}out.flush();out.close();input.close();return SUCCESS;}?
轉載于:https://www.cnblogs.com/fudapeng/p/3805396.html
總結
以上是生活随笔為你收集整理的Struts2 随笔1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jquery获取select,dropd
- 下一篇: Linux:编译动态库时遇到的错误rel