通過struts2提供的fileUpload攔截器可以方便的實現文件的上傳。那么我們來簡單的實現以下:
首先,我們新建一個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中的uploads/值賦給savePath屬性
10 * 的作用就是為Action中的某些屬性賦一個默認值,通常這樣做的如配置路徑、文件名之類的.... 傳遞參數
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 //創建路徑,如果目錄不存在,則創建
25 File file = new File(path);
26 if(!file.exists()) {
27 file.mkdirs();
28 }
29 //文件路徑+文件名
30 path +=this.getFileUploadFileName();
31 //1.構建輸入流
32 FileInputStream fis = new FileInputStream(fileUpload);
33 //2.構建輸出流
34 FileOutputStream fos = new FileOutputStream(path);
35 //3.通過字節寫入輸出流
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 來命名。否則一定無法接受到參數,不能獲取文件名。同時fileUploadContentType也是同樣的道理。savePath這個屬性代表我們上傳之后的存放地址的目錄名。這個參數需要到struts.xml中進行配置,且需要命名一致。
當我們的控制層完成了之后就可以寫我們的struts.xml配置文件。當然我們需要用到fileUpload這個攔截器:
復制代碼
1
2
3
4
5
6
7
8
9 image/bmp,image/png,image/gif,image/jpeg,image/jpg
10
11 1024*1024
12
13
14 uploads
15
16
17
18
19
20
21
復制代碼
我們必須也要加上struts默認的defaultStack這個攔截器,且要放在fileUpload攔截器的后面。這里的savePath參數配置的就是目錄名,和action中的屬性對應,這樣action中 就可以取到我們配置的文件名。
最后來到了我們的展示層:
1
2
3
4
必須提供method 和 enctype這兩個屬性,且不能改變,這樣才能上傳文件格式。這樣我們就基本實現了文件的上傳功能。如果我們需要上傳多個文件,只需要修改如下屬性:
1 private List
2 private List
3 private List
然后在方法中進行循環遍歷,同時form表單的file元素的name屬性一定要全部相同,這樣struts2就會自動為我們進行處理。如下:
1
2
3
4
5
循環遍歷文件對象和文件名即可。
復制代碼
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 //創建路徑,如果目錄不存在,則創建
8 File file = new File(path);
9 if(!file.exists()) {
10 file.mkdirs();
11 }
12 //文件路徑+文件名
13 path +=fileUploadFileName.get(i);
14 //1.構建輸入流
15 FileInputStream fis = new FileInputStream(f);
16 //2.構建輸出流
17 FileOutputStream fos = new FileOutputStream(path);
18 //3.通過字節寫入輸出流
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 }