2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 富文本编辑器Froala editor上传图片到指定的服务器

富文本编辑器Froala editor上传图片到指定的服务器

时间:2021-10-30 04:17:55

相关推荐

富文本编辑器Froala editor上传图片到指定的服务器

1.在加载前端js时,先加载下面方法

$('.edit').editable({imageUploadURL: base.domain +"NewSoft/ziliaoceshi.do",})

在上面的代码中你看到imageUploadURL,imageUploadURL其实就是你处理该插件上传图片的后台方法地址,而不是你的图片要上传到的路径。

下面贴出我的后台处理方法。

2.后台代码,这里是上传到OSS服务器的

@RequestMapping(value ="/ziliaoceshi",method = RequestMethod.POST)public void ziliaoceshi(HttpServletRequest request,HttpServletResponse response,@RequestParam(value = "file") MultipartFile file) throws IOException {request.setCharacterEncoding("UTF-8");// 创建OSS连接OSSClient ossClient = new OSSClient(ConfigInfo.getByProperties("ossjichu.endpoint"), ConfigInfo.getByProperties("ossjichu.accessKeyId"),ConfigInfo.getByProperties("ossjichu.accessKeySecret"));//创建OSSClient实例String bucket = ConfigInfo.getByProperties("ossjichu.bucket");//给上传的图片重命名String adsPic = ConfigInfo.getByProperties("softWen")+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+Tools.createRandomID(5)+".jpg";String picUrl="";//图片路径try {InputStream ips =file.getInputStream();ossClient.putObject(bucket, adsPic, ips);ossClient.shutdown();ips.close();picUrl="https://" + bucket + ".oss-cn-/"+ adsPic;Log.info("上传软文正文图片oss路径:"+picUrl);} catch (IOException e) {e.printStackTrace();Log.info("上传软文正文图片失败:"+e);}Map<String,String>map = new HashMap<>();map.put("link",picUrl);Gson gson=new Gson();System.out.println("hashmap"+gson.toJson(map));// response.setHeader("Access-Control-Allow-Origin", "*"); //解决跨域问题 一般不推荐使用 现在没找到好的解决方法response.getWriter().write(gson.toJson(map));}

后台返回格式一定是

{"link": "图片的绝对地址"}

即:

如此,就可以把富文本编辑器中的图片上传到指定的服务器上了

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。