Servlet 3.0規范的HttpServletRequest已經提供了方法來處理文件上傳但這種上傳需要在Servlet中完成。而Struts2則提供了更簡單的封裝。
Struts2默認使用的是Jakarta的Common-FileUpload的文件上傳框架,因此使用Struts2的文件上傳功能,則需要添加兩個jar包,即commons-io-2.2.jar和commons-fileupload-1.3.1.jar。
Struts2簡單文件上傳示例:
1.文件上傳頁面
為了能上傳文件,表單的method必須設置為POST,并且enctype設置為multipart/form-data。一旦設置了enctype為multipart/form-data,此時瀏覽器將會采用二進制流的方式來處理表單數據。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<%@ taglib prefix="s" uri="/struts-tags" %> <%-- Created by IntelliJ IDEA. User: Administrator Date: 2018/1/16 Time: 14:06 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> < html > < head > < title >Struts2 簡單文件上傳</ title > </ head > < body > < s:form action = "file_upload" method = "POST" enctype = "multipart/form-data" > < s:file name = "upload" label = "選擇文件" /> < s:submit value = "上傳" /> </ s:form > </ body > </ html > |
2.處理上傳請求的Action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/** * Description:Struts2簡單文件上傳 * Author: Eleven * Date: 2018/1/24 10:39 */ public class FileAction extends ActionSupport{ //上傳文件 private File upload; //上傳文件類型 private String uploadContentType; //上傳文件名 private String uploadFileName; //文件上傳允許的類型在struts.xml中使用param標簽動態設置了 private String allowTypes; public String page(){ return "page" ; } public void upload() { //文件上傳: //1.讀取文件內容 //2.將文件內容寫到指定文件 try { System.out.println( "文件上傳允許的類型=" +allowTypes); String realPath = ServletActionContext.getServletContext().getRealPath( "/upload" ); System.out.println( "項目的絕對路徑=" +realPath); //創建文件保存目錄 new File(realPath).mkdir(); File file = new File(realPath+ "/" +uploadFileName); //文件不存在則創建 if (!file.exists()){ file.createNewFile(); } FileOutputStream out = new FileOutputStream(file); FileInputStream in = new FileInputStream(upload); byte [] buffer = new byte [ 1024 ]; int len = 0 ; //邊讀邊寫 每次讀取1kb 寫1kb while ((len = in.read(buffer))> 0 ){ out.write(buffer, 0 ,len); } System.out.println( "文件上傳成功..." ); } catch (Exception e){ e.printStackTrace(); } } public File getUpload() { return upload; } public void setUpload(File upload) { this .upload = upload; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this .uploadContentType = uploadContentType; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this .uploadFileName = uploadFileName; } public String getAllowTypes() { return allowTypes; } public void setAllowTypes(String allowTypes) { this .allowTypes = allowTypes; } } |
如果表單中包含一個name屬性為xxx的文件域,則對應的Action中需要使用三個成員變量來封裝該文件域的信息。
類型為File的xxx成員變量封裝了該文件域對應的文件內容。
類型為String的xxxFileName成員變量封裝了該文件域對應的文件的文件名。
類型為String的xxxContentType成員變量封裝了該文件域對應的文件的文件類型。
3.配置struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> < struts > < constant name = "struts.enable.DynamicMethodInvocation" value = "false" /> < constant name = "struts.devMode" value = "true" /> < package name = "default" namespace = "/" extends = "struts-default" > <!--文件上傳--> < action name = "file_*" class = "eleven.action.FileAction" method = "{1}" > < result name = "page" >/WEB-INF/jsp/fileUpload.jsp</ result > <!--動態設置action的屬性,這里舉例設置了允許文件上傳的類型,但是action程序中并未做過多的處理--> < param name = "allowTypes" >image/png,image/gif,image/jpeg</ param > </ action > </ package > </ struts > |
攔截器實現文件過濾
Struts2提供了一個文件上傳的攔截器,fileUpload,為了讓該攔截器起作用,要在action中配置攔截器引用。
配置fileUpload攔截器時,可以為其指定兩個參數:
allowTypes:允許上傳的文件類型,多個文件類型之間用英文逗號,隔開
maximumSize:允許上傳的文件大小,單位是字節。
當文件過濾失敗后,系統自動轉入input邏輯視圖,因此必須為該Action配置名為input的邏輯視圖。除此之外,還必須顯示地為該Action配置defaultStack的攔截器引用。
struts.xml配置文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> < struts > < constant name = "struts.enable.DynamicMethodInvocation" value = "false" /> < constant name = "struts.devMode" value = "true" /> < package name = "default" namespace = "/" extends = "struts-default" > <!--文件上傳--> < action name = "file_*" class = "eleven.action.FileAction" method = "{1}" > <!--配置fileUpload攔截器 且配置在defaultStack攔截器棧之前--> < interceptor-ref name = "fileUpload" > <!--允許上傳的文件類型--> < param name = "allowedTypes" >image/png,image/gif,image/jpeg</ param > <!--允許上傳文件大小--> < param name = "maximumSize" >2000</ param > </ interceptor-ref > <!--配置系統默認攔截器--> < interceptor-ref name = "defaultStack" /> <!--配置input視圖頁面--> < result name = "input" >/WEB-INF/jsp/input.jsp</ result > < result name = "page" >/WEB-INF/jsp/fileUpload.jsp</ result > </ action > </ package > </ struts > |
上面配置的文件上傳的攔截器,要求文件上傳的類型只能是圖片文件,并且文件大小不能大于2000字節,如果上傳文件太大,或者類型不符合,則將跳轉到input邏輯視圖。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/eleven258/archive/2018/01/26/8358159.html