package com.gx.obe.management.webservice;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;

public class OSSUtil {

	private static OSSUtil instance = null;

	private static String ossUrl = "http://gongxiangcunchu.oss-cn-beijing.aliyuncs.com/";
	private static String ossUrlIn = "http://gongxiangcunchu.oss-cn-beijing-internal.aliyuncs.com/";
	private static String accessKeyId = "LTAINp8eSoae7Z1j";
	private static String accessKeySecret = "GC1LXW0n1XFcZpPWUdmlU7udEliQlz";
	private static String endpoint = "http://oss-cn-beijing.aliyuncs.com";

	private static OSSClient client;

	public static OSSUtil getInstance() {
		if (instance == null) {
			instance = new OSSUtil();
			instance.init();
		}
		return instance;
	}

	public void init() {
		client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	}

	public String setData(File file, String filePath) {
		String str = "";
		try {
			// 创建上传Object的Metadata
			ObjectMetadata meta = new ObjectMetadata();
			InputStream content;
			content = new FileInputStream(file);
			// 必须设置ContentLength
			meta.setContentLength(file.length());
			// 上传Object.
			PutObjectResult result = client.putObject("gongxiangcunchu", filePath,content, meta);
			String kk = result.getETag();
			str = ossUrl + filePath;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	public URL getPutUrl(String key) {
		String bucketName = "gongxiangcunchu";
		// 设置URL过期时间为1小时
		Date expiration = new Date(new Date().getTime() + 3600 * 1000);
		// 生成URL
		URL url = client.generatePresignedUrl(bucketName, key, expiration);
		return url;
	}

    /**
     * 文件上传并返回文件路径
     * @param fileName
     * @return
     * @throws IOException 
     */
    public static String fileUpload(String filePath,String fileKey) throws IOException{
		File file = new File(filePath);
		String setData = OSSUtil.getInstance().setData(file,fileKey);
        return setData;
    }
    
    /**
     * 文件下载并返回文件路径
     * @param fileName
     * @return
     * @throws IOException 
     */
    public static URL fileDownload(String fileKey) throws IOException{
    	URL putUrl = OSSUtil.getInstance().getPutUrl(fileKey);
        return putUrl;
    }
    
    /**
     * @Description:url下载 
     * @author wangxiang
     * @param fileKey
     * @return
     * @throws IOException 
     */
    public static InputStream downloadFile(String fileKey) throws IOException{
		InputStream is = null;
		try {
			getInstance();
            OSSObject object = client.getObject(new GetObjectRequest("gongxiangcunchu", fileKey));
            is = object.getObjectContent();
        }  catch (Exception ce) {
        }
		
		return is;
	}
    
	public static void main(String[] args) throws IOException {
		String filePath = "C:\\1.doc";
		String fileKey = "1.doc";
		//测试上传
//		File file = new File(filePath);
//		String setData = OSSUtil.getInstance().setData(file,fileKey);
//		System.out.println(setData);
		//测试下载
//		URL putUrl = OSSUtil.getInstance().getPutUrl(fileKey);
//		System.out.println(putUrl);
//		URL fileDownload = fileDownload(fileKey);
		  //System.out.println(fileDownload);D:/upload/project/\2018-07\84d6389f06e34309a1699cb56e6ed597\createBulletinContentFile\95caea3b146b452085abf91c3d490310.pdf
		   
		  String fileUpload = fileUpload(filePath,fileKey);
		  URL url = fileDownload(fileKey);
		  String targetPath = "C:\\2.doc";
		  
		  
		  System.out.println("下载路径："+url);
			
	}
	
	
}
