UEditor自定义上传和读取文件路径

本文档介绍如何在UEditor中自定义文件上传和读取的路径,通过修改默认设置,实现对jsp页面中UEditor组件的路径控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.首先在jsp中覆盖UEidtor获取路径的方法 

UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
		    UE.Editor.prototype.getActionUrl = function(action) {

				if (action == 'uploadimage') {
					return WEBPATH+'/webCol/uploadImage';
				} else if (action == 'uploadvideo') {
					return '';
				} else {
					return this._bkGetActionUrl.call(this, action);
				}
			}

2.在springmvc配置文件中配置多文件上传

<!-- spring多文件上传 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding">
			<value>UTF-8</value>
		</property>
		<property name="maxUploadSize">
			<!-- 上传文件大小限制为50M,50*1024*1024 -->
			<value>52428800</value>
		</property>
			<!-- 内存大小限制为8M,8*1024*1024 -->
		<property name="maxInMemorySize">
			<value>8388608</value>
		</property>
		<property name="resolveLazily" value="true"/>  
	</bean>

3.在controller中定义上传和读取文件的方法

	/**
	 * ueditor上传图片重写
	 * 
	 * @param upfile
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadImage")
	@ResponseBody//这里upfile是config.json中图片提交的表单名称
    public Map<String,String> uploadImage(@RequestParam("upfile")  CommonsMultipartFile upfile,HttpServletRequest request) throws IOException{  
        //文件原名称
		String fileName=upfile.getOriginalFilename();
		//文件后缀名
		String type = fileName.substring(upfile.getOriginalFilename().lastIndexOf("."));
		String nowName=DateUtil.format(new Date(), "yyyyMMdd") + "_" + new Date().getTime() + type;
		if(!upfile.isEmpty()){ 

			//上传位置路径
			String path0 = "D:\\static\\ueditor\\jsp\\upload\\image\\"+nowName;
			//按照路径新建文件
			File newFile = new File(path0);
			//复制
			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", type);
         //文件路径
         map.put("url", "/webCol/"+nowName+"/getImage");
         //文件大小(字节数)
         map.put("size", upfile.getSize()+"");

        return map;

	}
	
	/**
	 * ueditor读取文件重写
	 */
	@RequestMapping("{imgName}/getImage")
	public void readImg(@PathVariable("imgName") String imgName,  HttpServletResponse response)  throws Exception {  
		//设置文件的返回类型
        response.setContentType("image/*");
        //文件路径(windows下是\\,linux下是//,都必须是绝对路径)
        String imgPath="D:\\static\\ueditor\\jsp\\upload\\image\\"+imgName;
        //java中用File类来表示一个文件
        File image = new File(imgPath);
        //测试这个文件路径是否存在(也就是这个文件是否存在)
        if (!image.exists()) {  
            return;  
        }  

		//FileUtils.readFileToByteArray(File file)把一个文件转换成字节数组返回
		response.getOutputStream().write(FileUtils.readFileToByteArray(image));  
		//java在使用流时,都会有一个缓冲区,按一种它认为比较高效的方法来发数据:
		//把要发的数据先放到缓冲区,缓冲区放满以后再一次性发过去,而不是分开一次一次地发.
		//而flush()表示强制将缓冲区中的数据发送出去,不必等到缓冲区满.
		response.getOutputStream().flush();  
		response.getOutputStream().close();  
	}  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值