實作從oss(阿裡雲)伺服器以附件形式下載下傳檔案(含批量下載下傳)
筆者在項目中寫一個從阿裡雲伺服器上面以附件形式下載下傳檔案的接口時,遇到了問題,網上搜尋無任何相關的解決方案,最後通過通過自己查閱API文檔,再結合自己的經驗,實作了下載下傳功能。
通過查詢oss官方文檔,我發現隻有一個下載下傳檔案到本地的方法(方法1),但是這個方法下載下傳的檔案隻能夠将檔案下載下傳到本地的一個固定的目錄下,即必須要在API提供的方法中寫死下載下傳檔案的下載下傳路徑,而且下載下傳檔案時沒有任何下載下傳提示,是以這個方法不适合直接在項目中使用。
[java] view plain copy- /**
- * 從阿裡雲下載下傳檔案 (下載下傳目錄定死了的,無法更改)
- * @param map
- * @return
- */
- // endpoint以杭州為例,其它region請按實際情況填寫
- String endpoint = ”http://oss-cn-hangzhou.aliyuncs.com”;
- // accessKey請登入https://ak-console.aliyun.com/#/檢視
- String accessKeyId = ”<yourAccessKeyId>”;
- String accessKeySecret = ”<yourAccessKeySecret>”;
- String bucketName = ”<yourBucketName>”;
- // 建立OSSClient執行個體
- OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
- // 下載下傳object到檔案 new File(“<yourLocalFile>”這個file對象需要給定一個本地目錄,檔案會下載下傳到該目錄中
- ossClient.getObject(new GetObjectRequest(bucketName, “<yourKey>”), new File(“<yourLocalFile>”));
- // 關閉client
- ossClient.shutdown();
/** * 從阿裡雲下載下傳檔案 (下載下傳目錄定死了的,無法更改) * @param map * @return */ // endpoint以杭州為例,其它region請按實際情況填寫 String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // accessKey請登入https://ak-console.aliyun.com/#/檢視 String accessKeyId = "<yourAccessKeyId>"; String accessKeySecret = "<yourAccessKeySecret>"; String bucketName = "<yourBucketName>"; // 建立OSSClient執行個體 OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); // 下載下傳object到檔案 new File("<yourLocalFile>"這個file對象需要給定一個本地目錄,檔案會下載下傳到該目錄中 ossClient.getObject(new GetObjectRequest(bucketName, "<yourKey>"), new File("<yourLocalFile>")); // 關閉client ossClient.shutdown();
最後通過API文檔發現:
在“下載下傳闆塊”第一個“以流形式”下載下傳檔案的方法中,“ossObject.getObjectContent()”方法可以擷取指定id的檔案并傳回一個位元組流,是以可以利用這個特性,自己改造一個方法(方法2):
- * 從阿裡雲下載下傳檔案 (以附件形式下載下傳)
- * @param request
- * @param response
- */
- @ResponseBody
- @RequestMapping(value = “/download”, method = RequestMethod.GET)
- public void downLoadFile(HttpServletRequest request,HttpServletResponse response){
- try {
- String fileid = request.getParameter(”fileid”).toString();//從前台擷取目前下載下傳檔案的id值(每個上傳到阿裡雲的檔案都會有一個獨一無二的id值)
- String filename =request.getParameter(”filename”).toString();//從前台擷取要下載下傳檔案的檔案名
- int i=filename.lastIndexOf(“\\”);
- filename=filename.substring(i+1);
- String aliyunId = ApplicationPropertyUtils.getContextProperty(”ALIYUN_ACCESS_KEY_ID”);
- String aliyunSecret = ApplicationPropertyUtils.getContextProperty(”ALIYUN_ACCESS_KEY_SECRET”);
- String ossEndpoint = ApplicationPropertyUtils.getContextProperty(”ALIYUN_OSS_ENDPOINT”);
- OSSClient ossClient = new OSSClient(ossEndpoint, aliyunId, aliyunSecret);
- //擷取fileid對應的阿裡雲上的檔案對象
- OSSObject ossObject = ossClient.getObject(bucketName, fileid);//bucketName需要自己設定
- // 讀去Object内容 傳回
- BufferedInputStream in=new BufferedInputStream(ossObject.getObjectContent());
- //System.out.println(ossObject.getObjectContent().toString());
- BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream());
- //通知浏覽器以附件形式下載下傳
- response.setHeader(”Content-Disposition”,“attachment;filename=”+URLEncoder.enco de(filename,“utf-8”));
- //BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(new File(“f:\\a.txt”)));
- byte[] car=new byte[1024];
- int L=0;
- while((L=in.read(car))!=-1){
- out.write(car, 0,L);
- }
- if(out!=null){
- out.flush();
- out.close();
- if(in!=null){
- in.close();
- ossClient.shutdown();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
/** * 從阿裡雲下載下傳檔案 (以附件形式下載下傳) * @param request * @param response */ @ResponseBody @RequestMapping(value = "/download", method = RequestMethod.GET) public void downLoadFile(HttpServletRequest request,HttpServletResponse response){ try { String fileid = request.getParameter("fileid").toString();//從前台擷取目前下載下傳檔案的id值(每個上傳到阿裡雲的檔案都會有一個獨一無二的id值) String filename =request.getParameter("filename").toString();//從前台擷取要下載下傳檔案的檔案名 int i=filename.lastIndexOf("\\"); filename=filename.substring(i+1); String aliyunId = ApplicationPropertyUtils.getContextProperty("ALIYUN_ACCESS_KEY_ID"); String aliyunSecret = ApplicationPropertyUtils.getContextProperty("ALIYUN_ACCESS_KEY_SECRET"); String ossEndpoint = ApplicationPropertyUtils.getContextProperty("ALIYUN_OSS_ENDPOINT"); OSSClient ossClient = new OSSClient(ossEndpoint, aliyunId, aliyunSecret); //擷取fileid對應的阿裡雲上的檔案對象 OSSObject ossObject = ossClient.getObject(bucketName, fileid);//bucketName需要自己設定 // 讀去Object内容 傳回 BufferedInputStream in=new BufferedInputStream(ossObject.getObjectContent()); //System.out.println(ossObject.getObjectContent().toString()); BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream()); //通知浏覽器以附件形式下載下傳 response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.enco de(filename,"utf-8")); //BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(new File("f:\\a.txt"))); byte[] car=new byte[1024]; int L=0; while((L=in.read(car))!=-1){ out.write(car, 0,L); } if(out!=null){ out.flush(); out.close(); } if(in!=null){ in.close(); } ossClient.shutdown(); } catch (Exception e) { e.printStackTrace(); } }
上面這段代碼就實作了使用者自定義檔案下載下傳路徑,并成功下載下傳檔案的方法!
——注意:
在實際使用該方法下載下傳的過程中,可能遇到伺服器不報錯,但就是下載下傳不下來檔案的問題,這樣有可能是前端頁面發出下載下傳請求的方式有誤,必須是GET請求,而且不知道為什麼,不能使用AJAX的get方式通路改方法,筆者使用的方式是用window.location.href通路,可能還有其它方式可以通路,筆者這裡就不在說明了。
原文位址
http://www.bieryun.com/3022.html