博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Netty聊天之发送图片
阅读量:6865 次
发布时间:2019-06-26

本文共 1941 字,大约阅读时间需要 6 分钟。

hot3.png

  1. 效果图:

  1. 操作流程

这里是借助layUi的富文本框。

插入图片的时候有个上传的动作,后台保存到本地然后把项目的路径返回到前端

最后传输到后台netty的是图片路径

  1. 工具

    使用layui的富文本框

layedit.set({		                uploadImage: {		                     url: '${httpServletRequest.getContextPath()}/fileUpload/imageUpload' //接口url		                    ,type: 'post' //默认post		                }		            });

后端文件接收

public String ckeditorUpload(HttpServletRequest request,@RequestParam("file") MultipartFile file) throws Exception {		//获取跟目录		File path = new File(ResourceUtils.getURL("classpath:").getPath());		if(!path.exists()) path = new File("");		System.out.println("path:"+path.getAbsolutePath());		//如果上传目录为/static/images/upload/,则可以如下获取:		File upload = new File(path.getAbsolutePath(),"static/images/upload/");		if(!upload.exists()) upload.mkdirs();		System.out.println("upload url:"+upload.getAbsolutePath());				// 获取文件名        String fileName = file.getOriginalFilename();        // 获取文件的后缀名        String suffixName = fileName.substring(fileName.lastIndexOf("."));        //实际处理肯定是要加上一段唯一的字符串(如现在时间),这里简单加 cun       String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();        String newFileName = uuid + suffixName;        //使用架包 common-io实现图片上传        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(upload.getAbsolutePath()+File.separator + newFileName));        //实现图片回显,基本上是固定代码,只需改路劲即可        Map
resultMap=new HashMap
(); Map
fileMap=new HashMap
(); resultMap.put("code", "0"); resultMap.put("msg", "上传成功"); resultMap.put("data", fileMap); fileMap.put("src", request.getContextPath()+File.separator +"static/images/upload/"+ newFileName); fileMap.put("title", newFileName); ObjectMapper mapper = new ObjectMapper(); String result=mapper.writeValueAsString(resultMap); return result; }

转载于:https://my.oschina.net/findurl/blog/2874721

你可能感兴趣的文章
Django 的数据库查询
查看>>
gitlab备份与恢复
查看>>
运用mysql 中的source命令导入大的sql文件的解决方法
查看>>
CDH + phoenix+ zeppelin
查看>>
Cloudera与Hortonworks宣布合并创建全球领先的下一代数据平台
查看>>
JDBC Statement PreparedStatement CallableStatement
查看>>
Asynchronous Module Definition (AMD)
查看>>
javac java 命令行编译运行程序
查看>>
几个重要的Linux系统内核文件介绍
查看>>
jquery ajax “Uncaught TypeError: Illegal invocation”
查看>>
Android 农历和节气相关工具类(记录)
查看>>
php字符串处理函数大全
查看>>
redis应用场景
查看>>
页面加载自动定位到显示位置
查看>>
JTree实现文件树
查看>>
FMDB详解
查看>>
F12浏览器调试模式页面刷新network日志刷新消失的解决办法
查看>>
DiskGroup resource are not running on nodes. Database instance may not come up on these nodes
查看>>
nginx+keepalived实现集群配置
查看>>
maven常用命令整理
查看>>