问题:本地测试用外网没问题,现在把项目部署到阿里云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;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。