资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java中怎么利用oss实现下载功能

这篇文章将为大家详细讲解有关java中怎么利用oss实现下载功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

成都创新互联公司专注于辉县企业网站建设,成都响应式网站建设,商城网站制作。辉县网站建设公司,为辉县等地区提供建站服务。全流程按需定制开发,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务

首先,导入依赖包

 com.aliyun.oss  aliyun-sdk-oss  3.6.0

参数类

public class OssParams {  public static final String endpoint = "oss-cn-hangzhou.aliyuncs.com";  public static final String accessKeyId = ""; public static final String accessKeySecret = "";  public static final String bucketName = "";  public static final String rootName = "";//根目录}

工具类

import com.aliyun.oss.ClientException;import com.aliyun.oss.OSS;import com.aliyun.oss.OSSClientBuilder;import com.aliyun.oss.OSSException;import com.aliyun.oss.model.AppendObjectRequest;import com.aliyun.oss.model.ObjectMetadata;import org.joda.time.DateTime;import org.slf4j.Logger;import org.slf4j.LoggerFactory; import java.io.ByteArrayInputStream;import java.net.URL;import java.util.Date;import java.util.concurrent.atomic.AtomicLong; import static OssParams.bucketName; /** * 先做单例对象调用,防止多线程并发bug * 后期加入线程锁控制后,做成公共utils */public class WriteLeadsOssUtils { private static Logger LOG = LoggerFactory.getLogger(WriteLeadsOssUtils.class.getName());  private static final String fileSub = ".csv";  private AtomicLong atomicLong = new AtomicLong(0L);  private WriteLeadsOssUtils() { }  private static class BuildWriteOSS {  private static final WriteLeadsOssUtils instance = new WriteLeadsOssUtils(); }  public static WriteLeadsOssUtils getInstance() {  return BuildWriteOSS.instance; }  private OSS getOssClient() {  return new OSSClientBuilder().build(OssParams.endpoint, OssParams.accessKeyId, OssParams.accessKeySecret); }  public URL writeJSONObjectFile(String content,String fileName) {  // 创建OSSClient实例。  OSS ossClient = getOssClient();  try {   DateTime currentTime = new DateTime(System.currentTimeMillis());//   String objectName = OssParams.rootName + currentTime.toString("yyyy-MM-dd-HH") + fileSub;   String objectName = OssParams.rootName + fileName;//   AppendObjectRequest appendObjectRequest = null;   String data = content;    ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));    Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 180);   URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);    LOG.info("{} saved to OSS, url={}", objectName,url.toString());//   for (IngestLeadingRequest param : params) {//    String jsonStr = JSONObject.toJSONString(param);//    JSONObject object = JSONObject.parseObject(jsonStr);//    data += object.toJSONString() + "\n";//   }   //文件不存在//   if (!ossClient.doesObjectExist(OssParams.bucketName, objectName)) {//    atomicLong.set(0L);//    appendObjectRequest = getAppendObjectRequest(data, true, objectName);//    appendObjectRequest.setPosition(0L);//   } else {//    appendObjectRequest = getAppendObjectRequest(data, false, objectName);//   }//   AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);//   atomicLong.set(appendObjectResult.getNextPosition());//   System.gc();   return url;  } catch (OSSException e) {   e.printStackTrace();   return null;  } catch (ClientException e) {   e.printStackTrace();   return null;  } finally {   ossClient.shutdown();  } }   /**  * flag标志位,true为新文件,false为追加文件  *  * @param data  * @param flag  * @param objectName  * @return  */ private AppendObjectRequest getAppendObjectRequest(String data, Boolean flag, String objectName) {  ObjectMetadata meta = new ObjectMetadata();  // 指定上传的内容类型。  meta.setContentType("text/plain;charset=utf-8");  if (flag == true) {   return new AppendObjectRequest(bucketName, objectName,     new ByteArrayInputStream(data.getBytes()), meta);  } else {   return new AppendObjectRequest(bucketName, objectName,     new ByteArrayInputStream(data.getBytes()), meta).withPosition(atomicLong.get());  } }}

关于java中怎么利用oss实现下载功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


新闻标题:java中怎么利用oss实现下载功能
转载源于:http://cdkjz.cn/article/igioij.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220