struts標簽庫中有相應html:file標簽,enctype這個屬性貌似去掉也可以,不知道有什么用。
一點積累:
1. Action類代碼中要獲得當前地址可以使用
getServlet().getServletContext().getRealPath(String)
2. Forward的目標地址前面貌似都要加"/"。 Path success.jsp does not start with a "/" character
1
<html:form action="/upload" enctype="multipart/form-data">
2
theFile :
3
<html:file property="theFile" />
4
<html:errors property="theFile"/><br/>
5
<html:submit/>
6
</html:form>
網上看到的例子是在Action類中處理的,不知道這么做合不合理,這個是簡單的處理代碼,沒有驗證文件大小和同名文件是否存在,注意先要在相應目錄中建立file目錄。
2

3

4

5

6

1
UploadForm uploadForm = (UploadForm) form;// TODO Auto-generated method stub
2
FormFile file = uploadForm.getTheFile();
3
try {
4
InputStream stream = file.getInputStream();
5
String filePath = getServlet().getServletContext().getRealPath("/");
6
OutputStream fileout = new FileOutputStream(filePath + "/file/" + file.getFileName());
7
int bytesRead = 0;
8
byte[] buffer = new byte[8192];
9
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
10
fileout.write(buffer, 0, bytesRead);
11
}
12
fileout.close();
13
stream.close();
14
} catch (Exception e) {
15
System.err.println(e);
16
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

一點積累:
1. Action類代碼中要獲得當前地址可以使用
getServlet().getServletContext().getRealPath(String)
2. Forward的目標地址前面貌似都要加"/"。 Path success.jsp does not start with a "/" character