弄了N久,终于成功了。
必须设置 <interceptor-ref name="fileUploadStack"></interceptor-ref>
<action name="uploadfile" class="org.lib.main.action.UploadAction">
<interceptor-ref name="fileUploadStack"></interceptor-ref>
<result type="json">
<param name="contentType">text/html</param>
<param name="includeProperties">success,resultMsg</param>
</result>
</action>
Ext.onReady(function(){
Ext.QuickTips.init();
var uploadForm = Ext.create('Ext.form.Panel',{
title:'Ext.form.field.File示例',
bodyStyle:'padding:5 5 5 5',//表单边距
frame : true,
height:100,
width:300,
fileUpload : true,
renderTo :'form',
defaults:{//统一设置表单字段默认属性
labelSeparator :':',//分隔符
labelWidth : 50,//标签宽度
width : 150,//字段宽度
allowBlank : false,//是否允许为空
labelAlign : 'left',//标签对齐方式
msgTarget :'side' //在字段的右边显示一个提示信息
},
items:[{
xtype: 'filefield',
name: 'upload',
fieldLabel: '照片',
anchor: '100%',
buttonText: '选择照片...'
}],
buttons: [{
text: '上传文件',
handler: function() {
var form = uploadForm.getForm();
if(form.isValid()){
form.submit({
url: 'uploadfile',
waitMsg: '正在上传照片文件请稍候...',
success: function(fp, o) {
Ext.Msg.alert('提示信息', '您的照片文件 "' + o.result.file + '"已经成功上传。');
},
failure : function (fp,o){
Ext.Msg.alert('上传失败!',o.result.resultMsg);
},
});
}
}
}]
});
});
package org.lib.main.action;
//package com.jfok;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private File upload; //上传的文件,在extjs 中对应 xtype:'fileuploadfield', name:'upload'
private String fileName; //上传的文件名
private String uploadContentType; //限制上传的后缀名
private String caption; //form中其他的上传的字段
private Boolean success = true; //json中返回的结果
private String resultMsg = "ok" ; //上传失败后,返回的错误结果
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
System.out.println("上传到tomcat中的临时文件名:" + upload.getPath());
}
public String getUploadFileName() {
return fileName;
}
public void setUploadFileName(String fileName) throws UnsupportedEncodingException {
System.out.println("上传的文件名:" +fileName);
this.fileName = fileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String execute() throws Exception{
try { //此处为最简单的文件拷贝方法
java.io.InputStream is = new FileInputStream(upload);
java.io.OutputStream os = new FileOutputStream("d:/"+fileName);//上传到d盘根目录
byte buffer[] = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) > 0)
os.write(buffer, 0, count);
os.close();
is.close();
} catch (Exception e)
{
resultMsg = "上传文件失败,文件太大或没有选择上传的文件 ";
System.out.println(resultMsg);
success = false;
}
//此处会根据 success 和 resultMsg 返回 json
//{"resultMsg":"ok","success":true}
return SUCCESS;
}
public void setCaption(String caption) {
System.out.println("接收到的caption:" + caption );
this.caption = caption;
}
public String getCaption() {
return caption;
}
public Boolean getSuccess() {
return success;
}
public String getResultMsg() {
return resultMsg;
}
}