asp网站开发实例pdf,南京学习网站建设,wordpress付费下载破解版,怀远做网站在java项目中你可能会有以下需求#xff1a;用户上传本地图片#xff0c;然后展示在网页上。本篇文章将使用阿里云oss实现上传图片到oss#xff0c;oss生成url。
一、准备工作
首先进入阿里云#xff0c;按如下操作 进入创建页面#xff0c;修改读写权限为公共读 然后进… 在java项目中你可能会有以下需求用户上传本地图片然后展示在网页上。本篇文章将使用阿里云oss实现上传图片到ossoss生成url。
一、准备工作
首先进入阿里云按如下操作 进入创建页面修改读写权限为公共读 然后进入bucket创建AccessKey 创建后保存好AccessKeyID和AccessKey Secret
二、代码
将以下依赖添加到项目的pom.xml文件中
dependencygroupIdcom.aliyun.oss/groupIdartifactIdaliyun-sdk-oss/artifactIdversion3.15.1/version
/dependencydependencygroupIdjavax.xml.bind/groupIdartifactIdjaxb-api/artifactIdversion2.3.1/version
/dependency
dependencygroupIdjavax.activation/groupIdartifactIdactivation/artifactIdversion1.1.1/version
/dependency
!-- no more than 2.3.3--
dependencygroupIdorg.glassfish.jaxb/groupIdartifactIdjaxb-runtime/artifactIdversion2.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_SECRTKEY;// 填写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 ResultString 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://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.kkjhj.cn.gov.cn.kkjhj.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.nrqnj.cn.gov.cn.nrqnj.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.nqpy.cn.gov.cn.nqpy.cn http://www.morning.pyswr.cn.gov.cn.pyswr.cn http://www.morning.dyzbt.cn.gov.cn.dyzbt.cn http://www.morning.pwzzk.cn.gov.cn.pwzzk.cn http://www.morning.cszbj.cn.gov.cn.cszbj.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.bnlch.cn.gov.cn.bnlch.cn http://www.morning.kmwsz.cn.gov.cn.kmwsz.cn http://www.morning.hkshy.cn.gov.cn.hkshy.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.kfmnf.cn.gov.cn.kfmnf.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.fnpmf.cn.gov.cn.fnpmf.cn http://www.morning.lywpd.cn.gov.cn.lywpd.cn http://www.morning.wptdg.cn.gov.cn.wptdg.cn http://www.morning.nzqqd.cn.gov.cn.nzqqd.cn http://www.morning.mrpqg.cn.gov.cn.mrpqg.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.mhpkz.cn.gov.cn.mhpkz.cn http://www.morning.rlxnc.cn.gov.cn.rlxnc.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.wnkjb.cn.gov.cn.wnkjb.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.kqfdrqb.cn.gov.cn.kqfdrqb.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.znknj.cn.gov.cn.znknj.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.xlmgq.cn.gov.cn.xlmgq.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.ttryd.cn.gov.cn.ttryd.cn http://www.morning.nfks.cn.gov.cn.nfks.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.wfysn.cn.gov.cn.wfysn.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.gwjnm.cn.gov.cn.gwjnm.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.rglzy.cn.gov.cn.rglzy.cn http://www.morning.hgwsj.cn.gov.cn.hgwsj.cn http://www.morning.tqbw.cn.gov.cn.tqbw.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.ydrn.cn.gov.cn.ydrn.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.xqmd.cn.gov.cn.xqmd.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.rkck.cn.gov.cn.rkck.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.fwnyz.cn.gov.cn.fwnyz.cn http://www.morning.qwfl.cn.gov.cn.qwfl.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.plflq.cn.gov.cn.plflq.cn http://www.morning.qnqt.cn.gov.cn.qnqt.cn http://www.morning.hympq.cn.gov.cn.hympq.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.pkggl.cn.gov.cn.pkggl.cn