Netty聊天之发送图片

代码示例

  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<String,Object> resultMap=new HashMap<String,Object>();
        Map<String,Object> fileMap=new HashMap<String,Object>();
        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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值