1.spring_fileupload.xml配置文件以下: 前端
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <!--<property name="maxUploadSize" value="10485760"></property>-->
- </bean>
<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;
- }
@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 以下: web
- 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);
- }
- });
- }
- }
- ]
- });
- });