extjs6.5文件上传_ExtJS:文件上传实例

ExtJS:文件上传实例

var ext_dateFormat = 'Y-m-d H:i:s';

var dateFormat = 'yyyy-MM-dd HH:mm:ss';

var date = new Date();

Ext.onReady(function() {

var fifp =Ext.create('Ext.form.Panel', {

renderTo: 'fi-form',

width: 500,

frame: true,

title: '文件上传',

bodyPadding: '10 10 0',

defaults: {

anchor: '100%',

allowBlank: false,

msgTarget: 'side',

labelWidth: 80

},

items: [{

xtype: 'textfield',

fieldLabel: '样品编号',

id:'finfo',

name:'finfo'

},{

xtype: 'container',

layout: 'hbox',

items: [{

xtype: 'textfield',

fieldLabel: '当前经度',

id:'flongitude',

name:'flongitude',

msgTarget: 'side',

allowBlank: false,

labelWidth: 80

}, {

xtype: 'textfield',

fieldLabel: '当前纬度',

id:'flatitude',

name:'flatitude',

msgTarget: 'side',

allowBlank: false,

labelWidth: 80

}]

},{

xtype : 'textfield',

fieldLabel : '上传时间',

id : 'ftime',

name : 'ftime',

// yyyy-MM-dd HH:mm:ss

value : Ext.Date.format(new Date(date.getFullYear(),date.getMonth(),date.getDate(),

date.getHours(),date.getMinutes(),date.getSeconds()), ext_dateFormat),

listeners : {

'focus' : function() {

WdatePicker({

dateFmt : dateFormat

});

}

}

},{

xtype: 'filefield',

id: 'fiupload',

emptyText: '请点击右边按钮选择文件!',

fieldLabel: '选择文件',

name: 'fiupload',

buttonText: '浏览文件',

buttonConfig: {

iconCls: 'upload-icon'

}

}],

buttons: [{

text: '保存文件',

handler: function(){

var fiform = this.up('form').getForm();

if(fiform.isValid()){

fiform.submit({

type : 'ajax',

url: 'files/addData.action',

method : "POST",

waitMsg: ' 正在上传,请稍候...',

success: function(form, action) {

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

},

failure:function(form, action)

{

Ext.Msg.alert("Failure","文件上传失败");

}

});

}

}

},{

text: '重新上传',

handler: function() {

this.up('form').getForm().reset();

}

}]

});

});

后台处理核心类方法:

private static final int BUFFER_SIZE = 16 * 1024;

public String addData() throws Exception {

Timestamp ts = new Timestamp(System.currentTimeMillis());

SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");// 设置日期格式

ts = Timestamp.valueOf(this.ftime);

System.out.println(ts);

String nowtime = df.format(new Date());

System.out.println("uploadFileName = " + this.fiuploadFileName);

System.out.println("uploadContentType = " + this.fiuploadContentType);

System.out.println(nowtime);

// upload -- wapps 下面的文件夹,用来存放图片

String toSrc = ServletActionContext.getServletContext().getRealPath(

"upload")

+ "/" + nowtime + getFileExp(this.fiuploadFileName); // 使用時間戳作為文件名

String toFilename = nowtime + getFileExp(this.fiuploadFileName);

String toSrcPath = "./upload/" + toFilename;

String toinfo = this.finfo;

Double tolongitude = Double.parseDouble(this.flongitude);

Double tolatitude = Double.parseDouble(this.flatitude);

System.out.println("原文件名 : " + this.fiuploadFileName);

System.out.println("文件描述 : " + toinfo);

System.out.println("存放路径: " + toSrcPath);

System.out.println("存放文件名: " + toFilename);

System.out.println("当前经度 : " + tolongitude);

System.out.println("当前维度 : " + tolatitude);

File toFile = new File(toSrc);

writeFile(this.fiupload, toFile);

Files files = new Files(ts, toFilename, toSrcPath, toinfo, tolatitude,

tolongitude);

String result = filesService.addData(files);

System.out.println(result);

success = true;

return SUCCESS;

}

private static void writeFile(File src, File dst) {

System.out.println(" == 文件写入 == ");

try {

InputStream in = null;

OutputStream out = null;

try {

in = new BufferedInputStream(new FileInputStream(src),

BUFFER_SIZE);

out = new BufferedOutputStream(new FileOutputStream(dst),

BUFFER_SIZE);

byte[] buffer = new byte[BUFFER_SIZE];

while (in.read(buffer) > 0) {

out.write(buffer);

}

} finally {

if (null != in) {

in.close();

}

if (null != out) {

out.close();

}

}

} catch (Exception e) {

e.printStackTrace();

}

System.out.println(" == 写入成功! == ");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值