Upload.html
<HTML>
<Head>
<Title> upload a file </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<LINK rel = "stylesheet" type = "text/CSS" href = "/ext-2.1/resources/CSS/ext-all.css"/>
<SCRIPT type = "text/JavaScript" src = "/ext-2.1/adapter/EXT/ext-base.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "/ext-2.1/ext-all.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "/newext/upload. js"> </SCRIPT>
</Head>
<Body>
<H1> upload a file </Body>
</Html>
<HTML>
<Head>
<Title> upload a file </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<LINK rel = "stylesheet" type = "text/CSS" href = "/ext-2.1/resources/CSS/ext-all.css"/>
<SCRIPT type = "text/JavaScript" src = "/ext-2.1/adapter/EXT/ext-base.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "/ext-2.1/ext-all.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "/newext/upload. js"> </SCRIPT>
</Head>
<Body>
<H1> upload a file
</Body>
</Html>
Upload. js
1. Ext. onready (function (){
2.
3. var form = new Ext. Form. formpanel ({
4.
5. fileupload: True,
6.
7. basecls: 'x-plain ',
8.
9. Layout: 'absolute ',
10.
11. url: 'save-form. php ',
12.
13. defaulttype: 'textfield ',
14.
15. Frame: True,
16.
17. url: '/member/upload. action ',//
18.
19.
20.
21. Items :[{
22.
23. xtype: 'textfield ',
24.
25. fieldlabel: 'filename ',
26.
27. Name: 'upload ',
28.
29. inputtype: 'file' // file type
30.
31.}],
32.
33. Buttons :[{
34.
35. Text: 'upload ',
36.
37. Handler: function (){
38.
39. Form. getform (). Submit ({
40.
41. Success: function (Form, Action ){
42.
43. Ext. msg. Alert ('info', 'File Uploaded successfully! ');
44.
45 .},
46.
47. Failure: function (){
48.
49. Ext. msg. Alert ('error', 'file upload failed ');
50.
51 .}
52.
53 .});
54.
55 .}
56.
57.}]
58.
59 .});
60.
61.
62.
63. var window = new Ext. Window ({
64.
65. Title: 'resize me ',
66.
67. Width: 500,
68.
69. Height: 300,
70.
71. minwidth: 300,
72.
73. minheight: 200,
74.
75. Layout: 'fit ',
76.
77. Plain: True,
78.
79. bodystyle: 'padding: 5px ;',
80.
81. buttonalign: 'center ',
82.
83. Items: Form
84.
85 .});
86.
87.
88.
89. Window. Show ();
90.
91 .});
Ext. onready (function (){
VaR form = new Ext. Form. formpanel ({
Fileupload: True,
Basecls: 'x-plain ',
Layout: 'absolute ',
URL: 'save-form. php ',
Defaulttype: 'textfield ',
Frame: True,
URL: '/member/upload. action ',//
Items :[{
Xtype: 'textfield ',
Fieldlabel: 'filename ',
Name: 'upload ',
Inputtype: 'file' // file type
}],
Buttons :[{
Text: 'upload ',
Handler: function (){
Form. getform (). Submit ({
Success: function (Form, Action ){
Ext. msg. Alert ('info', 'file uploaded successfully! ');
},
Failure: function (){
Ext. msg. Alert ('error', 'file upload failed ');
}
});
}
}]
});
VaR window = new Ext. Window ({
Title: 'resize me ',
Width: 500,
Height: 300,
Minwidth: 300,
Minheight: 200,
Layout: 'fit ',
Plain: True,
Bodystyle: 'padding: 5px ;',
Buttonalign: 'center ',
Items: Form
});
Window. Show ();
});
Photoaction. Java
View plaincopy to clipboardprint?
1. Public class photoaction extends sessionawareaction {
2.
3.
4.
5. Private Static final long serialversionuid =-3991411826094455715l;
6.
7.
8.
9.
10.
11. Private file [] upload;
12.
13.
14.
15. Private string [] uploadfilename;
16.
17.
18.
19. Private string [] uploadcontenttype;
20.
21.
22.
23. Private string [] dir;
24.
25.
26.
27. Private string [] targetfilename;
28.
29.
30.
31./** automatically allocate the file name for the uploaded file to avoid duplication
32.
33. * @ Param filename
34.
35. * @ return
36.
37 .*/
38.
39. Private string generatefilename (string filename ){
40.
41. // obtain the current time
42.
43. dateformat format = new simpledateformat ("yymmddhhmmss ");
44.
45. // convert to a string
46.
47. String formatdate = format. Format (calendar. getinstance (). gettime ());
48.
49. // randomly generate the file number
50.
51. Int random = new random (). nextint (10000 );
52.
53. // get the file suffix name
54.
55. Int position = filename. lastindexof (".");
56.
57. string extension = filename. substring (position );
58.
59. // form a new file name
60.
61. Return formatdate + random + extension;
62.
63 .}
64.
65.
66.
67. Public String upload () throws ioexception {
68.
69. // obtain the actual directory of the upload path
70.
71. @ suppresswarnings ("UNUSED ")
72.
73. Member member = (member) getsessionmap (). Get ("member ");
74.
75. string realpath = ""; // servletactioncontext. getservletcontext (). getrealpath ("/image/" + member. getemail () + "/" + (string) getsessionmap (). get ("selectalbum "));
76.
77. // obtain the actual directory
78.
79. String targetdirectory = "C ://";
80.
81. String [] mydir = new string [upload. Length];
82.
83. String [] tname = new string [upload. Length];
84.
85.
86.
87. For (INT I = 0; I <upload. length; I ++ ){
88.
89. // generate the name of the file to save
90.
91. tname [I] = generatefilename (uploadfilename [I]);
92.
93. // path to save the file
94.
95. mydir [I] = targetdirectory + "//" + tname [I];
96.
97.
98.
99. // create a target file
100.
101. File target = new file (targetdirectory, tname [I]);
102.
103. // copy the temporary file to the target file
104.
105. Try {
106.
107. fileutils. copyfile (upload [I], target );
108.
109.} catch (ioexception e ){
110.
111. This. setmessage (E. getmessage ());
112.
113. E. printstacktrace ();
114.
115 .}
116.
117.
118.
119 .}
120.
121.
122.
123. This. setmessage ("Upload" + upload. Length + "Photo succeeded ");
124.
125. Return success;
126.
127 .}
128.
129.
130.
131. Public file [] getupload (){
132.
133. Return upload;
134.
135 .}
136.
137.
138.
139. Public void setupload (file [] upload ){
140.
141. This. Upload = upload;
142.
143 .}
144.
145.
146.
147. Public String [] getuploadfilename (){
148.
149. Return uploadfilename;
150.
151 .}
152.
153.
154.
155. Public void setuploadfilename (string [] uploadfilename ){
156.
157. This. uploadfilename = uploadfilename;
158.
159 .}
160.
161.
162.
163. Public String [] getuploadcontenttype (){
164.
165. Return uploadcontenttype;
166.
167 .}
168.
169.
170.
171. Public void setuploadcontenttype (string [] uploadcontenttype ){
172.
173. This. uploadcontenttype = uploadcontenttype;
174.
175 .}
176.
177.
178.
179. Public String [] getdir (){
180.
181. Return dir;
182.
183 .}
184.
185.
186.
187. Public void setdir (string [] DIR ){
188.
189. This. dir = dir;
190.
191 .}
192.
193.
194.
195. Public String [] gettargetfilename (){
196.
197. Return targetfilename;
198.
199 .}
200.
201.
202.
203. Public void settargetfilename (string [] targetfilename ){
204.
205. this.tar getfilename = targetfilename;
206.
207 .}
208.
209.
210.
211 .}
Public class photoaction extends sessionawareaction {
Private Static final long serialversionuid =-3991411826094455715l;
Private file [] upload;
Private string [] uploadfilename;
Private string [] uploadcontenttype;
Private string [] dir;
Private string [] targetfilename;
/** Automatically allocate file names for uploaded files to avoid duplication
* @ Param filename
* @ Return
*/
Private string generatefilename (string filename ){
// Obtain the current time
Dateformat format = new simpledateformat ("yymmddhhmmss ");
// Convert to a string
String formatdate = format. Format (calendar. getinstance (). gettime ());
// Randomly generate the file number
Int random = new random (). nextint (10000 );
// Obtain the file suffix name
Int position = filename. lastindexof (".");
String extension = filename. substring (position );
// Create a new file name
Return formatdate + random + extension;
}
Public String upload () throws ioexception {
// Obtain the actual directory of the upload path
@ Suppresswarnings ("UNUSED ")
Member member = (member) getsessionmap (). Get ("member ");
String realpath = ""; // servletactioncontext. getservletcontext (). getrealpath ("/image/" + member. getemail () + "/" + (string) getsessionmap (). get ("selectalbum "));
// Obtain the actual directory
String targetdirectory = "C ://";
String [] mydir = new string [upload. Length];
String [] tname = new string [upload. Length];
For (INT I = 0; I <upload. length; I ++ ){
// Generate the name of the file to save
Tname [I] = generatefilename (uploadfilename [I]);
// Save the file path
Mydir [I] = targetdirectory + "//" + tname [I];
// Create a target file
File target = new file (targetdirectory, tname [I]);
// Copy the temporary file to the target file
Try {
Fileutils. copyfile (upload [I], target );
} Catch (ioexception e ){
This. setmessage (E. getmessage ());
E. printstacktrace ();
}
}
This. setmessage ("Upload" + upload. Length + "Photo succeeded ");
Return success;
}
Public file [] getupload (){
Return upload;
}
Public void setupload (file [] upload ){
This. Upload = upload;
}
Public String [] getuploadfilename (){
Return uploadfilename;
}
Public void setuploadfilename (string [] uploadfilename ){
This. uploadfilename = uploadfilename;
}
Public String [] getuploadcontenttype (){
Return uploadcontenttype;
}
Public void setuploadcontenttype (string [] uploadcontenttype ){
This. uploadcontenttype = uploadcontenttype;
}
Public String [] getdir (){
Return dir;
}
Public void setdir (string [] DIR ){
This. dir = dir;
}
Public String [] gettargetfilename (){
Return targetfilename;
}
Public void settargetfilename (string [] targetfilename ){
This.tar getfilename = targetfilename;
}
}
Configuration File
<Package name = "com. Hm. member. Action" namespace = "/member" extends = "struts-Default">
<Action name = "Upload" class = "photoaction" method = "Upload">