相关配置
在阿里云控制台生成访问密钥(AccessKey)
创建OSS bucket。
阿里云提供了Endpoint(是阿里云自己的域名)。数据库中存储的文件地址如果包含阿里云的域名,将来如果不使用阿里云(使用其他的云服务,或者是自己搭建图片服务器),那么数据库中的地址全部要修改(这里面包括富文本内容,修改富文本内容中的图片地址复杂程度可想而知)。
域名解析选择CNAME类型解析即可
如果域名是使用同一个账号管理的,直接在oss控制面板绑定域名
实现代码
添加OSS服务SDK依赖
1 2 3 4 5 6
| <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>2.8.3</version> </dependency>
|
在properties文件中配置下面的相关信息
1 2 3 4 5 6 7 8 9 10 11
| aliyun.access.key.id=XXX aliyun.access.key.secret=XXXX
aliyun.oss.endpoint=http://oss-cn-shenzhen.aliyuncs.com
aliyun.oss.bucket.name=XXX
aliyun.oss.img.domain=XXX.XXX.com
|
在Spring的xml中配置OSSClient
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:aliyun.properties"/>
<context:component-scan base-package="com.cni.aliyun"/>
<bean id="ossClient" class="com.aliyun.oss.OSSClient" destroy-method="shutdown"> <constructor-arg name="endpoint" value="${aliyun.oss.endpoint}"/> <constructor-arg name="accessKeyId" value="${aliyun.access.key.id}"/> <constructor-arg name="secretAccessKey" value="${aliyun.access.key.secret}"/> </bean>
</beans>
|
上传文件的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| import com.aliyun.oss.OSS; import com.aliyun.oss.model.ObjectMetadata; import org.apache.http.HttpHost; import org.apache.http.client.utils.URIUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;
import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.util.Date;
@Component public class AliyunOss {
@Autowired private OSS ossClient;
@Value("${aliyun.oss.bucket.name}") private String bucketName;
@Value("${aliyun.oss.img.domain}") private String mappingDomain;
public String upload(InputStream is, String path, String contentType) throws URISyntaxException { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType(contentType); ossClient.putObject(bucketName, path, is, metadata); URL url = generateUrl(path); URI uri = url.toURI(); HttpHost httpHost = new HttpHost(mappingDomain); URI mappingURI = URIUtils.rewriteURI(uri, httpHost); return mappingURI.toString(); }
public URL generateUrl(String key) { final long duration = 1000L * 60L * 60L * 24L * 365L * 100L; long time = System.currentTimeMillis() + duration; Date expiration = new Date(time); return ossClient.generatePresignedUrl(bucketName, key, expiration); }
}
|
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:app-aliyun.xml") public class AliyunTest { @Autowired AliyunOss aliyunOss;
@Test public void testOss() throws FileNotFoundException, URISyntaxException { InputStream is = new FileInputStream("C:\\Users\\CNI\\Pictures\\5a55a640N8e90e084.jpg"); String key = "2018/1/12/img.jpg"; String url = aliyunOss.upload(is, key, "image/jpeg"); System.out.println(url); }
}
|
本作品采用 知识共享署名 4.0 国际许可协议 进行许可。
转载时请注明原文链接:https://blog.hufeifei.cn/2018/01/J2EE/Spring%E6%95%B4%E5%90%88%E9%98%BF%E9%87%8C%E4%BA%91OSS%E6%9C%8D%E5%8A%A1%E5%AE%9E%E7%8E%B0%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/