当前位置: 首页 > 工具软件 > Summernote > 使用案例 >

summernote复制无法保存图片问题

白修谨
2023-12-01

1、前端初始化

var imgReg = " /<img.*?(?:>|\\/>)/gi";

var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;

var reg = "[a-zA-z]+://[^\s]*";

$('.summernote1').summernote({

height: 200,

tabsize: 2,

lang: 'zh-CN',

callbacks : {

onPaste : function(e) {

console.log(e)

setTimeout(function() {

var str = $('.summernote1').summernote('code');

//匹配图片(g表示匹配所有结果i表示区分大小写)

var imgReg = /<img.*?(?:>|\/>)/gi;

//匹配src属性

var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;

var arr = str.match(imgReg);

if(arr != null) {

/* var formData = new FormData(); */

for (var i = 0; i < arr.length; i++) {

var src = arr[i].match(srcReg);

//当然你也可以替换src属性

if (src) {

$.ajax({

url : "${pageContext.request.contextPath}/knowledgePoint/saveParseImg.do?id=${knowledgePoint.id}&pathName=" + src[1],

type : 'post',

async: false,

/* data : formData, */

processData : false,

contentType : false,

success : function(data) {

str = str.replace(src[0], 'src="' + data + '"');

$('.summernote1').summernote('code', str);

}

});

}

}

}

}, 1000);

}

}

});

2、后台保存方法

@RequestMapping("/saveParseImg")

@ResponseBody

public String savePasteImg(String id, String pathName, HttpServletRequest request) {

StringBuffer requestURL = request.getRequestURL();

String domainURL = requestURL.delete(requestURL.length() - request.getRequestURI().length(), requestURL.length()).append(request.getServletContext().getContextPath())

.append("/").toString();

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

String oriFileName = "";

String newFileName = "";

String errorMessage = null;

String type = "attachment";

HttpURLConnection conn = null;

BufferedInputStream bis = null;

FileOutputStream out = null;

InputStream in = null;

try {

String folder = request.getServletContext().getRealPath("");

StringBuffer savePath = new StringBuffer(folder).append(File.separator).append("knowledgebase").append(File.separator).append(id).append(File.separator)

.append(type).append(File.separator);

File f1 = new File(savePath.toString());

if (!f1.exists())

f1.mkdirs();

// 生成目录

String[] arr = pathName.split("/");

oriFileName = arr[arr.length - 1]; //pathName.split("/")[-1];

int index = oriFileName.indexOf(".");

String ext = oriFileName.substring(index);

if (ext.equals(".jpg") || ext.equals(".bmp") || ext.equals(".png") || ext.equals(".gif") || ext.equals(".pdg") || ext.equals(".jpeg")) {

type = "images";

StringBuffer savePath2 = new StringBuffer(folder).append(File.separator).append("knowledgebase").append(File.separator).append(id).append(File.separator).append(type).append(File.separator);

File f2 = new File(savePath2.toString());

if (!f2.exists())

f2.mkdirs();

int idRom = (int) ((Math.random() * 9 + 1) * 10000);

String rom = Integer.toString(idRom);

newFileName = dateFormat.format(new Date()) + rom + ext;

File uploadFile = new File(savePath2 + newFileName);

URL httpUrl = new URL(pathName);

conn = (HttpURLConnection) httpUrl.openConnection();

conn.setRequestMethod("GET");

conn.setDoInput(true);

conn.setDoOutput(true);

conn.setUseCaches(false);

conn.connect();

out = new FileOutputStream(uploadFile);

in = conn.getInputStream();

bis = new BufferedInputStream(in);

byte[] bytes = new byte[1024];

int len = 0;

while ((len = bis.read(bytes)) != -1) {

out.write(bytes, 0, len);

}

StringBuffer url = new StringBuffer(domainURL).append(Constants.KNOWLEDGE_BASE_ROOT_PATH).append(id).append("/").append(type).append("/").append(newFileName);

String urlNew = url.toString();

return urlNew;

} else {

if (ext.equals(".jsp")) {

errorMessage = "拒绝上传JSP类型文件";

}

int idRom = (int) ((Math.random() * 9 + 1) * 10000);

String rom = Integer.toString(idRom);

newFileName = dateFormat.format(new Date()) + rom + ext;

File uploadFile = new File(savePath + newFileName);

URL httpUrl = new URL(pathName);

conn = (HttpURLConnection) httpUrl.openConnection();

conn.setRequestMethod("GET");

conn.setDoInput(true);

conn.setDoOutput(true);

conn.setUseCaches(false);

conn.connect();

out = new FileOutputStream(uploadFile);

in = conn.getInputStream();

bis = new BufferedInputStream(in);

byte[] bytes = new byte[1024];

int len = 0;

while ((len = bis.read(bytes)) != -1) {

out.write(bytes, 0, len);

}

StringBuffer url = new StringBuffer(domainURL).append(Constants.KNOWLEDGE_BASE_ROOT_PATH).append(id).append("/").append(type).append("/").append(newFileName);

String urlNew = url.toString();

return urlNew;

}

} catch (Exception ex) {

errorMessage = "上传失败:" + ex;

} finally {

try {

if(in != null) {

in.close();

}

if(bis != null) {

bis.close();

}

if(out != null) {

out.close();

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

return "";

}

 类似资料: