運行上傳組件例子Upload Component
創建3個文件
UploadPage.java
1
import java.io.File;
2
import java.io.FileOutputStream;
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.text.Format;
6
import java.text.NumberFormat;
7
8
import org.apache.tapestry.IRequestCycle;
9
import org.apache.tapestry.html.BasePage;
10
import org.apache.tapestry.request.IUploadFile;
11
12
public class UploadPage extends BasePage
{
13
public static final Format SIZE_FORMAT = NumberFormat.getNumberInstance();
14
15
private IUploadFile file;
16
17
private File serverFile;
18
19
public IUploadFile getFile()
{
20
return file;
21
}
22
23
public void setFile(IUploadFile value)
{
24
file = value;
25
}
26
27
public String getFilename()
{
28
if (file != null)
{
29
return file.getFileName();
30
} else
{
31
return "";
32
}
33
}
34
35
public String getClientPath()
{
36
if (file != null)
{
37
return file.getFilePath();
38
} else
{
39
return "";
40
}
41
}
42
43
public String getServerPath()
{
44
if (serverFile != null)
{
45
return serverFile.getAbsolutePath();
46
} else
{
47
return "";
48
}
49
}
50
51
public long getFileSize()
{
52
if (serverFile != null)
{
53
return serverFile.length();
54
} else
{
55
return 0;
56
}
57
}
58
59
public boolean isFileTruncated()
{
60
if (file != null)
{
61
return true;
62
//修改過
63
} else
{
64
return false;
65
}
66
}
67
68
public void formSubmit(IRequestCycle cycle)
{
69
InputStream fis = file.getStream();
70
FileOutputStream fos = null;
71
72
try
{
73
fos = new FileOutputStream(new File(file.getFileName()));
74
byte[] buffer = new byte[1024];
75
while (true)
{
76
int length = fis.read(buffer);
77
if (length < 0)
{
78
break;
79
}
80
fos.write(buffer, 0, length);
81
}
82
fis.close();
83
fos.close();
84
serverFile = new File(file.getFileName());
85
86
} catch (IOException ioe)
{
87
ioe.printStackTrace();
88
} finally
{
89
if (fis != null)
{
90
try
{
91
fis.close();
92
} catch (IOException ioe)
{
93
}
94
}
95
if (fos != null)
{
96
try
{
97
fos.close();
98
} catch (IOException ioe)
{
99
}
100
}
101
}
102
}
103
104
/** *//****
105
public static void main(String args[]) {
106
107
}
108
**/
109
protected void initialize()
{
110
file = null;
111
serverFile = null;
112
}
113
}
114

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

85

86



87

88



89



90



91

92



93

94

95



96



97

98



99

100

101

102

103

104


105

106

107

108

109



110

111

112

113

114

UploadPage.page
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE page-specification PUBLIC
3
"-//Apache Software Foundation//Tapestry Specification 3.0//EN"
4
"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
5
<!-- generated by Spindle, http://spindle.sourceforge.net -->
6
7
<page-specification class="UploadPage">
8
9
<description>add a description</description>
10
11
12
13
</page-specification>

2

3

4

5

6

7

8

9

10

11

12

13

UploadPage.html
1
<html>
2
<head>
3
</head>
4
<body>
5
<form jwcid="@Form" listener="ognl:listeners.formSubmit">
6
<table bgcolor="#c0c0c0" cellpadding="4">
7
<tr>
8
<td colspan="2">File: <input jwcid="@Upload" file="ognl:file" type= "file"></input></td>
9
</tr>
10
<tr>
11
<td colspan="2"><input type= "submit"value="Upload"></input></td>
12
</tr>
13
<tr>
14
<td colspan="2"><hr></td>
15
</tr>
16
<tr>
17
<td>Filename:</td><td><span jwcid="@Insert" value="ognl:filename"/></td>
18
</tr>
19
<tr>
20
<td>Client path:</td><td><span jwcid="@Insert" value="ognl:clientPath"/></td>
21
</tr>
22
<tr>
23
<td>Server Path:</td><td><span jwcid="@Insert" value="ognl:serverPath"/></td>
24
</tr>
25
<tr>
26
<td>File Truncated:</td><td><span jwcid="@Insert" value="ognl:fileTruncated"/></td>
27
</tr>
28
<tr>
29
<td>File Size:</td><td><span jwcid="@Insert" value="ognl:fileSize" format="ognl:@UploadPage@SIZE_FORMAT"/> bytes</td>
30
</tr>
31
</table>
32
</form>
33
</body>
34
</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

posted on 2005-11-09 10:48 bluesky 閱讀(617) 評論(0) 編輯 收藏 所屬分類: 框架應用