以下是我上传文件到OSS的部分代码:
try {
// 创建OSS客户端
client = new OSSClient(endpoint, keyId, key, conf);
// 设置过期时间
Date expires = new Date (new Date().getTime() + 1000 * second);
// 创建请求
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucketName, DestPathName);
generatePresignedUrlRequest.setBucketName(bucketName);
// HttpMethod为PUT
generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
generatePresignedUrlRequest.setExpiration(expires);
// ContentType必须设置, 否则签名失败
generatePresignedUrlRequest.setContentType(contentType);
// 生成签名的URL
URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
HashMap<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Content-Length", inputLength+"");
requestHeaders.put("Content-Type", contentType);
// 使用URL签名方式上传指定输入流
PutObjectResult result = client.putObject(url, inputStream, inputLength, requestHeaders);
// System.out.println(result.getETag());
return 0;
} catch(Exception e) {
logger.error("上传异常: " + e.getMessage());
} finally {
// if(null != client) {
// client.shutdown();
// }
}
如果完成操作之后使用shutdown方法, 会抛出线程异常, 如果对OSSClient不做任何处理, 每隔60秒就有关闭空闲连接的日志输出(new了2个OSSClient实例, 完成操作之后断点停住):
2015-09-09 15:56:50,517 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:56:50,517 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:57:50,517 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:57:50,517 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:58:50,519 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:58:50,519 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:59:50,520 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 15:59:50,520 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:00:50,521 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:00:50,521 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:01:50,521 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:01:50,522 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:02:50,522 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
2015-09-09 16:02:50,522 DEBUG [org.apache.http.impl.conn.PoolingClientConnectionManager] - <Closing connections idle longer than 60 SECONDS>
这样上传文件是否合适? 该如何回收连接?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。