springMVC + swfupload 附件上传功能的实现

转载地址:http://blog.csdn.net/houpengfei111/article/details/9371829


[html]  view plain copy print ?
  1.                             flash_url : "${ctx}/resources/plugins/swfupload/swfupload.swf",  
  2. 是指定swfupload 的 swf的flash的地址                                                       
  3. upload_url: "${ctx}/fileUpload/swfupload?jsessionid=<%=request.getSession().getId()%>",  
  4. //这个是上传附件的上传地址                
[html]  view plain copy print ?
  1.                             file_post_name:"filedata",  
  2. 是后台接受的参数名称(这里写什么,后台就接什么)                  
  3. button_action:SWFUpload.BUTTON_ACTION.SELECT_FILES,  
  4. 是设置swf 是否是多选还是单选,单选设置成“SWFUpload.BUTTON_ACTION.SELECT_FILE”  
[html]  view plain copy print ?
  1.                 file_size_limit : "100 MB",  
  2.                 file_types : "*.*",  
  3.                 file_types_description : "All Files",  
  4.                 file_upload_limit : 10,  
  5. //允许上传的最大数  
[html]  view plain copy print ?
  1. file_queue_limit : 5,  
  2. 上传队列的最大数  
[html]  view plain copy print ?
  1. custom_settings : {  
  2.     progressTarget : "fsUploadProgress",  
  3.     cancelButtonId : "btnCancel"  
  4. },  
  5.   
  6. debug: false,  
  7. bug模式是否开启  
  8. // 按钮的设置  
  9. button_image_url: "${ctx}/resources/images/TestImageNoText_65x29.png",  
  10. button_width: "65",  
  11. button_height: "29",  
  12. button_placeholder_id: "spanButtonPlaceHolder",  
  13. button_text: '<span class="theFont">选择</span>',  
  14. button_text_style: ".theFont { font-size: 16; }",  
  15. button_text_left_padding: 12,  
  16. button_text_top_padding: 3,  
  17.   
  18. // The event handler functions are defined in handlers.js  
  19. file_queued_handler : fileQueued,  
  20. file_queue_error_handler : fileQueueError,  
  21. file_dialog_complete_handler : fileDialogComplete,  
  22. 监听器的配置(可以自己写,在相应的时间会调用相应的方法)  
[html]  view plain copy print ?
  1. upload_start_handler : uploadStart,  
  2. upload_progress_handler : uploadProgress,  
  3. upload_error_handler : uploadError,  
  4. upload_success_handler : uploadSuccess,  
  5. upload_complete_handler : uploadComplete,  
  6. queue_complete_handler : queueComplete  // Queue plugin event  

其他的基本也都能看懂吧!其实很简单的,但是一下午都卡在了,上传多个附件时,默认只上传队列中的第一个,剩余的都不上传。这里可以这样处理:

function uploadComplete(){

swfUploadObj.startUpload();

}
最好是修改了swfupload.js中的方法如下:

[html]  view plain copy print ?
  1. SWFUpload.prototype.uploadComplete = function (file) {  
  2.     file = this.unescapeFilePostParams(file);  
  3.     this.queueEvent("upload_complete_handler", file);  
  4.     this.startUpload();  
  5. };  

即可解决问题。

spring mvc 的cotroller中代码如下:

[java]  view plain copy print ?
  1. @RequestMapping("/swfupload")  
  2.     public ModelAndView fileUpload(HttpServletRequest request,  
  3.             @RequestParam("filedata") MultipartFile file) throws Exception {  
  4.         try {  
  5.             String uploadDir = request.getRealPath("/upload");  
  6.             File dirPath = new File(uploadDir);  
  7.             if (!dirPath.exists()) {  
  8.                 dirPath.mkdirs();  
  9.             }  
  10.             file.transferTo(new File(uploadDir + "/" + file.getOriginalFilename()));  
  11.         } catch (IllegalStateException | IOException e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         ModelAndView mv = new ModelAndView();  
  15.         mv.setViewName("system/file/upload");  
  16.         return mv;  
  17.     }  


这里要提出,必须return 一个ModelAndview。

在网上找到的写法是这样的:

[java]  view plain copy print ?
  1. public ModelAndView upload(HttpServletRequest request,     
  2.             HttpServletResponse response) throws Exception{      
  3.         try{      
  4.             String uploadDir = request.getRealPath("/upload");      
  5.             File dirPath = new File(uploadDir);      
  6.             if (!dirPath.exists()) {      
  7.                 dirPath.mkdirs();      
  8.             }      
  9.                   
  10.             MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;      
  11.             CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("filedata");//这里是表单的名字,在swfupload.js中this.ensureDefault("file_post_name", "filedata");      
  12.                   
  13.             InputStream stream = file.getInputStream();      
  14.             String fileName = file.getOriginalFilename();      
  15.             fileName = new String(fileName.getBytes(),"utf-8");      
  16.             String fileNameFull = uploadDir + Constants.FILE_SEP + fileName;      
  17.             OutputStream bos = new FileOutputStream(fileNameFull);      
  18.             int bytesRead = 0;      
  19.             byte[] buffer = new byte[8192];      
  20.             while ((bytesRead = stream.read(buffer, 08192)) != -1) {      
  21.                 bos.write(buffer, 0, bytesRead);      
  22.             }      
  23.      
  24.             bos.close();      
  25.             // close the stream      
  26.             stream.close();      
  27.                   
  28.         }catch(Exception e){      
  29.             e.printStackTrace();      
  30.         }    


后来发现springmvc 有这样的支持,是不需要自己去得到,直接注入即可。

再贴上和上传有关的配置:

[html]  view plain copy print ?
  1. <bean id="multipartResolver"  
  2.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  3.         <!-- one of the properties available; the maximum file size in bytes -->  
  4.         <property name="maxUploadSize" value="10485760000000" />  
  5.     </bean>  

ok 搞定!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值