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

4,此实例只新增UploadOSSUtil.java及修改BinaryUploader.java即可,其他地方不用做任何修改

二:安装完成后需要更改的地方:

1,打开包com.baidu.ueditor,upload,新建class文件:UploadOSSUtil.java内容如下

/**

* 上传到阿里云:xhj

*

*

*/

importjava.io.FileNotFoundException;

importjava.io.InputStream;

importcom.aliyun.oss.OSSClient;

publicclassUploadOSSUtil {

publicUploadOSSUtil(){}

publicstaticvoiduploadImgAliyun(InputStream inputStream ,String fileName)

throwsFileNotFoundException{

String accesskeyId = "***你的阿里云accesskeyId***";

String accessKeySecret = "***你的阿里云accessKeySecret***";

String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";

String bucketName = "***你的bucketName***";

OSSClient client = newOSSClient(endpoint,accesskeyId,accessKeySecret);

//此处"xxxx/yyyy/"+fileName,表示上传至阿里云中xxxx文件夹下的yyyy文件夹中,请修改为自己的路径即可

client.putObject(bucketName, "xxxx/yyyy/"+fileName, inputStream);

client.shutdown();

}

}

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

publicstaticfinalState save(HttpServletRequest request,

Map conf) {

FileItemStream fileStream = null;

booleanisAjaxUpload = request.getHeader("X_Requested_With") !=null;

if(!ServletFileUpload.isMultipartContent(request)) {

returnnewBaseState(false, AppInfo.NOT_MULTIPART_CONTENT);

}

ServletFileUpload upload = newServletFileUpload(

newDiskFileItemFactory());

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) {

returnnewBaseState(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;

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

if(!validType(suffix, (String[]) conf.get("allowFiles"))) {

returnnewBaseState(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 = newStringBuffer().append(newDate().getTime()).append(fileStream.getName().substring(fileStream.getName().indexOf("."))).toString();

State storageState = null;

try{

newUploadOSSUtil();

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);

returnstorageState;

} catch(FileUploadException e) {

returnnewBaseState(false, AppInfo.PARSE_REQUEST_ERROR);

} catch(IOException e) {

}

returnnewBaseState(false, AppInfo.IO_ERROR);

}

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

如有疑问,欢迎提问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值