分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

百度 UEditor--自定义上传文件路径及读取文件

发布时间:2023-09-06 01:14责任编辑:赖小花关键词:暂无标签

jsp中替换掉上传和预览图片的URL

注意第一句判断,注意要用项目的相对URL

 return ‘/sirdifoa/kentra/file/uploadImage.do‘;

if(UE.Editor.prototype._bkGetActionUrl == undefined)UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;    UE.Editor.prototype.getActionUrl = function(action) {    //判断路径   这里是config.json 中设置执行上传的action名称        if (action == ‘uploadimage‘) {            return ‘/sirdifoa/kentra/file/uploadImage.do‘;        } else if (action == ‘uploadvideo‘) {            return ‘‘;        } else {            return this._bkGetActionUrl.call(this, action);        }    }


自定义的上传、预览图片的方法。

@RequestMapping("/uploadImage")@ResponseBody // 这里upfile是config.json中图片提交的表单名称public Map<String, String> uploadImage(@RequestParam("upfile") CommonsMultipartFile upfile,HttpServletRequest request) throws IOException {// 文件原名称String fileName = upfile.getOriginalFilename();// 为了避免重复简单处理String nowName = new Date().getTime() + "_" + fileName;if (!upfile.isEmpty()) {String ueditorImagePath = PropertyUtil.readValue(Const.ueditorImagePath);SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");String formatDay = simpleDateFormat.format(new Date());// 上传位置路径String path0 = ueditorImagePath + "/" +formatDay+"/"+ nowName;// 按照路径新建文件java.io.File newFile = new java.io.File(path0);java.io.File newFile1 = new java.io.File(ueditorImagePath+ "/" +formatDay);if (!newFile1.exists()){boolean mkdir = newFile1.mkdirs();}// 复制FileCopyUtils.copy(upfile.getBytes(), newFile);}// 返回结果信息(UEditor需要)Map<String, String> map = new HashMap<String, String>();// 是否上传成功map.put("state", "SUCCESS");// 现在文件名称map.put("title", nowName);// 文件原名称map.put("original", fileName);// 文件类型 .+后缀名map.put("type", fileName.substring(upfile.getOriginalFilename().lastIndexOf(".")));// 文件路径map.put("url", "/kentra/file/getImage.do?imgName="+nowName);// 文件大小(字节数)map.put("size", upfile.getSize() + "");return map;}/** *  * 读取文件 *  */@RequestMapping("/getImage")public void readImg(String imgName, HttpServletResponse response)throws Exception {// 设置文件的返回类型response.setContentType("image/*");SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");String formatDay = simpleDateFormat.format(new Date());// 文件路径(windows下是\\,linux下是//,都必须是绝对路径)String imgPath = PropertyUtil.readValue(Const.ueditorImagePath) + "/" +formatDay+"/"+imgName;// java中用File类来表示一个文件java.io.File newFile = new java.io.File(imgPath);// 测试这个文件路径是否存在(也就是这个文件是否存在)if (!newFile.exists()) {return;}// FileUtils.readFileToByteArray(File file)把一个文件转换成字节数组返回response.getOutputStream().write(FileUtils.readFileToByteArray(newFile));// java在使用流时,都会有一个缓冲区,按一种它认为比较高效的方法来发数据:// 把要发的数据先放到缓冲区,缓冲区放满以后再一次性发过去,而不是分开一次一次地发.// 而flush()表示强制将缓冲区中的数据发送出去,不必等到缓冲区满.response.getOutputStream().flush();response.getOutputStream().close();}

config.json中加一个路径前缀:

 /* 上传图片配置项 */    "imageActionName": "uploadimage", /* 执行上传图片的action名称 */    "imageFieldName": "upfile", /* 提交的图片表单名称 */    "imageMaxSize": 20480000, /* 上传大小限制,单位B */    "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */    "imageCompressEnable": true, /* 是否压缩图片,默认是true */    "imageCompressBorder": 1600, /* 图片压缩最长边限制 */    "imageInsertAlign": "none", /* 插入的图片浮动方式 */    "imageUrlPrefix": "/sirdifoa", /* ==============图片访问路径前缀 ==================*/



本文出自 “JianBo” 博客,请务必保留此出处http://jianboli.blog.51cto.com/12075002/1969542

百度 UEditor--自定义上传文件路径及读取文件

原文地址:http://jianboli.blog.51cto.com/12075002/1969542

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved