SpringMvc+ext4文件上传

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>
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值