开发者社区> 问答> 正文

内外网问题

问题:本地测试用外网没问题,现在把项目部署到阿里云ECS 改用内网 出现com.aliyun.oss  - [Client]Unable to execute HTTP request: ConnectionTimeout
望大神指点一二



package com.hzero.app.utils;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

/**
* Description: 阿里云OSS 文件的存储工具
*  
* @author hujianjun
* @createtime 2017年4月10日 上午9:20:11
*/
public class FileUploadUtil {

    /**
     * @param inputStream
     *            文件的输入流
     * @param fileKey(数据库文件path
     *            url ) 文件储存地址
     */

    static Logger logger = Logger.getLogger(FileUploadUtil.class);

    // endpoint是访问OSS的域名
     //private static final String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";//外网域名
     private static final String endpoint = "http://oss-cn-shenzhen-internal.aliyuncs.com";// 内网域名

    // outNet是访问bucket的域名
     //private static final String outNet = "http://hzero.oss-cn-shenzhen.aliyuncs.com";//外网域名
     private static final String outNet = "http://hzero.oss-cn-shenzhen-internal.aliyuncs.com";// 内网域名

    // accessKeyId和accessKeySecret是OSS的访问密钥,您可以在控制台上创建和查看,
    private static String accessKeyId = "****";
    private static String accessKeySecret = "*****";

    // Bucket用来管理所存储Object的存储空间
    private static final String bucketName = "eyed";

    /**
     * 上传文件流
     *  
     * @param inputStream
     * @param fileKey
     */
    public static String inputStreamUpload(InputStream inputStream, String fileKey) {
        logger.info("Started");

        // 生成OSSClient,您可以指定一些参数,详见“SDK手册 > Java-SDK > 初始化”,
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/init.html?spm=5176.docoss/sdk/java-sdk/get-start
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        logger.info("生成OSSClient!");
        String resultStr = null;
        try {

            // 判断Bucket是否存在。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
            //链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
            createBucket(ossClient);
            logger.info("createBucket成功!");
            // 设置bucket的访问权限,public-read-write权限
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            logger.info("setBucketAcl成功!");
            PutObjectResult putObject = ossClient.putObject(bucketName, fileKey, inputStream);
            logger.info("putObject成功!");
            resultStr = putObject.getETag();
            logger.info(resultStr);
        } catch (OSSException oe) {
            oe.printStackTrace();
        } catch (ClientException ce) {
            ce.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ossClient.shutdown();
        }

        logger.info("Completed");
        return resultStr;
    }

    /**
     * 上传本地文件
     *  
     * @param file
     * @param fileKey
     */
    public static String fileUpload(File file, String fileKey) {
        logger.info("Started");

        // 生成OSSClient,您可以指定一些参数,详见“SDK手册 > Java-SDK > 初始化”,
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/init.html?spm=5176.docoss/sdk/java-sdk/get-start
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        String resultStr = null;
        try {

            // 判断Bucket是否存在,如果不存在则创建
            //链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
            createBucket(ossClient);

            // 设置bucket的访问权限,public-read-write权限
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);

            PutObjectResult putObject = ossClient.putObject(bucketName, fileKey, file);
            resultStr = putObject.getETag();

        } catch (OSSException oe) {
            oe.printStackTrace();
        } catch (ClientException ce) {
            ce.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ossClient.shutdown();

        }

        logger.info("Completed");
        return resultStr;

    }

    /**
     * 单文件删除
     *  
     * @param fileKey
     *            要删除的Object路径
     */
    public static void delOne(String fileKey) {
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            // 删除Object
            ossClient.deleteObject(bucketName, fileKey);
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            // 关闭client
            ossClient.shutdown();
        }

    }

    /**
     * 批量删除的方法
     *  
     * @param fileKeys
     *            批量删除的地址集合
     */
    public static void delMany(List<String> fileKeys) {
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            // 删除Object
            DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
            // deleteObjectsRequest设置详情https://help.aliyun.com/document_detail/32015.html
            deleteObjectsRequest.setKeys(fileKeys);
            ossClient.deleteObjects(deleteObjectsRequest);
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            // 关闭client
            ossClient.shutdown();
        }

    }

    /**
     * 下载
     */
    public void download(HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
        String yourKey = request.getParameter("filePath");
        // 下载到本地的路径(可以根据服务器这边需求修改)
        String yourLocalFile = yourKey;
        // 截取文件所在的目录
        String filePath = yourLocalFile.substring(0, yourLocalFile.lastIndexOf("/"));
        // 如果目录不存在则创建
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }

        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 下载object到文件
        ossClient.getObject(new GetObjectRequest(bucketName, yourKey), new File(yourLocalFile));
        ossClient.shutdown();

    }

    /**
     * 此方法用来判断Bucket是否存在如果不存在则创建
     *  
     * @param ossClient
     */
    public static void createBucket(OSSClient ossClient) {
        if (ossClient.doesBucketExist(bucketName)) {
            System.out.println("您已经创建Bucket:" + bucketName + "。");
        } else {
            System.out.println("您的Bucket不存在,创建Bucket:" + bucketName + "。");
            // 创建Bucket。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
            //链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
            ossClient.createBucket(bucketName);
        }
    }

    /**
     * 此方法用来获取外网域名
     *  
     * @return 外网域名
     */
    public static String getoutNet() {
        return outNet;
    }

    /**
     * 此方法用来增加域名
     *  
     * @param oldfileKey
     * @return 增加域名后的fileKey(存入数据库)
     */
    public static String addOutNet(String oldfileKey) {
        String fileKey = outNet + File.separator + oldfileKey;
        return fileKey;
    }

    /**
     * 此方法用来删除域名
     *  
     * @param oldfileKey
     *            从数据库表检索到的带域名的iamgePath
     * @return 删除域名之后的fileKey
     */
    public static String deletOutNet(String oldfileKey) {
        String fileKey = oldfileKey.replace(outNet + File.separator, "");
        return fileKey;
    }
}

展开
收起
hzero 2017-04-10 16:52:00 3452 0
1 条回答
写回答
取消 提交回答
  • 把HTTP去掉试一下?
    2017-04-11 09:55:12
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载