java实现文件上传oss服务

本文介绍了如何使用Java进行OSS文件上传,包括在pom.xml中添加依赖,创建文件上传工具类,文件上传的封装方法,以及MD5校验的实现。
摘要由CSDN通过智能技术生成

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 =&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT界的一只菜鸟

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值