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>
//上传图片类型
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>
相关文章推荐
- eclipse中新建maven项目
- eclipse下查看maven下载的源码中文乱码问题
- 索引查找Java实现
- Java正则表达式基础知识
- java jvm 参数 -Xms -Xmx -Xmn -Xss 调优总结
- #java#接口
- JavaBean规范
- Struts2 指定页面返回
- Android在Eclipse上进行开发时分包
- java虚拟机加载系统环境变量到内存中
- Windows下使用Hadoop2.6.0-eclipse-plugin插件
- windows下eclipse调试hadoop详解
- Spring Boot 快速入门
- Eclipse中修改SVN用户名和密码方法
- svn—Eclipse中如何显示svn 信息
- SpringMVC 的几种返回数据的方式
- Spring事务(一) Markdown 版
- java scanner用法
- 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
- 个人学习-java-包装类