| 網(wǎng)站首頁 | 關于我們 | 開發(fā)優(yōu)勢 | 產(chǎn)品展示 |
| 合作企業(yè) | 新聞動態(tài) | 聯(lián)系我們 | 電話聯(lián)系 |
文章作者:濟南軟件開發(fā) 時間:2016年11月08日
通過struts2提供的fileUpload攔截器可以方便的實現(xiàn)文件的上傳。那么我們來簡單的實現(xiàn)以下:
首先,我們新建一個action
復制代碼
1 public class ToUploadAction extends ActionSupport {
2
3 private File fileUpload;
4
5 private String fileUploadFileName;//文件的名稱 如上傳的文件是a.png 則fileuploadFileName值為"a.png"
6 private String fileUploadContentType;//文件的類型 如上傳的是png格式的圖片,則fileuploadContentType值為"image/png"
7 /*
8 * 指定上傳文件在服務器上的保存目錄,需要在Action中為定義savePath變量并為其添加相應的setter和getter方法
9 * 便于Struts2將struts.xml中的<param name="savePath">uploads/</param>值賦給savePath屬性
10 * <param>的作用就是為Action中的某些屬性賦一個默認值,通常這樣做的如配置路徑、文件名之類的.... 傳遞參數(shù)
11 */
12 private String savePath;//文件的保存位置 ,是在struts.xml中配置的
13
14 /**
15 *
16 */
17 private static final long serialVersionUID = 8750193097637832895L;
18
19 public String fileUpload() throws IOException{
20
21 String absolutePath = ServletActionContext.getServletContext().getRealPath(""); // 獲取項目根路徑
22 //文件路徑
23 String path = absolutePath + "/" + this.savePath + "/";
24 //創(chuàng)建路徑,如果目錄不存在,則創(chuàng)建
25 File file = new File(path);
26 if(!file.exists()) {
27 file.mkdirs();
28 }
29 //文件路徑+文件名
30 path +=this.getFileUploadFileName();
31 //1.構(gòu)建輸入流
32 FileInputStream fis = new FileInputStream(fileUpload);
33 //2.構(gòu)建輸出流
34 FileOutputStream fos = new FileOutputStream(path);
35 //3.通過字節(jié)寫入輸出流
36 try {
37 byte[] buf = new byte[1024];
38 int len = 0;
39 while ((len = fis.read(buf)) > 0) {
40 fos.write(buf, 0, len);
41 }
42 } catch (Exception e) {
43 e.printStackTrace();
44 } finally {
45 fis.close();
46 fos.close();
47 }
48 return SUCCESS;
49 }
復制代碼
其中需要注意的就是,前面四個屬性名稱。fileUpload是通過表單傳到后臺的文件對象,fileUploadFileName是代表的上傳文件的名稱,屬性名必須以前面文件對象的屬性名fileUpload + FileName 來命名。否則一定無法接受到參數(shù),不能獲取文件名。同時fileUploadContentType也是同樣的道理。savePath這個屬性代表我們上傳之后的存放地址的目錄名。這個參數(shù)需要到struts.xml中進行配置,且需要命名一致。
當我們的控制層完成了之后就可以寫我們的struts.xml配置文件。當然我們需要用到fileUpload這個攔截器:
復制代碼
1 <struts>
2
3 <constant name="struts.devMode" value="true" />
4 <package name="fileUpload" extends="struts-default" namespace="/file">
5
6 <action name="fileUpload" class="com.deppon.file.ToUploadAction" method="fileUpload">
7 <interceptor-ref name="fileUpload">
8 <param name="allowedTypes">
9 image/bmp,image/png,image/gif,image/jpeg,image/jpg
10 </param>
11 <param name="maximumSize">1024*1024</param>
12 </interceptor-ref>
13 <interceptor-ref name="defaultStack" />
14 <param name="savePath">uploads</param>
15 <result name="success">/success.jsp</result>
16 <result name="input">/fileUpload.jsp</result>
17 </action>
18
19 </package>
20
21 </struts>
復制代碼
我們必須也要加上struts默認的defaultStack這個攔截器,且要放在fileUpload攔截器的后面。這里的savePath參數(shù)配置的就是目錄名,和action中的屬性對應,這樣action中 就可以取到我們配置的文件名。
最后來到了我們的展示層:
1 <s:form action="./file/fileUpload.action" method ="POST" enctype ="multipart/form-data">
2 <s:file name ="fileUpload"/>
3 <s:submit value="上傳" label="upload"/>
4 </s:form>
必須提供method 和 enctype這兩個屬性,且不能改變,這樣才能上傳文件格式。這樣我們就基本實現(xiàn)了文件的上傳功能。如果我們需要上傳多個文件,只需要修改如下屬性:
1 private List<File> image;//上傳的文件
2 private List<String> imageFileName;//文件的名字
3 private List<String> imageContentType;//文件的類型
然后在方法中進行循環(huán)遍歷,同時form表單的file元素的name屬性一定要全部相同,這樣struts2就會自動為我們進行處理。如下:
1 <s:file name="image" label="圖片"></s:file>
2
3 <s:file name="image" label="圖片"></s:file>
4
5 <s:file name="image" label="圖片"></s:file>
循環(huán)遍歷文件對象和文件名即可。
復制代碼
1 public String fileUpload() throws IOException{
2 int i = 0;
3 for(File f : getFileUpload()){
4 String absolutePath = ServletActionContext.getServletContext().getRealPath(""); // 獲取項目根路徑
5 //文件路徑
6 String path = absolutePath + "/" + this.savePath + "/";
7 //創(chuàng)建路徑,如果目錄不存在,則創(chuàng)建
8 File file = new File(path);
9 if(!file.exists()) {
10 file.mkdirs();
11 }
12 //文件路徑+文件名
13 path +=fileUploadFileName.get(i);
14 //1.構(gòu)建輸入流
15 FileInputStream fis = new FileInputStream(f);
16 //2.構(gòu)建輸出流
17 FileOutputStream fos = new FileOutputStream(path);
18 //3.通過字節(jié)寫入輸出流
19 try {
20 byte[] buf = new byte[1024];
21 int len = 0;
22 while ((len = fis.read(buf)) > 0) {
23 fos.write(buf, 0, len);
24 }
25 } catch (Exception e) {
26 e.printStackTrace();
27 } finally {
28 fis.close();
29 fos.close();
30 }
31 i++;
32 }
33 return SUCCESS;
34 }
想要了解更多詳情歡迎來電咨詢18678812288
登陸網(wǎng)址:m.h6244.cn。
聯(lián)系人:王經(jīng)理。