转载地址:http://blog.csdn.net/houpengfei111/article/details/9371829
- flash_url : "${ctx}/resources/plugins/swfupload/swfupload.swf",
- 是指定swfupload 的 swf的flash的地址
- upload_url: "${ctx}/fileUpload/swfupload?jsessionid=<%=request.getSession().getId()%>",
- //这个是上传附件的上传地址
- file_post_name:"filedata",
- 是后台接受的参数名称(这里写什么,后台就接什么)
- button_action:SWFUpload.BUTTON_ACTION.SELECT_FILES,
- 是设置swf 是否是多选还是单选,单选设置成“SWFUpload.BUTTON_ACTION.SELECT_FILE”
- file_size_limit : "100 MB",
- file_types : "*.*",
- file_types_description : "All Files",
- file_upload_limit : 10,
- //允许上传的最大数
- file_queue_limit : 5,
- 上传队列的最大数
- custom_settings : {
- progressTarget : "fsUploadProgress",
- cancelButtonId : "btnCancel"
- },
- debug: false,
- bug模式是否开启
- // 按钮的设置
- button_image_url: "${ctx}/resources/images/TestImageNoText_65x29.png",
- button_width: "65",
- button_height: "29",
- button_placeholder_id: "spanButtonPlaceHolder",
- button_text: '<span class="theFont">选择</span>',
- button_text_style: ".theFont { font-size: 16; }",
- button_text_left_padding: 12,
- button_text_top_padding: 3,
- // The event handler functions are defined in handlers.js
- file_queued_handler : fileQueued,
- file_queue_error_handler : fileQueueError,
- file_dialog_complete_handler : fileDialogComplete,
- 监听器的配置(可以自己写,在相应的时间会调用相应的方法)
- upload_start_handler : uploadStart,
- upload_progress_handler : uploadProgress,
- upload_error_handler : uploadError,
- upload_success_handler : uploadSuccess,
- upload_complete_handler : uploadComplete,
- queue_complete_handler : queueComplete // Queue plugin event
其他的基本也都能看懂吧!其实很简单的,但是一下午都卡在了,上传多个附件时,默认只上传队列中的第一个,剩余的都不上传。这里可以这样处理:
function uploadComplete(){
swfUploadObj.startUpload();
}
最好是修改了swfupload.js中的方法如下:
- SWFUpload.prototype.uploadComplete = function (file) {
- file = this.unescapeFilePostParams(file);
- this.queueEvent("upload_complete_handler", file);
- this.startUpload();
- };
即可解决问题。
spring mvc 的cotroller中代码如下:
- @RequestMapping("/swfupload")
- public ModelAndView fileUpload(HttpServletRequest request,
- @RequestParam("filedata") MultipartFile file) throws Exception {
- try {
- String uploadDir = request.getRealPath("/upload");
- File dirPath = new File(uploadDir);
- if (!dirPath.exists()) {
- dirPath.mkdirs();
- }
- file.transferTo(new File(uploadDir + "/" + file.getOriginalFilename()));
- } catch (IllegalStateException | IOException e) {
- e.printStackTrace();
- }
- ModelAndView mv = new ModelAndView();
- mv.setViewName("system/file/upload");
- return mv;
- }
这里要提出,必须return 一个ModelAndview。
在网上找到的写法是这样的:
- public ModelAndView upload(HttpServletRequest request,
- HttpServletResponse response) throws Exception{
- try{
- String uploadDir = request.getRealPath("/upload");
- File dirPath = new File(uploadDir);
- if (!dirPath.exists()) {
- dirPath.mkdirs();
- }
- MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
- CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("filedata");//这里是表单的名字,在swfupload.js中this.ensureDefault("file_post_name", "filedata");
- InputStream stream = file.getInputStream();
- String fileName = file.getOriginalFilename();
- fileName = new String(fileName.getBytes(),"utf-8");
- String fileNameFull = uploadDir + Constants.FILE_SEP + fileName;
- OutputStream bos = new FileOutputStream(fileNameFull);
- int bytesRead = 0;
- byte[] buffer = new byte[8192];
- while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
- bos.write(buffer, 0, bytesRead);
- }
- bos.close();
- // close the stream
- stream.close();
- }catch(Exception e){
- e.printStackTrace();
- }
后来发现springmvc 有这样的支持,是不需要自己去得到,直接注入即可。
再贴上和上传有关的配置:
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <!-- one of the properties available; the maximum file size in bytes -->
- <property name="maxUploadSize" value="10485760000000" />
- </bean>
ok 搞定!