当前位置: 首页 > news >正文

秦皇岛网站搜索排名电子商务网站开发的流程

秦皇岛网站搜索排名,电子商务网站开发的流程,人人网站建设方案书,渭南市住房和城乡建设局官方网站实际工作中,需要用阿里云对象存储保存图片,并且在上传图片到阿里云对象存储服务器后,获取图片在阿里云对象存储服务器的URL地址,以便给 WEB 前端显示。 阿里云对象存储上传图片的工具类 package zhangchao;import com.aliyun.os…

实际工作中,需要用阿里云对象存储保存图片,并且在上传图片到阿里云对象存储服务器后,获取图片在阿里云对象存储服务器的URL地址,以便给 WEB 前端显示。

阿里云对象存储上传图片的工具类

package zhangchao;import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.UUID;/*** 阿里云对象存储工具类*/
public class OSSUtils {// OSS阿里云对象存储的AK和SKprivate static final String OSS_AK = "yourAK";private static final String OSS_SK = "yourSK";// OSS阿里云对象存储的Bucket名称private static final String OSS_BUCKET_NAME = "your-files";// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。private static final String OSS_ENDPOINT = "https://oss-cn-hangzhou.aliyuncs.com";/*** 上传图片并返回图片URL* @return 图片URL*/public static String uploadImage(String imageFilePath) {String result = null;// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(OSS_ENDPOINT, OSS_AK, OSS_SK);FileInputStream fis = null;try {// 创建存储空间。fis = new FileInputStream(imageFilePath);String uuid = UUID.randomUUID().toString().replaceAll("-", "");ossClient.putObject(OSS_BUCKET_NAME, uuid, fis);// 设置URL过期时间为10年 3600l * 1000 * 24 * 365 * 10Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000L *24L * 365L * 10L);URL url = ossClient.generatePresignedUrl(OSS_BUCKET_NAME, uuid, expiration);if (url != null) {result = url.toString();}} catch (OSSException oe) {StringBuilder sb = new StringBuilder();sb.append("Caught an OSSException, which means your request made it to OSS, ").append("but was rejected with an error response for some reason.");sb.append("Error Message:").append(oe.getErrorMessage());sb.append("Error Code:").append(oe.getErrorCode());sb.append("Request ID:").append(oe.getRequestId());sb.append("Host ID:").append(oe.getHostId());System.out.println(sb.toString());} catch (ClientException ce) {StringBuilder sb = new StringBuilder();sb.append("Caught an ClientException, which means the client encountered ").append("a serious internal problem while trying to communicate with OSS, ").append("such as not being able to access the network.");sb.append("Error Message:" + ce.getMessage());System.out.println(sb.toString());ce.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} finally {try {if (null != fis) {fis.close();}} catch (IOException e) {e.printStackTrace();}if (ossClient != null) {ossClient.shutdown();}}return result;}
}

调用的 main 方法:

package zhangchao;public class Main {public static void main(String[] args) {String imgUrl = OSSUtils.uploadImage("E:\\ws\\zc\\Java8OSSImg\\src\\main\\resources\\q1.jpg");System.out.println(imgUrl);}
}

运行结果,这里显示了返回的图片 URL 地址:

https://your-files.oss-cn-hangzhou.aliyuncs.com/abc123?Expires=2021599742&OSSAccessKeyId=youAK&Signature=xxxxxx

这个地址没办法用浏览器直接看,需要写个HTML来测试一下。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><img src="https://your-files.oss-cn-hangzhou.aliyuncs.com/abc123?Expires=2021599742&OSSAccessKeyId=youAK&Signature=xxxxxx">
</body>
</html>
http://www.15wanjia.com/news/165506.html

相关文章:

  • 网站欢迎页模板建设项目验收公示网站
  • 网站建设所用的工具搭建网站属于什么专业
  • jianshe导航网站外链百科
  • 红河优才网站建设友情链接代码
  • 网站建设的功能模型wordpress和网盘结合
  • 私人做网站有什么用微信小程序开店需要多少钱
  • 网站关键词字数北京seo排名
  • 视频网站的嵌入代码是什么WordPress显示403
  • 航天基地规划建设局网站昆明建设局网站
  • 自适应网站建设优化建站网站开发时保证用户登陆的安全
  • 广东住房和建设局网站官网中山市建设局网站窗口电话
  • 怎么做的360网站打不开贵阳网站建设-中国互联
  • nas服务器 做网站营销推广工作内容
  • vs2010 iis 网站开发豪华网站建设方案
  • 建设银行山西招聘网站一些建筑设计网站
  • 好看怎么上传视频网站吗西宁网站制作多少钱
  • 网站所有二级目录济南网站建设推荐搜点网络NO1
  • 在什么网站做贸易好制作php网站
  • 网站建设的一般步骤包括哪些30天网站建设 视频教程
  • 用什么网站开发巴西客户网络网页制作
  • 如何给网站做轮播图网站策划师招聘
  • zencart 一个产品网站下单wordpress外部调用插件
  • 网站建设公司谁管东莞手机网站建设
  • 百度推广网站一年多少钱网络科技公司都是干嘛的
  • 邯郸建站公司wap网站 开发
  • 模板建站总公司网站搭建修改收费依据
  • jsp网站开发介绍后端开发培训机构
  • 城阳建网站友情链接
  • 网站改版方案明星个人网站设计模板
  • 刷单网站建设专业旅游网站制作