本人菜鸟一枚,最近公司有需求要用到富文本编辑器,我选择的是百度的ueditor富文本编辑器,闲话不多说,进入正题:
一:ueditor的下载及安装以及OSS的下载及引入我就不详细说了,这里说下要注意的几点:   
     1,ueditor下载地址http://ueditor.baidu.com/website/download.html,记得下载的是开发版-完整源码版
     2,oss-Java-sdk下载地址:https://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/internal/oss/0.0.4/assets/sdk/aliyun_java_sdk_20160510.zip?spm=5176.doc32009.2.1.0lQMOb&file=aliyun_java_sdk_20160510.zip
     3,至于ueditor安装及初始化方法,自行百度。OSS引入包放如项目lib文件夹即可开始使用

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

[java] view plain copy

 
  1. /**
  2. * 上传到阿里云:xhj
  3. *
  4. *
  5. */
  6. import java.io.FileNotFoundException;
  7. import java.io.InputStream;
  8. import com.aliyun.oss.OSSClient;
  9. public class UploadOSSUtil {
  10. public UploadOSSUtil(){}
  11. public static void uploadImgAliyun(InputStream inputStream ,String fileName)
  12. throws FileNotFoundException{
  13. String accesskeyId = "***你的阿里云accesskeyId***" ;
  14. String accessKeySecret = "***你的阿里云accessKeySecret***" ;
  15. String endpoint = "http://oss-cn-shenzhen.aliyuncs.com" ;
  16. String bucketName = "***你的bucketName***" ;
  17. OSSClient client = new OSSClient(endpoint,accesskeyId,accessKeySecret);
  18. //此处"xxxx/yyyy/"+fileName,表示上传至阿里云中xxxx文件夹下的yyyy文件夹中,请修改为自己的路径即可
  19. client.putObject(bucketName, "xxxx/yyyy/"+fileName, inputStream);
  20. client.shutdown();
  21. }
  22. }

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

[java] view plain copy

 
  1. public static final State save(HttpServletRequest request,
  2. Map<String, Object> conf) {
  3. FileItemStream fileStream = null;
  4. boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null;
  5. if (!ServletFileUpload.isMultipartContent(request)) {
  6. return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
  7. }
  8. ServletFileUpload upload = new ServletFileUpload(
  9. new DiskFileItemFactory());
  10. if ( isAjaxUpload ) {
  11. upload.setHeaderEncoding( "UTF-8" );
  12. }
  13. try {
  14. FileItemIterator iterator = upload.getItemIterator(request);
  15. while (iterator.hasNext()) {
  16. fileStream = iterator.next();
  17. if (!fileStream.isFormField())
  18. break;
  19. fileStream = null;
  20. }
  21. if (fileStream == null) {
  22. return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
  23. }
  24. String savePath = (String) conf.get("savePath");
  25. String originFileName = fileStream.getName();
  26. String suffix = FileType.getSuffixByFilename(originFileName);
  27. originFileName = originFileName.substring(0,
  28. originFileName.length() - suffix.length());
  29. savePath = savePath + suffix;
  30. long maxSize = ((Long) conf.get("maxSize")).longValue();
  31. if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
  32. return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
  33. }
  34. savePath = PathFormat.parse(savePath, originFileName);
  35. String physicalPath = (String) conf.get("rootPath") + savePath;
  36. InputStream is = fileStream.openStream();
  37. /**
  38. * 上传到阿里云:xhj添加
  39. */
  40. //*******************开始***********************
  41. String fileName = new StringBuffer().append(new Date().getTime()).append(fileStream.getName().substring(fileStream.getName().indexOf("."))).toString();
  42. State storageState = null;
  43. try {
  44. new UploadOSSUtil();
  45. UploadOSSUtil.uploadImgAliyun(is,fileName);
  46. storageState = StorageManager.saveFileByInputStream(is,
  47. physicalPath, maxSize);
  48. storageState.putInfo("state", "SUCCESS");// UEDITOR的规则:不为SUCCESS则显示state的内容
  49. //注意:下面的url是返回到前端访问文件的路径,请自行修改
  50. storageState.putInfo("url","http://XXXXXX.oss-cn-shenzhen.aliyuncs.com/images/companyNewsImages/" + fileName);
  51. storageState.putInfo("title", fileName);
  52. storageState.putInfo("original", fileName);
  53. } catch (Exception e) {
  54. // TODO: handle exception
  55. System.out.println(e.getMessage());
  56. storageState.putInfo("state", "文件上传失败!");
  57. storageState.putInfo("url","");
  58. storageState.putInfo("title", "");
  59. storageState.putInfo("original", "");
  60. //System.out.println("文件 "+fileName+" 上传失败!");
  61. }
  62. //********************结束**********************
  63. is.close();
  64. /*if (storageState.isSuccess()) {
  65. storageState.putInfo("url", PathFormat.format(savePath));
  66. storageState.putInfo("type", suffix);
  67. storageState.putInfo("original", originFileName + suffix);
  68. }*/
  69. //System.out.println("storageState="+storageState);
  70. return storageState;
  71. } catch (FileUploadException e) {
  72. return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
  73. } catch (IOException e) {
  74. }
  75. return new BaseState(false, AppInfo.IO_ERROR);
  76. }

如有疑问,欢迎提问。

最新文章

  1. oracle 11.2.0.4单实例文件系统安装与补丁
  2. CocoaPods 安装 使用
  3. [Head First设计模式]云南米线馆中的设计模式——模版方法模式
  4. java读取xml文件
  5. 禁随意改root密码
  6. 大话string
  7. easyui跨iframe属性datagrid
  8. zookeeper 伪集群模式
  9. QT 中使用 c++ 的指针
  10. Linux编程之epoll
  11. Azkaban使用简单笔记
  12. 使用 RAII 完成线程等待
  13. OPC转发阿里云alink工具
  14. Javascript高级编程学习笔记(78)—— 表单(6)HTML约束验证API
  15. emoji错误:ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value:
  16. 【OCR技术系列之七】端到端不定长文字识别CRNN算法详解
  17. mysql原理~undo
  18. K8S学习笔记之二进制的方式创建一个Kubernetes集群
  19. js window.open隐藏参数提交
  20. Javac语法糖之内部类

热门文章

  1. &quot;车羊门问题&quot;作业
  2. log4j-日志记录小结
  3. GitHub学习途径
  4. Linux常用的工具软件安装
  5. 用HTML做登录网页
  6. Effective Java --使类和成员的可访问性最小化
  7. 中标麒麟(linux)下Qt调用python数据转换
  8. shell awk处理过滤100万条数据
  9. @RequestParam与@PathVariable
  10. 第52章:Java操作MongoDB-[Mongo-Java-3.x]