|
@@ -65,44 +65,52 @@ public class OSSUtil {
|
|
|
}
|
|
|
|
|
|
public static void batchDownload(List<Map<String,String>> fileList, OutputStream output){
|
|
|
- BufferedInputStream bis = null;
|
|
|
-
|
|
|
try{
|
|
|
ZipOutputStream zos = new ZipOutputStream(output);
|
|
|
|
|
|
for (Map<String,String> map : fileList) {
|
|
|
String fileUrl = map.get("fileUrl");
|
|
|
+ String filePath = map.get("filePath");
|
|
|
+ String fileName = map.get("fileName");
|
|
|
|
|
|
- URL url = new URL(fileUrl);
|
|
|
- URLConnection conn = url.openConnection();
|
|
|
- InputStream inputStream = conn.getInputStream();
|
|
|
+ try {
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
+ fileName = fileUrl;
|
|
|
+ }
|
|
|
|
|
|
- byte[] buffs = new byte[1024 * 10];
|
|
|
+ if (fileName.indexOf("?") != -1) {
|
|
|
+ fileName = fileName.substring(0, fileName.indexOf("?"));
|
|
|
+ }
|
|
|
|
|
|
- String fileName = map.get("fileName");
|
|
|
+ fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
|
|
|
|
|
|
- if (StringUtils.isEmpty(fileName)){
|
|
|
- fileName = fileUrl;
|
|
|
- }
|
|
|
+ String zipFile = fileName;
|
|
|
|
|
|
- if (fileName.indexOf("?")!=-1){
|
|
|
- fileName = fileName.substring(0,fileName.indexOf("?"));
|
|
|
- }
|
|
|
+ if(StringUtils.isNotEmpty(filePath)){
|
|
|
+ zipFile = filePath + fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ ZipEntry zipEntry = new ZipEntry(zipFile);
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
|
|
|
- fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
|
|
|
+ URL url = new URL(fileUrl);
|
|
|
+ URLConnection conn = url.openConnection();
|
|
|
+ InputStream inputStream = conn.getInputStream();
|
|
|
|
|
|
- String zipFile = fileName;
|
|
|
+ byte[] buffs = new byte[1024 * 10];
|
|
|
|
|
|
- ZipEntry zipEntry = new ZipEntry(zipFile);
|
|
|
- zos.putNextEntry(zipEntry);
|
|
|
- bis = new BufferedInputStream(inputStream, 1024 * 10);
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(inputStream, 1024 * 10);
|
|
|
|
|
|
- int read;
|
|
|
- while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
|
|
|
- zos.write(buffs, 0, read);
|
|
|
- }
|
|
|
+ int read;
|
|
|
+ while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
|
|
|
+ zos.write(buffs, 0, read);
|
|
|
+ }
|
|
|
|
|
|
- inputStream.close();
|
|
|
+ bis.close();
|
|
|
+ }
|
|
|
+ catch(Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
zos.close();
|
|
@@ -110,18 +118,9 @@ public class OSSUtil {
|
|
|
catch(Exception ex){
|
|
|
log.error(ex.getMessage(),ex);
|
|
|
}
|
|
|
- finally {
|
|
|
- if(bis!=null){
|
|
|
- try {
|
|
|
- bis.close();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
- public static void presignedDownload(OSSConfig ossConfig, List<String> fileList, OutputStream output){
|
|
|
+ public static void presignedDownload(OSSConfig ossConfig, List<Map> fileList, OutputStream output){
|
|
|
BufferedInputStream bis = null;
|
|
|
|
|
|
try{
|
|
@@ -129,51 +128,63 @@ public class OSSUtil {
|
|
|
|
|
|
OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
|
|
|
|
|
|
- for (String filePath : fileList) {
|
|
|
- Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
|
|
|
+ for (Map<String,String> map : fileList) {
|
|
|
+ try {
|
|
|
+ String fileUrl = map.get("fileUrl");
|
|
|
+ String filePath = map.get("filePath");
|
|
|
|
|
|
- if (filePath.startsWith(ossConfig.getUrlPrefix())){
|
|
|
- filePath = filePath.substring(ossConfig.getUrlPrefix().length());
|
|
|
- }
|
|
|
+ Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
|
|
|
|
|
|
- if(filePath.indexOf("?")!=-1){
|
|
|
- filePath = filePath.substring(0,filePath.indexOf("?"));
|
|
|
- }
|
|
|
+ if (fileUrl.startsWith(ossConfig.getUrlPrefix())) {
|
|
|
+ fileUrl = fileUrl.substring(ossConfig.getUrlPrefix().length());
|
|
|
+ }
|
|
|
|
|
|
- if (filePath.startsWith("/")){
|
|
|
- filePath = filePath.substring(1);
|
|
|
- }
|
|
|
+ if (fileUrl.indexOf("?") != -1) {
|
|
|
+ fileUrl = fileUrl.substring(0, fileUrl.indexOf("?"));
|
|
|
+ }
|
|
|
|
|
|
- GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(ossConfig.getBucketName(), filePath, HttpMethod.GET);
|
|
|
+ if (fileUrl.startsWith("/")) {
|
|
|
+ fileUrl = fileUrl.substring(1);
|
|
|
+ }
|
|
|
|
|
|
- // 设置过期时间。
|
|
|
- request.setExpiration(expiration);
|
|
|
+ GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(ossConfig.getBucketName(), fileUrl, HttpMethod.GET);
|
|
|
|
|
|
- //设置缩放
|
|
|
- request.setProcess("image/resize,l_1024,limit_1");
|
|
|
+ // 设置过期时间。
|
|
|
+ request.setExpiration(expiration);
|
|
|
|
|
|
- // 生成签名URL(HTTP GET请求)。
|
|
|
- URL signedUrl = ossClient.generatePresignedUrl(request);
|
|
|
+ //设置缩放
|
|
|
+ request.setProcess("image/resize,l_1024,limit_1");
|
|
|
|
|
|
- // 使用签名URL发送请求。
|
|
|
- OSSObject ossObject = ossClient.getObject(signedUrl, new HashMap<>());
|
|
|
+ // 生成签名URL(HTTP GET请求)。
|
|
|
+ URL signedUrl = ossClient.generatePresignedUrl(request);
|
|
|
|
|
|
- if(ossObject!=null) {
|
|
|
- InputStream inputStream = ossObject.getObjectContent();
|
|
|
- byte[] buffs = new byte[1024 * 10];
|
|
|
- String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
|
|
|
- String zipFile = fileName;
|
|
|
+ // 使用签名URL发送请求。
|
|
|
+ OSSObject ossObject = ossClient.getObject(signedUrl, new HashMap<>());
|
|
|
|
|
|
- ZipEntry zipEntry = new ZipEntry(zipFile);
|
|
|
- zos.putNextEntry(zipEntry);
|
|
|
- bis = new BufferedInputStream(inputStream, 1024 * 10);
|
|
|
+ if (ossObject != null) {
|
|
|
+ InputStream inputStream = ossObject.getObjectContent();
|
|
|
+ byte[] buffs = new byte[1024 * 10];
|
|
|
+ String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
|
|
|
+ String zipFile = fileName;
|
|
|
|
|
|
- int read;
|
|
|
- while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
|
|
|
- zos.write(buffs, 0, read);
|
|
|
- }
|
|
|
+ if (StringUtils.isNotEmpty(filePath)){
|
|
|
+ zipFile = filePath + fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ ZipEntry zipEntry = new ZipEntry(zipFile);
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
+ bis = new BufferedInputStream(inputStream, 1024 * 10);
|
|
|
|
|
|
- ossObject.close();
|
|
|
+ int read;
|
|
|
+ while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
|
|
|
+ zos.write(buffs, 0, read);
|
|
|
+ }
|
|
|
+
|
|
|
+ ossObject.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch(Exception ex){
|
|
|
+ log.error(ex.getMessage(),ex);
|
|
|
}
|
|
|
}
|
|
|
|