Extjs+struts2 实现文件上传

弄了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;
	}
}


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值