springMVC实现上传文件功能
2012-07-24 15:49
645 查看
1.spring_fileupload.xml配置文件如下:
Xml代码
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--<property name="maxUploadSize" value="10485760"></property>-->
</bean>
2.FileUploadAction控制器如下:
Java代码
@RequestMapping(params = "method=up", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> uploadSolver(@RequestParam MultipartHttpServletRequest request {
Map<String,String> info = new HashMap<String,String>();
MultipartFile patch = request.getFile("file");
if (!patch.isEmpty()) {
try {
String fileName = patch.getOriginalFilename();
/**
* 获取文件后缀
*/
System.out.println(fileName);
String suffix = fileName.substring(fileName.lastIndexOf("."));
/**
* 判断上传的文件格式是否正确
*/
if ((".zip.rar.gz.tar.bz2.txt".indexOf(suffix.toLowerCase()) != -1)) {
Integer fileSize = (int) patch.getSize() / 1024;
/**
* 如果文件小于10M,则上传文件,否则提示用户不能超过10M
*/
if (fileSize <= 10240) {
String uploadPath = ClassLoaderUtil.getProperties("uploadFile.properties").getProperty("filePath");
System.out.println(uploadPath);
File filePath = new File(request.getSession()
.getServletContext().getRealPath(uploadPath));
System.out.println(filePath.getAbsolutePath());
/**
* 判读存储文件路是否存在,不存在则创建
*/
if (! filePath.exists()) {
filePath.mkdirs();
System.out.println("上传文件路径不存在,创建成功!");
}
/**
* 文件开始上传到服务器上
*/
patch.transferTo(new File(filePath.getAbsolutePath()+"\\"+fileName));
info.put("success", "true");
info.put("msg", "上传成功!");
} else {
System.out.println("上传的文件太大,文件大小不能超过10M");
info.put("success","false");
info.put("msg", "上传的文件太大,文件大小不能超过10M");
}
} else {
System.out.println("上传的文件格式不支持");
info.put("success","false");
info.put("msg", "上传的文件格式不支持");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("系统异常");
info.put("success","false");
info.put("msg", "系统异常");
}
}
return info;
}
3.前端表单使用Extjs 如下:
Js代码
Ext.onReady(function (){
var addFileTextField = new Ext.form.TextField({
name:'file',
allowBlank:false,
//使用HTML中的filetext
inputType:'file'
});
var addFileFormPanel = new Ext.form.FormPanel({
autoDestory:true,
fileUpload:true,
frame:true,
width:300,
autoHeight:true,
labelAlign:'right',
labelWidth:60,
defaultType:'textfield',
defaults:{width:200,allowBlank:false},
items: [addFileTextField]
});
var addFileWindow = new Ext.Window({
id : "addFileWindow",
title : "上传文件",
width : 640,
height : 200,
resizable : false,
modal : true,
maximizable : false,
closeAction : "hide",
constrain : true,
layout : "vbox",
animateTarget:'target',
layoutConfig:{
align: "stretch"
},
items : [addFileFormPanel],
buttons:[
{text:'上传',handler:function (){
if(! addFileFormPanel.getForm().isValid()) {
return false;
}
//上传
addFileFormPanel.getForm().submit({
url:'uploadFile.do?method=up',
waitMsg: '正在上传...',
success: function (form,response){
Ext.Msg.alert('success',response.result.msg);
},
failure: function (form,response){
Ext.Msg.alert('error',response.result.msg);
}
});
}
}
]
});
});
Xml代码
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--<property name="maxUploadSize" value="10485760"></property>-->
</bean>
2.FileUploadAction控制器如下:
Java代码
@RequestMapping(params = "method=up", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> uploadSolver(@RequestParam MultipartHttpServletRequest request {
Map<String,String> info = new HashMap<String,String>();
MultipartFile patch = request.getFile("file");
if (!patch.isEmpty()) {
try {
String fileName = patch.getOriginalFilename();
/**
* 获取文件后缀
*/
System.out.println(fileName);
String suffix = fileName.substring(fileName.lastIndexOf("."));
/**
* 判断上传的文件格式是否正确
*/
if ((".zip.rar.gz.tar.bz2.txt".indexOf(suffix.toLowerCase()) != -1)) {
Integer fileSize = (int) patch.getSize() / 1024;
/**
* 如果文件小于10M,则上传文件,否则提示用户不能超过10M
*/
if (fileSize <= 10240) {
String uploadPath = ClassLoaderUtil.getProperties("uploadFile.properties").getProperty("filePath");
System.out.println(uploadPath);
File filePath = new File(request.getSession()
.getServletContext().getRealPath(uploadPath));
System.out.println(filePath.getAbsolutePath());
/**
* 判读存储文件路是否存在,不存在则创建
*/
if (! filePath.exists()) {
filePath.mkdirs();
System.out.println("上传文件路径不存在,创建成功!");
}
/**
* 文件开始上传到服务器上
*/
patch.transferTo(new File(filePath.getAbsolutePath()+"\\"+fileName));
info.put("success", "true");
info.put("msg", "上传成功!");
} else {
System.out.println("上传的文件太大,文件大小不能超过10M");
info.put("success","false");
info.put("msg", "上传的文件太大,文件大小不能超过10M");
}
} else {
System.out.println("上传的文件格式不支持");
info.put("success","false");
info.put("msg", "上传的文件格式不支持");
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("系统异常");
info.put("success","false");
info.put("msg", "系统异常");
}
}
return info;
}
3.前端表单使用Extjs 如下:
Js代码
Ext.onReady(function (){
var addFileTextField = new Ext.form.TextField({
name:'file',
allowBlank:false,
//使用HTML中的filetext
inputType:'file'
});
var addFileFormPanel = new Ext.form.FormPanel({
autoDestory:true,
fileUpload:true,
frame:true,
width:300,
autoHeight:true,
labelAlign:'right',
labelWidth:60,
defaultType:'textfield',
defaults:{width:200,allowBlank:false},
items: [addFileTextField]
});
var addFileWindow = new Ext.Window({
id : "addFileWindow",
title : "上传文件",
width : 640,
height : 200,
resizable : false,
modal : true,
maximizable : false,
closeAction : "hide",
constrain : true,
layout : "vbox",
animateTarget:'target',
layoutConfig:{
align: "stretch"
},
items : [addFileFormPanel],
buttons:[
{text:'上传',handler:function (){
if(! addFileFormPanel.getForm().isValid()) {
return false;
}
//上传
addFileFormPanel.getForm().submit({
url:'uploadFile.do?method=up',
waitMsg: '正在上传...',
success: function (form,response){
Ext.Msg.alert('success',response.result.msg);
},
failure: function (form,response){
Ext.Msg.alert('error',response.result.msg);
}
});
}
}
]
});
});
相关文章推荐
- springMVC图片文件上传功能的实现
- 使用SpringMVC实现文件上传功能
- MyBatis与SpringMVC相结合实现文件上传、下载功能
- WebUploader+SpringMVC实现文件上传功能
- SpringMVC图片文件上传功能实现
- SpringMVC高速实现文件上传功能
- jquery.form.js框架实现文件上传功能案例解析(springmvc)
- SpringMVC(24):使用springmvc+spring+jdbc 优化订单管理系统的示例(多文件上传功能的实现)
- SpringMVC实现文件上传和下载功能
- SocketIo+SpringMvc实现文件的上传下载功能
- springMVC实现上传文件功能
- springMVC图片文件上传功能的实现
- SpringMVC下实现多文件上传功能
- SpringMVC快速实现文件上传功能
- Java与WebUploader相结合实现文件上传功能(实例代码)
- commons-fileupload实现文件上传功能实例
- springmvc实现多文件上传
- jquery-ajax+springMVC实现文件上传
- php 实现文件上传功能