springMVC实现上传文件功能

1.spring_fileupload.xml配置文件如下:

Xml代码 复制代码 收藏代码29093242_5PGL.gif
  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代码 复制代码 收藏代码29093242_5PGL.gif
  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 如下:

Js代码 复制代码 收藏代码29093242_5PGL.gif
  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. });

转载于:https://my.oschina.net/u/615071/blog/106036

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值