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,//debug模式是否開啟// 按鈕的設定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.jsfile_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 搞定。