java实现文件上传oss服务
首先是pom.xml引入对应的jar包
<!-- OSS文件上传 start -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<!--<version>1.2</version> -->
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!-- 支付宝授权认证 -->
<dependency>
<groupId>com.alipay.user.info.auth</groupId>
<artifactId>user-info-auth</artifactId>
<version>20161102120103</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>oss</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
<!-- OSS文件上传 end -->
文件上传工具类
package com.asphalt.common.upload;
import java.io.ByteArrayInputStream;
import java.io.File;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.alibaba.druid.util.StringUtils;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.asphalt.common.LoggerProxy;
/**
* 文件上传工具类
*
* @author issuser
* @date 2016-10-22
*/
@Component
public class FileUploadUtils {
private static Logger logger=LoggerProxy.ProxyLogger(FileUploadUtils.class);
/* private static String ossUrl = PropertiesUtil.GetValueByKey("application.properties", "asphalt.oss_url");
private static String bucketName = PropertiesUtil.GetValueByKey("application.properties", "asphalt.bucket_name");
private static String accessKeyId = PropertiesUtil.GetValueByKey("application.properties", "asphalt.access_key_id");
private static String accessKeySecret = PropertiesUtil.GetValueByKey("application.properties", "asphalt.access_key_secret");*/
@Value("${asphalt.oss_url}")
private String ossUrl;
@Value("${asphalt.bucket_name}")
private String bucketName;
@Value("${asphalt.access_key_id}")
private String accessKeyId;
@Value("${asphalt.access_key_secret}")
private String accessKeySecret;
//Constructs a client instance with your account for accessing OSS
private static OSSClient client = null;
public OSSClient getInstance()
{
if(null == client)
{
client = new OSSClient(ossUrl, accessKeyId, accessKeySecret);
}
return client;
}
/**
* 文件上传
* @param uploadFile
* @return
*/
public boolean upload(File uploadFile) {
try {
if(uploadFile =&