关于富文本编辑器ueditor(jsp版)上传文件到阿里云OSS的简单实例,适合新手


       本人菜鸟一枚,最近公司有需求要用到富文本编辑器,我选择的是百度的ueditor富文本编辑器,闲话不多说,进入正题:
一:ueditor的下载及安装以及OSS的下载及引入我就不详细说了,这里说下要注意的几点:  
     1,ueditor下载地址:点击打开链接 ,记得下载的是开发版 - 完整源码版
     2,oss -java-sdk下载地址:点击打开链接
     3,至于ueditor安装及初始化方法,自行百度.OSS引入包放如项目lib文件夹即可开始使用

     4,此实例只是增加UploadOSSUtil.java及修改BinaryUploader.java即可,其他地方不用做任何修改
二:安装完成后需要更改的地方:       
     1,打开包com.baidu.ueditor.upload,新建class文件: UploadOSSUtil.java内容如下

package com.baidu.ueditor.upload;


/**
 * 上传到阿里云:xhj
 * 
 * 
 */
import java.io.FileNotFoundException;
import java.io.InputStream; 
import com.aliyun.oss.OSSClient; 


public class UploadOSSUtil {
	public UploadOSSUtil(){}
	
	public static void uploadImgAliyun(InputStream inputStream ,String fileName)
	throws FileNotFoundException{
		String accesskeyId = "***你的阿里云accesskeyId***" ;  
		String accessKeySecret = "***你的阿里云accessKeySecret***" ;  
		String endpoint = "http://oss-cn-shenzhen.aliyuncs.com" ;  
		String bucketName = "***你的bucketName***" ;  
		OSSClient client = new OSSClient(endpoint,accesskeyId,accessKeySecret);  
		//此处"images/companyNewsImages/"+fileName,表示上传至阿里云中images文件夹下的companyNewsImages文件夹中,请修改为自己的路径即可  
		client.putObject(bucketName, "images/companyNewsImages/"+fileName, inputStream);  
		client.shutdown();
	}
}


修改同目录下的BinaryUploader.java的save()

public static final State save(HttpServletRequest request,
			Map<String, Object> conf) {
		FileItemStream fileStream = null;
		boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null;


		if (!ServletFileUpload.isMultipartContent(request)) {
			return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
		}


		ServletFileUpload upload = new ServletFileUpload(
				new DiskFileItemFactory());


        if ( isAjaxUpload ) {
            upload.setHeaderEncoding( "UTF-8" );
        }


		try {
			FileItemIterator iterator = upload.getItemIterator(request);


			while (iterator.hasNext()) {
				fileStream = iterator.next();


				if (!fileStream.isFormField())
					break;
				fileStream = null;
			}


			if (fileStream == null) {
				return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
			}


			String savePath = (String) conf.get("savePath");
			String originFileName = fileStream.getName();
			String suffix = FileType.getSuffixByFilename(originFileName);


			originFileName = originFileName.substring(0,
					originFileName.length() - suffix.length());
			savePath = savePath + suffix;


			long maxSize = ((Long) conf.get("maxSize")).longValue();


			if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
				return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
			}


			savePath = PathFormat.parse(savePath, originFileName);


			String physicalPath = (String) conf.get("rootPath") + savePath;
			
			InputStream is = fileStream.openStream();
			
			/**
			 * 上传到阿里云:xhj添加
			 */
			//*******************开始***********************
			String fileName = new StringBuffer().append(new Date().getTime()).append(fileStream.getName().substring(fileStream.getName().indexOf("."))).toString();
			State storageState = null;
			try {
				
				new UploadOSSUtil();
				UploadOSSUtil.uploadImgAliyun(is,fileName);
				storageState = StorageManager.saveFileByInputStream(is,
						physicalPath, maxSize);
				storageState.putInfo("state", "SUCCESS");
				//url:返回前端的访问路径,请根据自己实际情况填写
				storageState.putInfo("url","http://kbs-test.oss-cn-shenzhen.aliyuncs.com/images/companyNewsImages/" + fileName);
				storageState.putInfo("title", fileName);
				storageState.putInfo("original", fileName);
			} catch (Exception e) {
				// TODO: handle exception
				System.out.println(e.getMessage());
				storageState.putInfo("state", "文件上传失败!");
				storageState.putInfo("url","");
				storageState.putInfo("title", "");
				storageState.putInfo("original", "");
	            //System.out.println("文件 "+fileName+" 上传失败!");
			}
			
			
			
			//********************结束**********************
			
			is.close();
			/*if (storageState.isSuccess()) {
				storageState.putInfo("url", PathFormat.format(savePath));
				storageState.putInfo("type", suffix);
				storageState.putInfo("original", originFileName + suffix);
			}*/
			//System.out.println("storageState="+storageState);
			return storageState;
		} catch (FileUploadException e) {
			return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
		} catch (IOException e) {
		}
		return new BaseState(false, AppInfo.IO_ERROR);
	}






如有疑问,欢迎提问。

PS:抽点时间做了个Demo,亲测可用

1:链接:https://pan.baidu.com/s/16C_ww0hFJPLkYJoBU8iyAQ      密码:eq5u

2:https://download.csdn.net/download/u013189988/10450304


以上两个地址都是相同的源代码,请自行下载。转载请注明出处,谢谢!


评论 14 您还未登录,请先 登录 后发表或查看评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页

打赏作者

甜酒

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值