ueditor 自定義上傳圖片,上傳圖片自定義
阿新 • • 發佈:2019-01-29
越來越不想寫部落格了,真的是累!
好了,廢話不多說,進入正題。
前臺:
既然你們都走到這裡來了,那麼前面的就不在多說了,直接進去配置好的頁面。
<script type="text/javascript"> var ue = UE.getEditor('editor', { maximumWords : 2000000, //允許的最大字元數 sourceEditorFirst : false, allHtmlEnabled : true, }); //圖片和檔案存入OSS中 UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl; UE.Editor.prototype.getActionUrl = function(action) { if (action == 'uploadimage' || action == 'uploadfile') { return '${path}/web/ueuploadimageoss!ueUploadImgaeOSS'; //此處改需要把圖片上傳到哪個Action(Controller)中 } else { return this._bkGetActionUrl.call(this, action); } }; </script>
需要上傳的路徑返回過去,記得把config.json 中的 imageUrlPrefix 清空。
後臺:
package com.yuneecuc.action.web; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletResponse; import org.apache.struts2.convention.annotation.Action; import org.json.JSONObject; import com.yuneecuc.action.PublicAccessAction; import com.yuneecuc.base.OSSUtils; import com.yuneecuc.entity.SavedImage; @Action("ueuploadimageoss") public class UEUploadImageOSSAction extends ActionSupport { private File[] upfile; private String[] upfileFileName; private String[] upfileContentType; public void ueUploadImgaeOSS() { if (upfile == null) { resultUEMessage("圖片不能為空", false, ""); return; } SavedImage si = null; for (int i = 0; i < upfile.length; i++) { String fileExtension = upfileContentType[i]; try { si = OSSUtils.uploadFileBinary(new FileInputStream(upfile[i]), fileExtension.substring(fileExtension.lastIndexOf("/") + 1, fileExtension.length()), null, -1, null, null); } catch (IOException e) { e.printStackTrace(); resultUEMessage("IO異常", false, ""); return; } catch (Exception e) { e.printStackTrace(); resultUEMessage("上傳失敗", false, ""); return; } } resultUEMessage("SUCCESS", true, si.getUrl()); } /** * 返回UEditor的資訊 * * @param message * 錯誤的訊息提示 * @param status * 是否成功 * @param url * 圖片的URL */ private void resultUEMessage(String message, boolean status, String url) { JSONObject jsobject = new JSONObject(); if (status) { jsobject.put("state", message); jsobject.put("url", url); jsobject.put("original", ""); } else { jsobject.put("state", message); jsobject.put("url", ""); jsobject.put("title", ""); jsobject.put("original", ""); } ServletResponse response = getResponse(); response.setContentType("application/json;charset=utf-8"); try { String outputStr = ""; if (jsobject != null) { outputStr = jsobject.toString(); } // ServletOutputStream對UTF-8的支援不好,此處用Writer PrintWriter out = response.getWriter(); out.write(outputStr); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } public File[] getUpfile() { return upfile; } public void setUpfile(File[] upfile) { this.upfile = upfile; } public String[] getUpfileFileName() { return upfileFileName; } public void setUpfileFileName(String[] upfileFileName) { this.upfileFileName = upfileFileName; } public String[] getUpfileContentType() { return upfileContentType; } public void setUpfileContentType(String[] upfileContentType) { this.upfileContentType = upfileContentType; } }
非常簡單,核心是:只要知道哪裡改上傳的URL 和怎麼返回資料格式。