本人菜鸟一枚,最近公司有需求要用到富文本编辑器,我选择的是百度的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文件夹即可开始使用
二:安装完成后需要更改的地方:
1,打开包com.baidu.ueditor,upload,新建class文件:UploadOSSUtil.java内容如下
/**
* 上传到阿里云: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);
//此处"xxxx/yyyy/"+fileName,表示上传至阿里云中xxxx文件夹下的yyyy文件夹中,请修改为自己的路径即可
client.putObject(bucketName, "xxxx/yyyy/"+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");// UEDITOR的规则:不为SUCCESS则显示state的内容
//注意:下面的url是返回到前端访问文件的路径,请自行修改
storageState.putInfo("url","http://XXXXXX.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);
}
说明:
高手绕道,此实例只增加UploadOSSUtil.java和修改BinaryUploader.java即可,其他地方不做任何修改。
如有疑问,欢迎提问。