最近項目須要新增一個發佈文章的模塊,用的是百度的Ueditor富文本編輯器。java
公司用的是阿里雲的圖片服務器,須要直接把文章中圖片上傳到服務器上,可是這個編輯器的上傳圖片是直接上傳到Tomcat的根目錄。web
不能知足要求,嘗試改造了一下上傳圖片的功能。spring
下載下來的編輯器直接導入項目webapp目錄下服務器
由於用的是Spring框架,基本已經包含了ueditor須要的幾個jar包,因此不須要導入了。app
須要注意的是,這個ueditor-1.1.1.jar的這個jar包,其實不須要導入,由於這個包裏面就只有一個文件Uploader.java框架
而在ueditor的jsp目錄下已經有了Uploader.java文件,因此直接把這個文件copy到工做區中,訪問這個文件就能夠了。webapp
在調用的地方改一下目錄jsp
把編輯器
<%@ page import="com.baidu.ueditor.um.Uploader" %>
改爲ide
<%@ page import="com.myproject.upload.Uploader"%>
以下圖:
而後關鍵就是要改造一下Uploader.java這個類。
原有上傳代碼以下:
1 public void upload() throws Exception { 2 boolean isMultipart = ServletFileUpload.isMultipartContent(this.request); 3 if (!isMultipart) { 4 this.state = this.errorInfo.get("NOFILE"); 5 return; 6 } 7 DiskFileItemFactory dff = new DiskFileItemFactory(); 8 String savePath = this.getFolder(this.savePath); 9 dff.setRepository(new File(savePath)); 10 try { 11 ServletFileUpload sfu = new ServletFileUpload(dff); 12 sfu.setSizeMax(this.maxSize * 1024); 13 sfu.setHeaderEncoding("utf-8"); 14 FileItemIterator fii = sfu.getItemIterator(this.request); 15 while (fii.hasNext()) { 16 FileItemStream fis = fii.next(); 17 if (!fis.isFormField()) { 18 this.originalName = fis.getName().substring(fis.getName().lastIndexOf(System.getProperty("file.separator")) + 1); 19 if (!this.checkFileType(this.originalName)) { 20 this.state = this.errorInfo.get("TYPE"); 21 continue; 22 } 23 this.fileName = this.getName(this.originalName); 24 this.type = this.getFileExt(this.fileName); 25 // 獲取了存放文件的相對路徑 26 this.url = savePath + "/" + this.fileName; 27 BufferedInputStream in = new BufferedInputStream(fis.openStream()); 28 File file = new File(this.getPhysicalPath(this.url)); 29 FileOutputStream out = new FileOutputStream( file ); 30 BufferedOutputStream output = new BufferedOutputStream(out); 31 // 這裏直接在服務器根目錄生成了圖片文件 32 Streams.copy(in, output, true); 33 this.state=this.errorInfo.get("SUCCESS"); 34 this.size = file.length(); 35 //UE中只會處理單張上傳,完成後即退出 36 break; 37 } else { 38 String fname = fis.getFieldName(); 39 //只處理title,其他表單請自行處理 40 if(!fname.equals("pictitle")){ 41 continue; 42 } 43 BufferedInputStream in = new BufferedInputStream(fis.openStream()); 44 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 45 StringBuffer result = new StringBuffer(); 46 while (reader.ready()) { 47 result.append((char)reader.read()); 48 } 49 this.title = new String(result.toString().getBytes(),"utf-8"); 50 reader.close(); 51 52 } 53 } 54 } catch (SizeLimitExceededException e) { 55 this.state = this.errorInfo.get("SIZE"); 56 } catch (InvalidContentTypeException e) { 57 this.state = this.errorInfo.get("ENTYPE"); 58 } catch (FileUploadException e) { 59 this.state = this.errorInfo.get("REQUEST"); 60 } catch (Exception e) { 61 this.state = this.errorInfo.get("UNKNOWN"); 62 } 63 }
改造後以下:
1 // 改造後的代碼,百度原有代碼註釋了 2 public void upload() throws Exception 3 { 4 boolean isMultipart = ServletFileUpload.isMultipartContent(this.request); 5 if (!isMultipart) 6 { 7 this.state = this.errorInfo.get("NOFILE"); 8 return; 9 } 10 try 11 { 12 MultipartResolver resolver = new CommonsMultipartResolver( 13 this.request.getSession().getServletContext()); 14 MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request); 15 CommonsMultipartFile orginalFile = (CommonsMultipartFile) multipartRequest.getFile("upfile"); 16 17 this.originalName = orginalFile.getOriginalFilename(); 18 if (!this.checkFileType(this.originalName)) 19 { 20 this.state = this.errorInfo.get("TYPE"); 21 return; 22 } 23 this.type = this.getFileExt(this.originalName); 24 this.size = orginalFile.getSize(); 25 26 // 這裏是公司內部上傳到阿里雲服務器的工具類 27 String key = ImgUtils.uploadImage("xxxx", 28 ImageKind.Picture, 29 orginalFile.getInputStream(), 30 this.size); 31 32 this.fileName = key; 33 this.url = "http://xxx.aliyuncs.com/" + key; 34 this.state = this.errorInfo.get("SUCCESS"); 35 } 36 catch (Exception e) 37 { 38 this.state = this.errorInfo.get("UNKNOWN"); 39 } 40 }
用到了Spring的這兩個文件
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
而後編輯器頁面上顯示的時候,img的src目錄須要改一下
1 callback: function (editor, $w, url, state) { 2 3 if (state == "SUCCESS") { 4 //顯示圖片計數+1 5 Upload.showCount++; 6 var $img = $("<img src='" + editor.options.imagePath + url + "' class='edui-image-pic' />"), 7 var $img = $("<img src='" + url + "' class='edui-image-pic' />"), 8 $item = $("<div class='edui-image-item edui-image-upload-item'><div class='edui-image-close'></div></div>").append($img); 9 10 if ($(".edui-image-upload2", $w).length < 1) { 11 $(".edui-image-content", $w).append($item); 12 13 Upload.render(".edui-image-content", 2) 14 .config(".edui-image-upload2"); 15 } else { 16 $(".edui-image-upload2", $w).before($item).show(); 17 } 18 19 $img.on("load", function () { 20 Base.scale(this, 120); 21 Base.close($(this)); 22 $(".edui-image-content", $w).focus(); 23 }); 24 25 } else { 26 currentDialog.showTip( state ); 27 window.setTimeout( function () { 28 29 currentDialog.hideTip(); 30 31 }, 3000 ); 32 } 33 34 Upload.toggleMask(); 35 36 }
大功告成。