Struts2中文件上傳下載實例

1.單文件上傳

 1 jsp頁面:
 2 
 3 <!-- 單文件上傳 -->
 4         <form action="Fileupload.action" method="post"
 5             enctype="multipart/form-data">
 6             username:
 7             <input type="text" name="username" />
 8             <br />
 9             file:
10             <input type="file" name="file" />
11             <br />
12             <input type="submit" name="提交" />
13         </form>
14 <!--注意:一定要寫enctype="multipart/form-data"-->
 1 action頁面:
 2 
 3 package com.Struts2.action;
 4 
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileOutputStream;
 8 import java.io.InputStream;
 9 import java.io.OutputStream;
10 
11 import org.apache.struts2.ServletActionContext;
12 
13 import com.opensymphony.xwork2.ActionSupport;
14 
15 public class Fileupload extends ActionSupport {
16 
17     /**
18      * 
19      */
20     private static final long serialVersionUID = 4038542394937191173L;
21 
22     private String username;
23     private File file;
24     private String fileFileName;// 提交過來的file的名字,必須為FileName后綴
25     private String fileContentType;// 提交過來的file的MIME類型,必須以ContentType為后綴
26 
27     public String getUsername() {
28         return username;
29     }
30 
31     public void setUsername(String username) {
32         this.username = username;
33     }
34 
35     public File getFile() {
36         return file;
37     }
38 
39     public void setFile(File file) {
40         this.file = file;
41     }
42 
43     public String getFileFileName() {
44         return fileFileName;
45     }
46 
47     public void setFileFileName(String fileFileName) {
48         this.fileFileName = fileFileName;
49     }
50 
51     public String getFileContentType() {
52         return fileContentType;
53     }
54 
55     public void setFileContentType(String fileContentType) {
56         this.fileContentType = fileContentType;
57     }
58 
59     @Override
60     public String execute() throws Exception {
61         String root = ServletActionContext.getServletContext().getRealPath(
62                 "/upload");
63         InputStream is = new FileInputStream(file);
64         OutputStream os = new FileOutputStream(new File(root, fileFileName));
65 
66         System.out.println("fileFileName:" + fileFileName);
67         // 因為file是存放在臨時文件夾的文件,我們可以將其文件名和文件路徑打印出來,看和之前的fileFileName是否相同
68         System.out.println("file:" + file.getName());
69         System.out.println("file:" + file.getPath());
70 
71         byte[] buffer = new byte[500];// 用緩沖
72         int length = 0;
73         while (-1 != (length = is.read(buffer, 0, buffer.length))) {
74             os.write(buffer);
75         }
76         os.close();
77         is.close();
78 
79         return SUCCESS;
80     }
81 }

?

2.多文件上傳

 1 jsp頁面:
 2 
 3 <script type="text/javascript">
 4             $(function() {
 5             $("#button").click(function (){
 6                 var html = $("<input type='file' name='file'/><br/>");
 7                 var button = $("<input type='button' name='button' value='刪除'/><br/>");
 8                 $("#body div").append(html).append(button);
 9                     button.click(function (){
10                         html.remove();
11                         button.remove();            
12                         });
13             });
14         });
15         </script>
16 <body id="body">
17 <!-- 多文件上傳 -->
18         <form action="manyFileupload.action" method="post"
19             enctype="multipart/form-data">
20             username:
21             <input type="text" name="username" />
22             <br />
23             file:
24             <input type="file" name="file" />
25             <br />
26             <input type="button" value="添加" id="button" />
27             <br />
28             <div></div>
29             <input type="submit" value="提交" />
30         </form>
31     </body>
action頁面:package com.Struts2.action;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/*** 多文件上傳* * @author admin* */
public class ManyFileupload extends ActionSupport {/*** */private static final long serialVersionUID = -1060365794705605882L;private String username;private List<File> file;// 因為有多個文件,所以用list集合,file屬性指上傳到的臨時空間文件夾(upload)中的絕對路徑private List<String> fileFileName;// 上傳的文件名稱,自動會加載,因為是以FileName為后綴,會自動識別文件名稱private List<String> fileContentType;// 上傳過來的文件類型,以ContentType后綴,自動識別類型(如:圖片類型就是image/jepg)public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public List<File> getFile() {return file;}public void setFile(List<File> file) {this.file = file;}public List<String> getFileFileName() {return fileFileName;}public void setFileFileName(List<String> fileFileName) {this.fileFileName = fileFileName;}public List<String> getFileContentType() {return fileContentType;}public void setFileContentType(List<String> fileContentType) {this.fileContentType = fileContentType;}@Overridepublic String execute() throws Exception {String root = ServletActionContext.getServletContext().getRealPath("/upload");for (int i = 0; i < file.size(); i++) {InputStream is = new FileInputStream(file.get(i));OutputStream os = new FileOutputStream(new File(root, fileFileName.get(i)));byte[] buffer = new byte[500];int length = 0;while (-1 != (length = is.read(buffer, 0, buffer.length))) {os.write(buffer);}os.close();is.close();}return super.execute();}}

3.文件下載

1 jsp頁面
2 
3         <!-- 下載 -->
4         <form action="fileDownload.action">
5             <input type="submit" value="下載" />
6         </form>
7     
action頁面package com.Struts2.action;import java.io.InputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/*** 文件下載* * @author admin* */
public class FileDownload extends ActionSupport {/*** */private static final long serialVersionUID = 509550154000070698L;public InputStream getDownloadFile() {return ServletActionContext.getServletContext().getResourceAsStream("upload/psb.jpg");// 從upload中選擇一張圖片進行下載
    }@Overridepublic String execute() throws Exception {return super.execute();}
}

4.struts.xml

