java基于阿里云OSS的访问私有云的localhost域名问题

来源:互联网 发布:住院电子病历软件 编辑:程序博客网 时间:2024/06/16 08:38

在使用阿里云的私有云访问时,需要更改服务器机器的host文件,host文件目录为:C:\Windows\System32\drivers\etc\hosts,如下是hosts的文件详情:

# Copyright (c) 1993-2009 Microsoft Corp.## This is a sample HOSTS file used by Microsoft TCP/IP for Windows.## This file contains the mappings of IP addresses to host names. Each# entry should be kept on an individual line. The IP address should# be placed in the first column followed by the corresponding host name.# The IP address and the host name should be separated by at least one# space.## Additionally, comments (such as these) may be inserted on individual# lines or following the machine name denoted by a '#' symbol.## For example:##      102.54.94.97     rhino.acme.com          # source server#       38.25.63.10     x.acme.com              # x client host# localhost name resolution is handled within DNS itself.#127.0.0.1       localhost#::1             localhost
--然后在最后加上私有云的内网IP和私有云的域名对饮关系:如

1.1.1.1   aa.oss.bb.aliyun-inc.com(其中aa是指bucket的名称,bb是私有云的具体申请的名称),这样就可以进行私有云的访问啦。

我这里用的是oss的2.0.6版本的sdk

具体方法--》上传文件:

/** * 上传文件 * @param client  OSSClient对象 * @param bucketName  Bucket名 * @param Objectkey  上传到OSS起的名 * @param filename  本地文件名(包括路径) * @throws OSSException * @throws ClientException * @throws FileNotFoundException */@Overridepublic void uploadFile(String bucketName, String objectKey, String fileName)throws OSSException, ClientException, FileNotFoundException {ClientConfiguration conf = new ClientConfiguration();OSSClient client = new OSSClient(OSS_ENDPOINT, ACCESS_ID, ACCESS_KEY);ObjectMetadata objectMeta = new ObjectMetadata();File file = new File(fileName);objectMeta.setContentLength(file.length());//判断上传类型,这里用的文件类型都是txt文档if(fileName.endsWith("txt")){objectMeta.setContentType("application/octet-stream");InputStream input = new FileInputStream(file);client.putObject(bucketName, objectKey, input, objectMeta);}}
具体方法--》下载文件:

/** * 下载文件 * @param client  OSSClient对象 * @param bucketName  Bucket名 * @param Objectkey  上传到OSS起的名 * @param filename 文件下载到本地保存的路径 * @throws OSSException * @throws ClientException */@Overridepublic void downloadFile(String bucketName, String objectKey, String fileName)throws OSSException, ClientException {OSSClient client = new OSSClient(OSS_ENDPOINT, ACCESS_ID, ACCESS_KEY);client.getObject(new GetObjectRequest(bucketName, objectKey), new File(fileName));}



0 0