1. 程式人生 > >wangedit上傳圖片的伺服器

wangedit上傳圖片的伺服器

後臺
@ApiOperation(notes = "返回一個檔名,需要呼叫 /home/download方法才可以獲取到檔案", httpMethod = "POST", value = "檔案/圖片上傳")
	@RequestMapping("/uploaderPic2")
	@ResponseBody
	public Object uploadHeadPic2(@RequestParam("file")MultipartFile file,HttpServletRequest request,HttpServletResponse response) throws IOException{

		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String uploadFilename = Random.getId()+suffix;
		FileUtils.copyInputStreamToFile(file.getInputStream(), new File(Constant.UPLOAD_DIR, uploadFilename));
		Map<String, String> map = new HashMap<String, String>();
		map.put("data",picDownloadServerUrl+"?filename="+uploadFilename);//這裡應該是專案路徑
		return map;//將圖片地址返回
	}

jsp

		root.editor = new wangEditor('#product-content');
				root.editor.customConfig.uploadImgServer = 'http://api.farmmermall.com/WebUploader/uploaderPic2'
				root.editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024;
				root.editor.customConfig.uploadImgMaxLength = 5;    
				root.editor.customConfig.uploadFileName = 'file';
				root.editor.customConfig.uploadImgHooks = {
				    customInsert: function (insertImg, result, editor) {
				                // 圖片上傳並返回結果,自定義插入圖片的事件(而不是編輯器自動插入圖片!!!)
				                // insertImg 是插入圖片的函式,editor 是編輯器物件,result 是伺服器端返回的結果
				         
				                // 舉例:假如上傳圖片成功後,伺服器端返回的是 {url:'....'} 這種格式,即可這樣插入圖片:
				                var url =result.data;
				                insertImg(url);
				         
				                // result 必須是一個 JSON 格式字串!!!否則報錯
				            }
				        }
		        root.editor.create();