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


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

     

           本人菜鸟一枚,最近公司有需求要用到富文本编辑器,我选择的是百度的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.   
    10. public class UploadOSSUtil {  
    11.  public UploadOSSUtil(){}  
    12.    
    13.  public static void uploadImgAliyun(InputStream inputStream ,String fileName)  
    14.  throws FileNotFoundException{  
    15.   String accesskeyId = "***你的阿里云accesskeyId***" ;  
    16.   String accessKeySecret = "***你的阿里云accessKeySecret***" ;  
    17.   String endpoint = "http://oss-cn-shenzhen.aliyuncs.com" ;  
    18.   String bucketName = "***你的bucketName***" ;  
    19.   OSSClient client = new OSSClient(endpoint,accesskeyId,accessKeySecret);  
    20.   //此处"xxxx/yyyy/"+fileName,表示上传至阿里云中xxxx文件夹下的yyyy文件夹中,请修改为自己的路径即可  
    21.   client.putObject(bucketName, "xxxx/yyyy/"+fileName, inputStream);  
    22.   client.shutdown();  
    23.  }  
    24. }  

     

     

     

     

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

     

     

     

     

     

    微信公众号【黄小斜】大厂程序员,互联网行业新知,终身学习践行者。关注后回复「Java」、「Python」、「C++」、「大数据」、「机器学习」、「算法」、「AI」、「Android」、「前端」、「iOS」、「考研」、「BAT」、「校招」、「笔试」、「面试」、「面经」、「计算机基础」、「LeetCode」 等关键字可以获取对应的免费学习资料。 

                         

     

    如有疑问,欢迎提问。

  • 相关阅读:
    javascript循环结构练习
    个人复习JavaScript重点(总结一)
    第十章汽车租凭系统
    员工执行
    第六章预习
    第五章使用Dictionary替换List<t>实现功能
    第5章体检套餐管理系统
    JAVA面试题
    AJAX
    SSM中的登陆验证码
  • 原文地址:https://www.cnblogs.com/xll1025/p/6502620.html
Copyright © 2020-2023  润新知