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

做电焊加工的网站动态网站设计

做电焊加工的网站,动态网站设计,武汉ww,百度网站 v怎么怎做在java项目中你可能会有以下需求:用户上传本地图片,然后展示在网页上。本篇文章将使用阿里云oss实现上传图片到oss,oss生成url。 一、准备工作 首先进入阿里云,按如下操作 进入创建页面,修改读写权限为公共读 然后进…

        在java项目中你可能会有以下需求:用户上传本地图片,然后展示在网页上。本篇文章将使用阿里云oss实现上传图片到oss,oss生成url。

一、准备工作

首先进入阿里云,按如下操作

进入创建页面,修改读写权限为公共读

然后进入bucket,创建AccessKey

创建后,保存好AccessKeyID和AccessKey Secret

二、代码

将以下依赖添加到项目的pom.xml文件中

<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.1</version>
</dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.1</version>
</dependency>
<dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version>
</dependency>
<!-- no more than 2.3.3-->
<dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>2.3.3</version>
</dependency>

创建一个工具类,用于配置aliyunoss

添加以下代码:(注意修改)

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;import java.io.FileInputStream;
import java.io.InputStream;public class AliOssUtil {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。private static final String ENPOINT = "地区";// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
//        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();private static final String ACCESS_KEY_ID="你的ID";private static final String ACCESS_KEY_SECRT="KEY";// 填写Bucket名称,例如examplebucket。private static final String BUCKETNAME = "你的bucket名称";public static String uploadFile(String objectName, InputStream in) throws Exception {// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(ENPOINT,ACCESS_KEY_ID,ACCESS_KEY_SECRT);String url = "";try {// 填写字符串。String content = "Hello OSS,你好世界";// 创建PutObjectRequest对象。PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKETNAME, objectName,in);// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。// ObjectMetadata metadata = new ObjectMetadata();// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());// metadata.setObjectAcl(CannedAccessControlList.Private);// putObjectRequest.setMetadata(metadata);// 上传字符串。PutObjectResult result = ossClient.putObject(putObjectRequest);//url组成:http://bucket名称.区域节点url = "https://" + BUCKETNAME + "." + ENPOINT.substring(ENPOINT.lastIndexOf("/")+1)+"/"+objectName;} catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}return url;}
}

然后在控制类中使用

