阿里云OSS使用

Maven依赖

<dependency>  
    <groupId>com.aliyun.oss</groupId>  
    <artifactId>aliyun-sdk-oss</artifactId>  
    <version>3.10.2</version>  
</dependency>

文件上传

OSS ossClient = new OSSClientBuilder().build(endpoint, assessKey, secretKey);
// 将fileUrl指向的文件上传到OSS
URLConnection urlConnection = new URL(fileUrl).openConnection();
urlConnection.setConnectTimeout(10000);  
urlConnection.setReadTimeout(10000);  
InputStream fileInputStream = urlConnection.getInputStream();  
ossClient.putObject(bucket, saveFilePath, fileInputStream);  
fileInputStream.close();  
ossClient.shutdown();

使用代理

OSS ossClient = new OSSClientBuilder().build(endpoint, assessKey, secretKey);
InetSocketAddress addr = new InetSocketAddress(proxyServer, proxyPort);  
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);  
URLConnection urlConnection = new URL(fileUrl).openConnection(proxy);  
urlConnection.setConnectTimeout(10000);  
urlConnection.setReadTimeout(10000);  
urlConnection.setRequestProperty("User-Agent", DoubucketHttpUtils.getRandomUserAgent());  
InputStream fileInputStream = urlConnection.getInputStream();  
ossClient.putObject(bucket, saveFilePath, fileInputStream);  
fileInputStream.close();  
ossClient.shutdown();