img summernote 加类_summernote复制无法保存图片问题

1、前端初始化

var imgReg = " /|\\/>)/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 = /|\/>)/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 "";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值