ext+struts2上传文件

Ext2.1 + Struts2 实现简单的文件上传

upload.html
<html>
<head>
<title>上传文件</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>上传文件</h1>
</body>
</html>

<html>

<head>

<title>上传文件</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>上传文件</h1>

</body>

</html>

[b]upload.js[/b]

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: '文件名',
26.
27. name: 'upload',
28.
29. inputType: 'file'//文件类型
30.
31. }],
32.
33. buttons: [{
34.
35. text: '上传',
36.
37. handler: function() {
38.
39. form.getForm().submit({
40.
41. success: function(form, action){
42.
43. Ext.Msg.alert('信息', '文件上传成功!');
44.
45. },
46.
47. failure: function(){
48.
49. Ext.Msg.alert('错误', '文件上传失败');
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: '文件名',

name: 'upload',

inputType: 'file'//文件类型

}],

buttons: [{

text: '上传',

handler: function() {

form.getForm().submit({

success: function(form, action){

Ext.Msg.alert('信息', '文件上传成功!');

},

failure: function(){

Ext.Msg.alert('错误', '文件上传失败');

}

});

}

}]

});


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. /**为上传文件自动分配文件名称,避免重复
32.
33. * @param fileName
34.
35. * @return
36.
37. */
38.
39. private String generateFileName(String fileName) {
40.
41. // 获得当前时间
42.
43. DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
44.
45. // 转换为字符串
46.
47. String formatDate = format.format(Calendar.getInstance().getTime());
48.
49. // 随机生成文件编号
50.
51. int random = new Random().nextInt(10000);
52.
53. // 获得文件后缀名称
54.
55. int position = fileName.lastIndexOf(".");
56.
57. String extension = fileName.substring(position);
58.
59. // 组成一个新的文件名称
60.
61. return formatDate + random + extension;
62.
63. }
64.
65.
66.
67. public String upload() throws IOException {
68.
69. // 获得upload路径的实际目录
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. //获得实际目录
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. // 生成保存文件的文件名称
90.
91. tname[i] = generateFileName(uploadFileName[i]);
92.
93. // 保存文件的路径
94.
95. mydir[i] = targetDirectory + "//" + tname[i];
96.
97.
98.
99. // 建立一个目标文件
100.
101. File target = new File(targetDirectory, tname[i]);
102.
103. // 将临时文件复制到目标文件
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.length + "张照片成功");
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.targetFileName = 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;



/**为上传文件自动分配文件名称,避免重复

* @param fileName

* @return

*/

private String generateFileName(String fileName) {

// 获得当前时间

DateFormat format = new SimpleDateFormat("yyMMddHHmmss");

// 转换为字符串

String formatDate = format.format(Calendar.getInstance().getTime());

// 随机生成文件编号

int random = new Random().nextInt(10000);

// 获得文件后缀名称

int position = fileName.lastIndexOf(".");

String extension = fileName.substring(position);

// 组成一个新的文件名称

return formatDate + random + extension;

}



public String upload() throws IOException {

// 获得upload路径的实际目录

@SuppressWarnings("unused")

Member member = (Member)getSessionMap().get("member");

String realPath = "";//ServletActionContext.getServletContext().getRealPath("/image/" + member.getEmail() + "/" + (String)getSessionMap().get("selectAlbum"));

//获得实际目录

String targetDirectory = "C:\\";

String[] mydir = new String[upload.length];

String[] tname = new String[upload.length];



for (int i = 0; i < upload.length; i++) {

// 生成保存文件的文件名称

tname[i] = generateFileName(uploadFileName[i]);

// 保存文件的路径

mydir[i] = targetDirectory + "//" + tname[i];


// 建立一个目标文件

File target = new File(targetDirectory, tname[i]);

// 将临时文件复制到目标文件

try {

FileUtils.copyFile(upload[i], target);

} catch (IOException e) {

this.setMessage(e.getMessage());

e.printStackTrace();

}



}



this.setMessage("上传" + upload.length + "张照片成功");

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.targetFileName = targetFileName;

}


}


配置文件

<package name="com.hm.member.action" namespace="/member" extends="struts-default">

<action name="upload" class="photoAction" method="upload">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值