springMVC實現上傳文件功能

1.spring_fileupload.xml配置文件以下: 前端

Xml代碼 複製代碼 收藏代碼
  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  2. <!--<property name="maxUploadSize" value="10485760"></property>-->
  3. </bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--<property name="maxUploadSize" value="10485760"></property>-->
</bean>

2.FileUploadAction控制器以下: java

Java代碼 複製代碼 收藏代碼
  1. @RequestMapping(params = "method=up", method = RequestMethod.POST)
  2. @ResponseBody
  3. public Map<String, String> uploadSolver(@RequestParam MultipartHttpServletRequest request {
  4. Map<String,String> info = new HashMap<String,String>();
  5. MultipartFile patch = request.getFile("file");
  6. if (!patch.isEmpty()) {
  7. try {
  8. String fileName = patch.getOriginalFilename();
  9. /**
  10. * 獲取文件後綴
  11. */
  12. System.out.println(fileName);
  13. String suffix = fileName.substring(fileName.lastIndexOf("."));
  14. /**
  15. * 判斷上傳的文件格式是否正確
  16. */
  17. if ((".zip.rar.gz.tar.bz2.txt".indexOf(suffix.toLowerCase()) != -1)) {
  18. Integer fileSize = (int) patch.getSize() / 1024;
  19. /**
  20. * 若是文件小於10M,則上傳文件,不然提示用戶不能超過10M
  21. */
  22. if (fileSize <= 10240) {
  23. String uploadPath = ClassLoaderUtil.getProperties("uploadFile.properties").getProperty("filePath");
  24. System.out.println(uploadPath);
  25. File filePath = new File(request.getSession()
  26. .getServletContext().getRealPath(uploadPath));
  27. System.out.println(filePath.getAbsolutePath());
  28. /**
  29. * 判讀存儲文件路是否存在,不存在則建立
  30. */
  31. if (! filePath.exists()) {
  32. filePath.mkdirs();
  33. System.out.println("上傳文件路徑不存在,建立成功!");
  34. }
  35. /**
  36. * 文件開始上傳到服務器上
  37. */
  38. patch.transferTo(new File(filePath.getAbsolutePath()+"\\"+fileName));
  39. info.put("success", "true");
  40. info.put("msg", "上傳成功!");
  41. } else {
  42. System.out.println("上傳的文件太大,文件大小不能超過10M");
  43. info.put("success","false");
  44. info.put("msg", "上傳的文件太大,文件大小不能超過10M");
  45. }
  46. } else {
  47. System.out.println("上傳的文件格式不支持");
  48. info.put("success","false");
  49. info.put("msg", "上傳的文件格式不支持");
  50. }
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. System.out.println("系統異常");
  54. info.put("success","false");
  55. info.put("msg", "系統異常");
  56. }
  57. }
  58. return info;
  59. }
@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

Js代碼 複製代碼 收藏代碼
  1. Ext.onReady(function (){
  2. var addFileTextField = new Ext.form.TextField({
  3. name:'file',
  4. allowBlank:false,
  5. //使用HTML中的filetext
  6. inputType:'file'
  7. });
  8. var addFileFormPanel = new Ext.form.FormPanel({
  9. autoDestory:true,
  10. fileUpload:true,
  11. frame:true,
  12. width:300,
  13. autoHeight:true,
  14. labelAlign:'right',
  15. labelWidth:60,
  16. defaultType:'textfield',
  17. defaults:{width:200,allowBlank:false},
  18. items: [addFileTextField]
  19. });
  20. var addFileWindow = new Ext.Window({
  21. id : "addFileWindow",
  22. title : "上傳文件",
  23. width : 640,
  24. height : 200,
  25. resizable : false,
  26. modal : true,
  27. maximizable : false,
  28. closeAction : "hide",
  29. constrain : true,
  30. layout : "vbox",
  31. animateTarget:'target',
  32. layoutConfig:{
  33. align: "stretch"
  34. },
  35. items : [addFileFormPanel],
  36. buttons:[
  37. {text:'上傳',handler:function (){
  38. if(! addFileFormPanel.getForm().isValid()) {
  39. return false;
  40. }
  41. //上傳
  42. addFileFormPanel.getForm().submit({
  43. url:'uploadFile.do?method=up',
  44. waitMsg: '正在上傳...',
  45. success: function (form,response){
  46. Ext.Msg.alert('success',response.result.msg);
  47. },
  48. failure: function (form,response){
  49. Ext.Msg.alert('error',response.result.msg);
  50. }
  51. });
  52. }
  53. }
  54. ]
  55. });
  56. });
相關文章
相關標籤/搜索