ueditor上傳到阿里雲(程式碼最少改動)
阿新 • • 發佈:2018-11-06
ueditor上傳到阿里雲(程式碼最少改動)
- 官網下載 ueditor1_4_2-utf8-jsp,這裡提供下載地址,裡面不包含jar包。http://download.csdn.net/detail/u013024120/9708899
- 把編輯器放入JavaWeb專案中,引入jar包,測試圖片,視訊,檔案的上傳功能,一般可以直接正常使用。
- 找到編輯器裡jsp資料夾開啟,其中config.json是上傳路徑配置不用修改;開啟controller.jsp,把其中”程式碼1”替換為”程式碼2”搞定,其中,ChangeOSS類是我手動建立的一個用來替換圖片或視屏地址的類。
// 程式碼1
out.write( new ActionEnter( request, rootPath ).exec() );
// 程式碼2
out.write( ChangeOSS.changeOSSFunction(request, new ActionEnter( request, rootPath ).exec() ));
ChangeOSS類定義如下,這裡要注意的是StringUtil.isBlank()要判斷空,空串,”null”字串:
import java.io.File;
import java.io.FileInputStream;
import java.io .InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
public class ChangeOSS {
public static String changeOSSFunction(HttpServletRequest request, String json) {
if (!StringUtil.isBlank (json)) {
@SuppressWarnings("unchecked")
Map<String, Object> map = JSONObject.parseObject(json, Map.class);
if (true) {
// 點選上傳圖片
String url = map.get("url") + "";
if (!StringUtil.isBlank(url)) {
String fileName = url.substring(url.lastIndexOf("/") + 1);
try {
String r = request.getSession().getServletContext().getRealPath("");
// r = r.substring(0, r.lastIndexOf("[PROJECT_NAME]") - 1); // 如果編輯器獲取到的真實路徑和專案同級,需要加上這一句。
String path = r + url;
File file = new File(path);
InputStream imgStream = new FileInputStream(file);
OSSUtil.uploadFile(fileName, imgStream);
imgStream.close();
} catch (Exception e) {
e.printStackTrace();
}
map.put("url", "http://xxx.oss-cn-beijing.aliyuncs.com/" + fileName);
}
}
if (true) {
// 點選上傳圖片
String list = map.get("list") + "";
if (!StringUtil.isBlank(list)) {
@SuppressWarnings("unchecked")
List<Object> listINFO = JSONObject.parseObject(list, ArrayList.class);
for (int i = 0; i < listINFO.size(); i++) {
@SuppressWarnings("unchecked")
Map<String, Object> mapList = JSONObject.parseObject(listINFO.get(i) + "", Map.class);
String urllist = mapList.get("url") + "";
if (!StringUtil.isBlank(urllist)) {
String fileName = urllist.substring(urllist.lastIndexOf("/") + 1);
try {
String r = request.getSession().getServletContext().getRealPath("");
// r = r.substring(0, r.lastIndexOf("[PROJECT_NAME]") - 1); // 如果編輯器獲取到的真實路徑和專案同級,需要加上這一句。
String path = r + urllist;
File file = new File(path);
InputStream imgStream = new FileInputStream(file);
OSSUtil.uploadFile(fileName, imgStream);
imgStream.close();
} catch (Exception e) {
e.printStackTrace();
}
mapList.put("url", "http://xxx.oss-cn-beijing.aliyuncs.com/" + fileName);
listINFO.set(i, mapList);
}
}
map.put("list", listINFO);
}
}
return JSONObject.toJSONString(map);
}
return json;
}
}
附StringUtil方法:
/**
* String為空判斷
*/
public static boolean isBlank(String str) {
int strLen;
if ((str == null) || "null".equals(str) || ((strLen = str.length()) == 0)) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
}