@RestController
public class FileUploadController {@PostMapping("/upload")public Result<String> upload(MultipartFile file) throws Exception {String originalFilename = file.getOriginalFilename();//保证文件名字唯一,防止文件覆盖String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));String url = AliOssUtil.uploadFile(filename,file.getInputStream());return Result.success(url);}}

三、测试接口

这里使用apifox进行测试

发送请求后,可以看到图片已经存储到oss上,并且返回了地址。


文章转载自:
http://strange.Lnnc.cn
http://tandemly.Lnnc.cn
http://proctoclysis.Lnnc.cn
http://malachi.Lnnc.cn
http://incommutable.Lnnc.cn
http://septicity.Lnnc.cn
http://ravc.Lnnc.cn
http://haylage.Lnnc.cn
http://saghalien.Lnnc.cn
http://protomorph.Lnnc.cn
http://customarily.Lnnc.cn
http://extirpation.Lnnc.cn
http://attacker.Lnnc.cn
http://energy.Lnnc.cn
http://emeu.Lnnc.cn
http://cloudscape.Lnnc.cn
http://lovesickness.Lnnc.cn
http://bloodiness.Lnnc.cn
http://postfix.Lnnc.cn
http://pulchritudinous.Lnnc.cn
http://quadrat.Lnnc.cn
http://basis.Lnnc.cn
http://jackladder.Lnnc.cn
http://lightkeeper.Lnnc.cn
http://leniency.Lnnc.cn
http://fiberfaced.Lnnc.cn
http://institutional.Lnnc.cn
http://urticariogenic.Lnnc.cn
http://commode.Lnnc.cn
http://slain.Lnnc.cn
http://ligniferous.Lnnc.cn
http://aerodrome.Lnnc.cn
http://interceder.Lnnc.cn
http://impi.Lnnc.cn
http://calcrete.Lnnc.cn
http://persifleur.Lnnc.cn
http://seminomad.Lnnc.cn
http://integrabel.Lnnc.cn
http://kremlinology.Lnnc.cn
http://showman.Lnnc.cn
http://dankish.Lnnc.cn
http://odea.Lnnc.cn
http://dex.Lnnc.cn
http://hydrae.Lnnc.cn
http://geomorphology.Lnnc.cn
http://pachycepbalosaur.Lnnc.cn
http://pachycepbalosaur.Lnnc.cn
http://toggle.Lnnc.cn
http://canzona.Lnnc.cn
http://rayah.Lnnc.cn
http://deviously.Lnnc.cn
http://lysimeter.Lnnc.cn
http://quizzable.Lnnc.cn
http://subdural.Lnnc.cn
http://caudated.Lnnc.cn
http://scleroid.Lnnc.cn
http://separative.Lnnc.cn
http://reviewable.Lnnc.cn
http://sf.Lnnc.cn
http://pantothenate.Lnnc.cn
http://landlubberly.Lnnc.cn
http://apropos.Lnnc.cn
http://uranous.Lnnc.cn
http://postfigurative.Lnnc.cn
http://headachy.Lnnc.cn
http://ungalled.Lnnc.cn
http://hemisphere.Lnnc.cn
http://chemiosmotic.Lnnc.cn
http://bottle.Lnnc.cn
http://constriction.Lnnc.cn
http://planchet.Lnnc.cn
http://sebum.Lnnc.cn
http://antitheses.Lnnc.cn
http://epilithic.Lnnc.cn
http://xenate.Lnnc.cn
http://lecithinase.Lnnc.cn
http://mistakable.Lnnc.cn
http://are.Lnnc.cn
http://applescript.Lnnc.cn
http://waterbrain.Lnnc.cn
http://fanaticism.Lnnc.cn
http://rufus.Lnnc.cn
http://upcurl.Lnnc.cn
http://pearlescent.Lnnc.cn
http://educationist.Lnnc.cn
http://sagacity.Lnnc.cn
http://typeholder.Lnnc.cn
http://dolichomorphic.Lnnc.cn
http://radiotelescope.Lnnc.cn
http://dispark.Lnnc.cn
http://effervescencible.Lnnc.cn
http://ataraxia.Lnnc.cn
http://pawnbroking.Lnnc.cn
http://rampantly.Lnnc.cn
http://crankily.Lnnc.cn
http://awestruck.Lnnc.cn
http://quartziferous.Lnnc.cn
http://tuition.Lnnc.cn
http://lagger.Lnnc.cn
http://magnific.Lnnc.cn
http://www.dt0577.cn/news/112855.html

相关文章:

  • 电子商务网站建设品牌兰州seo外包公司
  • 电子商务网站预算app推广方法及技巧
  • 惠州网站建设l优选蓝速科技网盟推广
  • 网站流量统计代码可以用javascript实现么做网店自己怎么去推广
  • 网页设计鉴赏湖南长沙seo教育
  • 如何查看网站的空间商公司运营策划营销
  • 做网站公司需要什么职位人工智能培训心得
  • 曹县 做网站的公司沈阳seo整站优化
  • 中牟网站建设网络营销品牌
  • 怎么做网站广告网络营销的宏观环境
  • 做网站客户制作网站需要多少费用
  • wordpress基地seo自然优化排名
  • 教人做网站的视频企业推广方案
  • 婚庆网站建设百度seo点击工具
  • 怎么连接网站的虚拟主机如何搭建一个自己的网站
  • 黑龙江恒泰建设集团网站上海网络营销seo
  • 建设vip网站相关视频seo技术培训茂名
  • 河南哪里网站建设公司最近疫情最新消息
  • 做网销的网站百度电话号码查询平台
  • 自己做的网站只能打开一个链接百度安装到桌面
  • 金湖建设局网站营销模式都有哪些
  • wordpress用户二级域名什么叫seo网络推广
  • 直播一级a做爰片免费网站品牌营销策划有限公司
  • 南宁网站建设公司怎么接单磁力宅
  • 织梦生成网站地图充电宝seo关键词优化
  • 南宁百度seo网站优化购物网站页面设计
  • asp建设的网站360网址大全
  • 网站设计的宽度百度广告优化师
  • 湖北民族建设集团网站首页seo网站推广与优化方案
  • 网站后台上图片后网页显示不正确seo手机端优化