 1     <!-- 定義全局結果:必須寫在action上面 -->    
 2     <global-results>
 3         <result name="success">/success.jsp</result>
 4     </global-results>
 5     
 6     <!-- 單文件上傳 -->
 7     <action name="Fileupload" class="com.Struts2.action.Fileupload"></action>
 8     <!-- 多文件上傳-->
 9     <action name="manyFileupload" class="com.Struts2.action.ManyFileupload"></action>
10     <!-- 文件下載 -->
11     <action name="fileDownload" class="com.Struts2.action.FileDownload">
12         <result name="success" type="stream">
13              <param name="contentDisposition">attachment;filename="psb.jpg"</param><!-- attachment表示彈出下載提示框 -->
14              <param name="inputName">downloadFile</param><!-- downloadFile對應action中的getDownloadFile方法 -->
15         </result>
16     </action>    

?

?

本博客源碼出自:http://www.cnblogs.com/xiaoluo501395377/archive/2012/10/26/2740882.html。謝謝他的分享!

?

轉載于:https://www.cnblogs.com/lirenzhujiu/p/5749303.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/256642.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256642.shtml
英文地址,請注明出處:http://en.pswp.cn/news/256642.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

最優化課堂筆記06-無約束多維非線性規劃方法(續)

6.5共軛方向法 6.5.1 共軛方向 6.5.1 共軛梯度法 6.6單純形法(不考) 6.7最小二乘法 6.7.2 改進的高斯-牛頓最小二乘法

opengl微發展理解

1.什么是OpenGL? 一種程序&#xff0c;可以與界面和圖形硬件交互作用、一個開放的標準 2.軟件管道 請看上圖 - Apllication層 表示你的程序&#xff08;調用渲染命令。如opengl API&#xff09; -Abstraction層 表示畫圖接口&#xff08;如OpenGL API或者DirectX API&a…

MacosX 下GCC編譯指定版本的代碼

export MACOSX_DEPLOYMENT_TARGET10.6轉載于:https://www.cnblogs.com/lovelylife/p/5754226.html

最優化作業第六章——共軛梯度法和鮑爾法

共軛梯度法&#xff1a; 代碼&#xff1a; #導入模塊 from sympy import * import sympy as sp #將導入的模塊重新定義一個名字以便后續的程序進行使用 from numpy import * import numpy as npdef main():#本例是利用共軛梯度法進行最優化x1,x2,alpha symbols("x1,x2,…

酒鬼隨機漫步(一個矢量類)

摘要: 閱讀全文這是一個定義的一個矢量類&#xff0c; 然后用矢量類模擬一個酒鬼的隨機漫步 問題很簡單&#xff0c; 實現也不麻煩&#xff0c; 但是這個小程序卻可以呈現出許多語法知識。而且代碼風格也不錯&#xff0c;因此保存在了這篇博客中。 建議&#xff1a; 1. 類的聲…

對高并發流量控制的一點思考

前言 在實際項目中&#xff0c;曾經遭遇過線上5WQPS的峰值&#xff0c;也在壓測狀態下經歷過10WQPS的大流量請求&#xff0c;本篇博客的話題主要就是自己對高并發流量控制的一點思考。 應對大流量的一些思路 首先&#xff0c;我們來說一下什么是大流量&#xff1f; 大流量&…

ndk學習19: 使用Eclipse調試so

1. 設置調試選項在AndroidManifest文件加入允許調試android:debuggable"true" 此時編譯項目會多出:2. 配置調試代碼把需要調試的代碼,放如按鈕事件中,如果放在OnCreate會導致連接調試器時,代碼已經跑完了Button btnTest (Button)findViewById(R.id.button1);btnT…

Inside the C++ Object Model | Outline

《Inside the C Object Model&#xff08;C對象模型&#xff09;》&#xff0c;這是一本灰常不錯的書&#xff01; CSDN下載頁面&#xff08;中文&#xff0c;侯捷譯&#xff09; 豆瓣評論 讀書筆記目錄如下&#xff08;不定時更新&#xff09;&#xff1a; 轉載于:https://www…

最優化課程筆記07——約束問題的非線性規劃方法(重點:拉格朗日乘子法和懲罰函數法)

7.1 間接法&#xff1a;約束轉化為無約束問題&#xff08;含一個重點&#xff1a;拉格朗日乘子法&#xff09; 當維數多的時候不適用 7.1.2拉格朗日乘子法&#xff08;重點&#xff09; 7.1.2.1 等式約束問題 7.1.2.2 不等式約束問題 7.1.3 懲罰函數法&#xff08;內懲罰函數法…

工業相機:傳感器尺寸與像元尺寸的關系

相同分辨率的工業相機&#xff0c;傳感器面積越大&#xff0c;則其單位像素的面積也越大&#xff0c;成像質量也會越好。同樣的500萬像素的工業相機&#xff0c;2/3”的傳感器成像質量就要優于1/2”的。一般來說&#xff0c;工業相機的靶面大小&#xff0c;如果要求不是太嚴格&…

macOS下安裝ipython

macOS下sudo安裝ipython&#xff0c;會提示限錯誤&#xff1a; [Errno 1] Operation not permitted: /tmp/pip-Elrhse-uninstall/System/Library... 解決方法&#xff1a; pip install ipython --user -U 參考&#xff1a; http://chaishiwei.com/blog/994.html 本文轉自 h2app…

結構化查詢語言包含哪些方面?

結構化查詢語言SQL&#xff08;STRUCTURED QUERY LANGUAGE&#xff09;是最重要的關系數據庫操作語言&#xff0c;并且它的影響已經超出數據庫領域&#xff0c;得到其他領域的重視和采用&#xff0c;如人工智能領域的數據檢索&#xff0c;第四代軟件開發工具中嵌入SQL的語言等。…

Opencv 找輪廓并畫出相應的矩形

找輪廓參考以下大神的&#xff0c;對于里面的方法和結果存儲解釋的很清楚&#xff1b; http://blog.csdn.net/gubenpeiyuan/article/details/44922413 缺少的是畫相應包圍矩形的&#xff0c;其中找矩形用最小外接矩形函數cvMinAreaRect2 。 CvBox2D rect; CvPoint2D32f Corner…

C# 圖片識別(支持21種語言)

圖片識別的技術到幾天已經很成熟了&#xff0c;只是相關的資料很少&#xff0c;為了方便在此匯總一下&#xff08;C#實現&#xff09;&#xff0c;方便需要的朋友查閱&#xff0c;也給自己做個記號。 圖片識別的用途&#xff1a;很多人用它去破解網站的驗證碼&#xff0c;用于達…

搭建Git Server - Centos+Gitosis

參考并部分轉載自&#xff1a;http://www.pfeng.org/archives/757 1. 安裝依賴 yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel git python python-setuptools2. 安裝gitosis git clone git://github.com/res0nat0r/gitosis.git cd…

php中rsa加密及解密和簽名及驗簽

加密的內容長度限制為密鑰長度少11位,如128位的密鑰最多加密的內容為117個長度。 公鑰加密    $public_contentfile_get_contents(公鑰路徑);    $public_keyopenssl_get_publickey($public_content);        $original_str待加密的內容;    $original_arr…

Opencv ---像素坐標轉世界坐標(已知外參)

只能求取已知外參的世界坐標平面上的世界坐標&#xff0c;具體公式如圖片所示&#xff01; PS&#xff1a;字丑請諒解&#xff01;

最優化5-8章重點(考試點全)

10道題&#xff0c;每道題10分&#xff0c;5-8章大概4題左右&#xff0c;后面的章節主要考的是概念題

多對多關聯映射(雙向)

關聯映射方面的最后一篇了&#xff0c;我覺得映射文件的編寫是使用hibernate的基礎&#xff0c;而關聯映射又是基礎的基礎&#xff0c;所以這方面分的細一些&#xff0c;羅嗦一些&#xff0c;說明白就好&#xff0c;呵呵。多對多關聯(雙向)&#xff0c;相對單向&#xff0c;在實…