您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了如何利用oss實(shí)現(xiàn)下載功能的具體代碼,供大家參考,具體內(nèi)容如下
目前我接觸的實(shí)現(xiàn)下載功能有3種方式。第一種,就是把要下載的內(nèi)容寫到本地目錄,然后把目錄返回給前端,利用a標(biāo)簽下載。第二種,本地不允許訪問(wèn),掛載目錄,使目錄可以對(duì)外訪問(wèn),同樣的,把目錄返給前端下載。第三種,使用第三方存儲(chǔ),其中阿里云oss就是一種可以用來(lái)實(shí)現(xiàn)的對(duì)象存儲(chǔ)。
首先,導(dǎo)入依賴包
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.6.0</version> </dependency>
參數(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; /** * 先做單例對(duì)象調(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實(shí)例。 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()); } } }
調(diào)用方法
WriteLeadsOssUtils.getInstance().writeJSONObjectFile("hello"); //將hello作為內(nèi)容存入oss,并獲得可以下載的url
利用上述方式,可以直接將可以下載的url返回給前端,直接用窗口打開(kāi)就可以下載。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。