一: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
以上两个地址都是相同的源代码,请自行下载。转载请注明出处,谢谢!