官網(wǎng)地址http://ueditor.baidu.comGit 地址 https://github.com/fex-team/ueditor參考博客地址 http://blog.ncmem.com/wordpress/2019/08/12/ueditor-word%E5%9B%BE%E7%89%87%E8%BD%AC%E5%AD%98%E4...
官網(wǎng)地址http://ueditor.baidu.com
Git 地址 https://github.com/fex-team/ueditor
參考博客地址 http://blog.ncmem.com/wordpress/2019/08/12/ueditor-word%E5%9B%BE%E7%89%87%E8%BD%AC%E5%AD%98%E4%BA%A4%E4%BA%92/
1.4.2之后官方并沒(méi)有做功能的改動(dòng),1.4.2在word復(fù)制這塊沒(méi)有bug,其他版本會(huì)出現(xiàn)手動(dòng)無(wú)法轉(zhuǎn)存的情況
本文使用的后臺(tái)是Java。前端為Jsp(前端都一樣,后臺(tái)如果語(yǔ)言不通得自己做 Base64編碼解碼)
因?yàn)楣緲I(yè)務(wù)需要支持IE8 ,網(wǎng)上其實(shí)有很多富文本框,效果都很好。
例如www.wangEditor.com 但試了一圈都不支持IE8 。
所以回到Ueditor,由于官方?jīng)]有維護(hù),新的neuditor 也不知道什么時(shí)候能支持word自動(dòng)轉(zhuǎn)存,只能自己想辦法。
如果沒(méi)有必要,不建議使用ueditor。我也是沒(méi)有辦法。
改動(dòng)過(guò)后的插件只適合IE8。
這里要說(shuō)明的一點(diǎn)是百度官方的編輯器不支持word圖片批量轉(zhuǎn)存,粘貼word后需要手動(dòng)選擇圖片再進(jìn)行上傳一次操作。網(wǎng)上找到的大部分的示例都是這個(gè)操作。如果需要自動(dòng)批量上傳word圖片的話可以使用WordPaster這個(gè)控件。
1.IE設(shè)置
在受信任站點(diǎn)里添加信任網(wǎng)站。
這里本機(jī)測(cè)試使用的直接是 http://localhost
因?yàn)樾枰x取客戶端的文件,所以需要設(shè)置允許訪問(wèn)數(shù)據(jù)源。
ActiveXObject設(shè)置可以去網(wǎng)上參考,這里不列舉了。
前面的
到這里 IE 的準(zhǔn)備工作完成了。
修改ueditor.all.js關(guān)鍵代碼
14006行附近,如果是其他版本的ueditor,在功能正常的情況下,可以拷貝下面代碼。
var imgPath = attrs.src;
var imgUrl = attrs.src;
if (navigator.appName === 'Microsoft Internet Explorer') { //判斷是否是IE瀏覽器
if (navigator.userAgent.match(/Trident/i) && navigator.userAgent.match(/MSIE 8.0/i)) { //判斷瀏覽器內(nèi)核是否為Trident內(nèi)核IE8.0
var realPath = imgPath.substring(8, imgPath.length);
var filename = imgPath.substring(imgPath.lastIndexOf('/') + 1, imgPath.length);
var result = UploadForIE.saveAttachment(filename, realPath);
if (result) {
var json = eval('(' + result + ')');
imgUrl = json.url;
}
}
}
img.setAttr({
width: attrs.width,
height: attrs.height,
alt: attrs.alt,
word_img: attrs.src,
src: imgUrl,
'style': 'background:url(' + (flag ? opt.themePath + opt.theme + '/images/word.gif': opt.langPath + opt.lang + '/images/localimage.png') + ') no-repeat center center;border:1px solid #ddd'
})
uploadForIE.js。
var UploadForIE = {
// 保存到xml附件,并且通過(guò)ajax 上傳
saveAttachment: function(upload_filename, localFilePath) {
//后臺(tái)接受圖片保存的方法。
var upload_target_url = "uploadImg";
var strTempFile = localFilePath;
// 創(chuàng)建XML對(duì)象,組合XML文檔數(shù)據(jù)
var xml_dom = UploadForIE.createDocument();
xml_dom.loadXML('<?xml version="1.0" encoding="GBK" ?> <root/>');
// 創(chuàng)建ADODB.Stream對(duì)象
var ado_stream = new ActiveXObject("adodb.stream");
// 設(shè)置流數(shù)據(jù)類型為二進(jìn)制類型
ado_stream.Type = 1; // adTypeBinary
// 打開ADODB.Stream對(duì)象
ado_stream.Open();
// 將本地文件裝載到ADODB.Stream對(duì)象中
ado_stream.LoadFromFile(strTempFile);
// 獲取文件大小(以字節(jié)為單位)
var byte_size = ado_stream.Size;
// 設(shè)置數(shù)據(jù)傳輸單元大小為1KB
var byte_unit = 1024;
// 獲取文件分割數(shù)據(jù)單元的數(shù)量
var read_count = parseInt((byte_size / byte_unit).toString()) + parseInt(((byte_size % byte_unit) == 0) ? 0 : 1);
// 創(chuàng)建XML元素節(jié)點(diǎn),保存上傳文件名稱
var node = xml_dom.createElement("uploadFilename");
node.text = upload_filename.toString();
var root = xml_dom.documentElement;
root.appendChild(node);
// 創(chuàng)建XML元素節(jié)點(diǎn),保存上傳文件大小
var node = xml_dom.createElement("uploadFileSize");
node.text = byte_size.toString();
root.appendChild(node);
// 創(chuàng)建XML元素節(jié)點(diǎn),保存上傳文件內(nèi)容
for (var i = 0; i < read_count; i++) {
var node = xml_dom.createElement("uploadContent");
// 文件內(nèi)容編碼方式為Base64
node.dataType = "bin.base64";
// 判斷當(dāng)前保存的數(shù)據(jù)節(jié)點(diǎn)大小,根據(jù)條件進(jìn)行分類操作
if ((parseInt(byte_size % byte_unit) != 0) && (i == parseInt(read_count - 1))) {
// 當(dāng)數(shù)據(jù)包大小不是數(shù)據(jù)單元的整數(shù)倍時(shí),讀取最后剩余的小于數(shù)據(jù)單元的所有數(shù)據(jù)
node.nodeTypedValue = ado_stream.Read();
} else {
// 讀取一個(gè)完整數(shù)據(jù)單元的數(shù)據(jù)
node.nodeTypedValue = ado_stream.Read(byte_unit);
}
root.appendChild(node);
}
// 關(guān)閉ADODB.Stream對(duì)象
ado_stream.Close();
delete ado_stream;
// 創(chuàng)建Microsoft.XMLHTTP對(duì)象
// var xmlhttp = new ActiveXObject("microsoft.xmlhttp");
var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
// 打開Microsoft.XMLHTP對(duì)象
xmlhttp.open("post", upload_target_url, false);
// 使用Microsoft.XMLHTP對(duì)象上傳文件
xmlhttp.send(xml_dom);
var state = xmlhttp.readyState;
var success_state = true;
if (state != 4) {
success_state = false;
}
var result = xmlhttp.responseText;
delete xmlhttp;
return result;
},
// 創(chuàng)建DOMdocuemnt
createDocument: function() {
var xmldom;
var versions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument"],
i,
len;
for (i = 0, len = versions.length; i < len; i++) {
try {
xmldom = new ActiveXObject(versions[i]);
if (xmldom != null) break;
} catch(ex) {
//跳過(guò)
alert("創(chuàng)建document對(duì)象失敗!");
}
}
return xmldom;
}
}
UEditorAction保存圖片方法
@RequestMapping("/uploadImg")
public void uploadADO(HttpServletRequest request, HttpServletResponse response) {
String path1 = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +path1;
String rootPath = request.getServletContext().getRealPath("/");
// 設(shè)置數(shù)據(jù)傳輸單元大小為1KB
int unit_size = 1024;
// 初始化xml文件大小(以字節(jié)為單位)
int xmlFileSize = 0;
// 初始化上傳文件名稱(完整文件名)
String xmlFilename = "";
// 初始化上傳文件保存路徑(絕對(duì)物理路徑)
String xmlFilepath = "";
// 聲明文件存儲(chǔ)字節(jié)數(shù)組
byte[] xmlFileBytes = null;
try {
// 初始化 SAX 串行xml文件解析器
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(request.getInputStream());
Element eRoot = doc.getRootElement();
// 獲取上傳文件的完整名稱
Iterator it_name = eRoot.getChildren("uploadFilename").iterator();
if (it_name.hasNext()) {
xmlFilename = ((Element) it_name.next()).getText();
}
//存放的相對(duì)路徑目錄
String relativePath = "/temp/"+EditorUtil.getToday()+"/";
xmlFilepath = rootPath+ relativePath;
// 獲取上傳文件的大小
Iterator it_size = eRoot.getChildren("uploadFileSize").iterator();
if (it_size.hasNext()) {
xmlFileSize = Integer.parseInt(((Element) it_size.next())
.getText());
if (xmlFileSize > 0) {
int unit_count = 0;
// 為存儲(chǔ)文件內(nèi)容的字節(jié)數(shù)組分配存儲(chǔ)空間
xmlFileBytes = new byte[xmlFileSize];
// 循環(huán)讀取文件內(nèi)容,并保存到字節(jié)數(shù)組中
Iterator it_content = eRoot.getChildren("uploadContent")
.iterator();
while (it_content.hasNext()) {
// 初始化Base64編碼解碼器
BASE64Decoder base64 = new BASE64Decoder();
byte[] xmlNodeByteArray = base64
.decodeBuffer(((Element) it_content.next())
.getText());
if (xmlNodeByteArray.length >= unit_size) {
// 讀取一個(gè)完整數(shù)據(jù)單元的數(shù)據(jù)
System.arraycopy(xmlNodeByteArray, 0, xmlFileBytes,
unit_count * unit_size, unit_size);
} else {
// 讀取小于一個(gè)數(shù)據(jù)單元的所有數(shù)據(jù)
System.arraycopy(xmlNodeByteArray, 0, xmlFileBytes,
unit_count * unit_size, xmlFileSize
% unit_size);
}
// 繼續(xù)向下讀取文件內(nèi)容
unit_count++;
}
}
}
// 保存路徑
File path = new File(xmlFilepath);
if(!path.exists()){
path.mkdirs();
}
// 保存文件 word粘貼圖片的名稱
File file = new File(path,xmlFilename);
// 創(chuàng)建文件輸入輸出流
FileOutputStream fos = new FileOutputStream(file);
// 寫入文件內(nèi)容
fos.write(xmlFileBytes);
fos.flush();
// 關(guān)閉文件輸入輸出流
fos.close();
ReturnUploadImage rui = new ReturnUploadImage();
rui.setTitle(xmlFilename);//這里需要設(shè)置文件名稱如:xxx.jpg
rui.setOriginal(xmlFilename);//這里需要設(shè)置文件名稱如:xxx.jpg
rui.setState("SUCCESS");
rui.setUrl(basePath +relativePath+xmlFilename);
JSONObject json = new JSONObject(rui);
String result = json.toString();//這邊就是為了返回給UEditor做的格式轉(zhuǎn)換
response.getWriter().write(result);
} catch (Exception e) {
e.printStackTrace();
}
}
優(yōu)化后的代碼:
upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import = "Xproer.*" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%out.clear();
/*
更新記錄:
2013-01-25 取消對(duì)SmartUpload的使用,改用commons-fileupload組件。因?yàn)闇y(cè)試發(fā)現(xiàn)SmartUpload有內(nèi)存泄露的問(wèn)題。
*/
//String path = request.getContextPath();
//String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String uname = "";// = request.getParameter("uid");
String upass = "";// = request.getParameter("fid");
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
//upload.setSizeMax(262144);//256KB
List files = null;
try
{
files = upload.parseRequest(request);
}
catch (FileUploadException e)
{// 處理文件尺寸過(guò)大異常
out.println("上傳文件異常:"+e.toString());
return;
}
FileItem imgFile = null;
// 得到所有上傳的文件
Iterator fileItr = files.iterator();
// 循環(huán)處理所有文件
while (fileItr.hasNext())
{
// 得到當(dāng)前文件
imgFile = (FileItem) fileItr.next();
// 忽略簡(jiǎn)單form字段而不是上傳域的文件域(<input type="text" />等)
if(imgFile.isFormField())
{
String fn = imgFile.getFieldName();
String fv = imgFile.getString();
if(fn.equals("uname")) uname = fv;
if(fn.equals("upass")) upass = fv;
}
else
{
break;
}
}
Uploader up = new Uploader(pageContext,request);
up.SaveFile(imgFile);
String url = up.GetFilePathRel();
out.write(url);
response.setHeader("Content-Length",url.length()+"");//返回Content-length標(biāo)記,以便控件正確讀取返回地址。
%>
剩下的后臺(tái)功能和js參考下載文件中的UEditorAction 和 uploadForIE.js。
下面是我安裝的依賴pom結(jié)構(gòu),可以根據(jù)自己的進(jìn)行調(diào)整。
<dependency>
<groupId>com.baidu</groupId>
<artifactId>ueditor</artifactId>
<version>1.1.0</version>
</dependency>
基于springboot 和idea ,這里只提取了自動(dòng)轉(zhuǎn)存功能出來(lái),功能還沒(méi)測(cè)試,git代碼沒(méi)做公開,等后續(xù)測(cè)試好了再公開。
可以先使用csdn下載查看代碼。
pom里引用了ueditor.jar
需要根據(jù)各自情況安裝jar包
1.4.2中的jar包版本是1.1.0
mvn install:install-file -DgroupId=com.baidu -DartifactId=ueditor -Dversion=1.1.0 -Dpackaging=jar -Dfile=\ueditor\jsp\lib\ueditor-1.1.0.jar
運(yùn)行
UeditorApplication的main方法
然后訪問(wèn)http://localhost:8088/ueditor/ 就可以測(cè)試了。
完成后的效果:
圖片自動(dòng)批量上傳,不需要手動(dòng)一張張選擇圖片上傳,用戶體驗(yàn)比百度ueditor自帶的更好,傳圖效率更高。
上傳成功后,圖片地址自動(dòng)替換成服務(wù)器地址
圖片自動(dòng)保存在服務(wù)器中
詳細(xì)資料可以參考這篇文章:
本文地址:https://blog.csdn.net/Mr_Zang666/article/details/103177274
來(lái)源:本文內(nèi)容搜集或轉(zhuǎn)自各大網(wǎng)絡(luò)平臺(tái),并已注明來(lái)源、出處,如果轉(zhuǎn)載侵犯您的版權(quán)或非授權(quán)發(fā)布,請(qǐng)聯(lián)系小編,我們會(huì)及時(shí)審核處理。
聲明:江蘇教育黃頁(yè)對(duì)文中觀點(diǎn)保持中立,對(duì)所包含內(nèi)容的準(zhǔn)確性、可靠性或者完整性不提供任何明示或暗示的保證,不對(duì)文章觀點(diǎn)負(fù)責(zé),僅作分享之用,文章版權(quán)及插圖屬于原作者。
Copyright?2013-2024 JSedu114 All Rights Reserved. 江蘇教育信息綜合發(fā)布查詢平臺(tái)保留所有權(quán)利
蘇公網(wǎng)安備32010402000125
蘇ICP備14051488號(hào)-3技術(shù)支持:南京博盛藍(lán)睿網(wǎng)絡(luò)科技有限公司
南京思必達(dá)教育科技有限公司版權(quán)所有 百度統(tǒng)計(jì)