| | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.util.Base64; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | |
| | | public void downLoadFile(String file) { |
| | | HttpServletResponse response = WebUtils.response(); |
| | | String replace = file.replace(rootSrc, ""); |
| | | response.setHeader("Access-Control-Expose-Headers","File-Type"); |
| | | COSCredentials cred = new BasicCOSCredentials( |
| | | secretId, |
| | | secretKey); |
| | |
| | | cosClient.shutdown(); |
| | | } |
| | | } |
| | | // https://xzgttest-1305134071.cos.ap-chengdu.myqcloud.com/xizang/e4ea88b8-5470-456e-bf97-75cf47f38e84.jpg |
| | | public String downLoadFileImg(String file) { |
| | | byte[] data = null; |
| | | String replace = file.replace(rootSrc, ""); |
| | | COSCredentials cred = new BasicCOSCredentials( |
| | | secretId, |
| | | secretKey); |
| | | // 2.1 设置存储桶的地域(上文获得) |
| | | Region region = new Region(bucketAddr); |
| | | ClientConfig clientConfig = new ClientConfig(region); |
| | | // 2.2 使用https协议传输 |
| | | clientConfig.setHttpProtocol(HttpProtocol.https); |
| | | COSClient cosClient = new COSClient(cred, clientConfig); |
| | | try { |
| | | // 5. 下载文件并获取输入流 |
| | | InputStream inputStream = cosClient.getObject(bucketName, replace).getObjectContent(); |
| | | ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
| | | // 6. 处理输入流,例如读取内容或保存到本地文件 |
| | | byte[] buffer = new byte[1024]; |
| | | int len; |
| | | while ((len = inputStream.read(buffer)) != -1) { |
| | | // 处理读取到的数据 |
| | | swapStream.write(buffer, 0, len); |
| | | } |
| | | data = swapStream.toByteArray(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | // 7. 关闭输入流 |
| | | cosClient.shutdown(); |
| | | } |
| | | return Base64.getEncoder().encodeToString(data); |
| | | } |
| | | } |