java利用oss實現下載功能
阿新 • • 發佈:2020-01-07
本文例項為大家分享瞭如何利用oss實現下載功能的具體程式碼,供大家參考,具體內容如下
目前我接觸的實現下載功能有3種方式。第一種,就是把要下載的內容寫到本地目錄,然後把目錄返回給前端,利用a標籤下載。第二種,本地不允許訪問,掛載目錄,使目錄可以對外訪問,同樣的,把目錄返給前端下載。第三種,使用第三方儲存,其中阿里雲oss就是一種可以用來實現的物件儲存。
首先,匯入依賴包
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.6.0</version> </dependency>
引數類
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,expiration); LOG.info("{} saved to OSS,url={}",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,new ByteArrayInputStream(data.getBytes()),meta); } else { return new AppendObjectRequest(bucketName,meta).withPosition(atomicLong.get()); } } }
呼叫方法
WriteLeadsOssUtils.getInstance().writeJSONObjectFile("hello"); //將hello作為內容存入oss,並獲得可以下載的url
利用上述方式,可以直接將可以下載的url返回給前端,直接用視窗開啟就可以下載。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。