jspsmart 上傳附件到服務(wù)器
index.jsp
1
<%@ page contentType="text/html;charset=gb2312"%>
2
<html>
3
<head>
4
<title>測試上傳附件</title>
5
<metahttp-equiv ="Content-Type"content="text/html;charset=GB2312">
6
</head>
7
<body>
8
<h2>
9
測試上傳附件
10
</h2>
11
<form name="Form1" enctype="multipart/form-data" method="post" action="Jspsmart.jsp">
12
<p>
13
上傳文件1:
14
<input type="file" name="File1" size="20" maxlength="20">
15
</p>
16
<p>
17
上傳文件2:
18
<input type="file" name="File2" size="20" maxlength="20">
19
</p>
20
<input type="submit" value="上傳">
21
<input type="reset" value="清除">
22
</form>
23
</body>
24
</html>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

Jspsmart.jsp
1
<%@ page import="com.jspsmart.upload.*"%>
2
<%@ page contentType="text/html;charset=GB2312"%>
3
4
<html>
5
<head>
6
<title>CH9 - Jspsmart2.jsp</title>
7
</head>
8
<body>
9
10
<h2>
11
文件上傳范例 - jspSmart
12
</h2>
13
14
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
15
<%
16
//計算文件上傳個數(shù)
17
int count = 0;
18
try {
19
//SmartUpload的初始化,使用這個jspsmart一定要在一開始就這樣聲明
20
mySmartUpload.initialize(pageContext);
21
22
//限制每個上傳附件的最大長度。
23
mySmartUpload.setMaxFileSize(5000000);
24
25
//限制總上傳數(shù)據(jù)的長度。
26
mySmartUpload.setTotalMaxFileSize(10000000);
27
28
//設(shè)定允許上傳的附件(通過擴展名限制)。
29
mySmartUpload.setAllowedFilesList("jpg,gif,GIF,JPG");
30
31
//依據(jù)form的內(nèi)容上傳
32
mySmartUpload.upload();
33
34
//將上傳的文件一個一個取出來處理
35
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
36
//取出一個文件
37
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
38
39
//如果文件存在,則做存檔操作
40
if (!myFile.isMissing()) {
41
42
//將文件存放于絕對路徑的位置
43
myFile.saveAs("C:\\upload\\" + myFile.getFileName(),mySmartUpload.SAVE_PHYSICAL);
44
45
//顯示此上傳文件的詳細信息
46
out.println("FieldName = " + myFile.getFieldName() + "<BR>");
47
out.println("Size = " + myFile.getSize() + "<BR>");
48
out.println("FileName = " + myFile.getFileName() + "<BR>");
49
out.println("FileExt = " + myFile.getFileExt() + "<BR>");
50
out.println("FilePathName = " + myFile.getFilePathName() + "<BR>");
51
out.println("ContentType = " + myFile.getContentType() + "<BR>");
52
out.println("ContentDisp = " + myFile.getContentDisp() + "<BR>");
53
out.println("TypeMIME = " + myFile.getTypeMIME() + "<BR>");
54
out.println("SubTypeMIME = " + myFile.getSubTypeMIME() + "<BR>");
55
count++;
56
}
57
}
58
59
// 顯示應(yīng)該上傳的文件數(shù)目
60
out.println("<BR>" + mySmartUpload.getFiles().getCount() + " files could be uploaded.<BR>");
61
62
// 顯示成功上傳的文件數(shù)目
63
out.println(count + "file(s) uploaded.");
64
} catch (SmartUploadException e) {
65
System.out.println("上傳文件出錯");
66
e.getMessage();
67
}
68
%>
69
</body>
70
</html>

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

posted on 2008-01-19 18:45 石業(yè)海 閱讀(1679) 評論(1) 編輯 收藏 所屬分類: 常用組件