您的位置:首页 > 编程语言 > Java开发

SpringMvc+ext4文件上传

2016-02-18 10:32 435 查看
ext jsp页面代码:

//上传图片类型

var img_reg = /\.([jJ][pP][gG]){1}$|\.([jJ][pP][eE][gG]){1}$|\.([gG][iI][fF]){1}$|\.([pP][nN][gG]){1}$|\.([bB][mM][pP]){1}$/;

var AddForm = Ext.create("Ext.form.Panel", {

border: false,

fieldDefaults: {

msgTarget: 'side',

labelWidth: 105,

align: "right",

regexText: '格式错误',

allowBlank: false

},

defaults: {

padding: 15,

width: 380

},

defaultType: "textfield",

items: [

{

xtype: "hidden",

id: 'id',

name: 'id'

},

{

fieldLabel: '节点名称',

id: 'text',

name: 'text',

allowBlank: false

},{

fieldLabel: '类别',

xtype: 'radiogroup',

margin:'10 0 0 15',

items: [

{boxLabel: '表格报表', name: 'type', inputValue: 1},

{boxLabel: '图形报表', name: 'type', inputValue: 2},

{boxLabel: '其它', name: 'type', inputValue: 3}

],

listeners:{

change: function(g , newValue , oldValue){

if(newValue.type==1){

AddForm.form.findField("url").setValue("/tableDisplay/list.do");

}else if(newValue.type==2){

AddForm.form.findField("url").setValue("/chartDisplay/list.do");

} else if(newValue.type==3){

AddForm.form.findField("url").setValue("");

}

}

}

},{

fieldLabel: '节点地址',

id: 'url',

name: 'url',

allowBlank: true

} ,

{

xtype: "checkbox",

fieldLabel: '末级节点',

inputValue:true,

id: 'leaf',

name: 'leaf'

},

{

fieldLabel: '排序序号',

id: 'xuHao',

name: 'xuHao'

},

{

xtype : 'textfield',

id : 'photoUpload',

name : 'uploadfile',

inputType : "file",

//labelWidth : 60,

fieldLabel : '图片上传',

allowBlank: true,

regex : img_reg,//图片验证正则表达式

listeners : {

}
}

]

});

var AddDialog = Ext.create("Ext.window.Window", {

iconCls: 'a_add',

width:450,

height:340,

closeAction: 'close',

resizable: false,

autoScroll: true,

closable: true,

modal: true,

items: AddForm,

buttons: [{

text: '添加',

id: "btnAdd",

handler: function () {

if (AddForm.form.isValid()) {

AddForm.form.submit({

method: "post",

url : '<%=request.getContextPath()%>/node/photoUpload.do',

//waitMsg : '正在保存图片...',

success : function(form,action) {

var filePath = this.result.filePath;

if (AddDialog.title == '添加同级') {

//O为同级,1为下级

AddNode(0,filePath);

}else {

AddNode(1,filePath);

}

}
});

}

}

},

{

text: '修改',

id: "btnEdit",

handler: EditNode

},

{

id: "btnCancel",

text: '关闭',

handler: function () {

AddDialog.close();

}

}]

});

java 代码:

@ResponseBody

@RequestMapping(value = "/photoUpload.do",method=RequestMethod.POST)

public String photoUpload(@RequestParam("uploadfile") CommonsMultipartFile mFile

,HttpServletResponse response,HttpServletRequest request) throws IOException {

String filename = mFile.getFileItem().getName();

try {

String filePath = request.getSession().getServletContext().getRealPath("images");

File f = new File(filePath);

if (!f.exists()) {

f.mkdir();

}

File targetFile = new File(filePath + "\\" + filename);

mFile.getFileItem().write(targetFile);

} catch (Exception e) {

e.printStackTrace();

}

return "{success:true,filePath:'"+filename+"'}";

}

applicationContext xml代码:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

">

<!-- 文件上传 -->

<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!-- 以字节为单位的最大上传文件的大小 -->

<property name="maxUploadSize" value="3145728" />

<property name="defaultEncoding" value="UTF-8"></property>

</bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: