這篇文章將為大家詳細(xì)講解有關(guān)java中怎么利用oss實現(xiàn)下載功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
成都創(chuàng)新互聯(lián)公司專注于輝縣企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站制作。輝縣網(wǎng)站建設(shè)公司,為輝縣等地區(qū)提供建站服務(wù)。全流程按需定制開發(fā),專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
首先,導(dǎo)入依賴包
參數(shù)類
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; /** * 先做單例對象調(diào)用,防止多線程并發(fā)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) { // 創(chuàng)建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標(biāo)志位,true為新文件,false為追加文件 * * @param data * @param flag * @param objectName * @return */ private AppendObjectRequest getAppendObjectRequest(String data, Boolean flag, String objectName) { ObjectMetadata meta = new ObjectMetadata(); // 指定上傳的內(nèi)容類型。 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()); } }}
關(guān)于java中怎么利用oss實現(xiàn)下載功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。