华为云obs;
This commit is contained in:
@@ -7,6 +7,7 @@ import com.linke.product.domain.menu.entity.Menu;
|
||||
import com.linke.product.domain.menu.repository.po.MenuPo;
|
||||
import com.linke.product.domain.util.Base64Util;
|
||||
import com.linke.product.infrastructure.obs.FileStorageService;
|
||||
import com.linke.product.infrastructure.obs.ObsProperties;
|
||||
import com.linke.product.interfaces.assemble.menu.MenuAssembler;
|
||||
import com.linke.product.interfaces.dto.menu.MenuDto;
|
||||
import com.mhd.common.core.domain.po.MenuVo;
|
||||
@@ -21,13 +22,16 @@ import com.mhd.common.log.annotation.Log;
|
||||
import com.mhd.common.log.enums.BusinessType;
|
||||
import com.mhd.common.security.utils.SecurityUtils;
|
||||
import com.mhd.system.api.model.LoginUser;
|
||||
import com.obs.services.ObsClient;
|
||||
import com.obs.services.model.GetObjectRequest;
|
||||
import com.obs.services.model.ObsObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
@@ -52,6 +56,21 @@ public class MenuApi extends BaseController {
|
||||
@Autowired
|
||||
private FileStorageService fileStorageService;
|
||||
|
||||
@Autowired
|
||||
private ObsClient obsClient;
|
||||
|
||||
@Autowired
|
||||
private ObsProperties obsProperties;
|
||||
|
||||
private String bucketName;
|
||||
|
||||
private String folder = "mhd";
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.bucketName = obsProperties.getBucketName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询菜单集合
|
||||
*/
|
||||
@@ -299,6 +318,42 @@ public class MenuApi extends BaseController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法2:使用StreamingResponseBody(更灵活)
|
||||
*/
|
||||
@GetMapping("/download-streaming")
|
||||
public ResponseEntity<StreamingResponseBody> downloadStreaming(
|
||||
@RequestParam String bucketName,
|
||||
@RequestParam String objectKey,
|
||||
@RequestParam String filename) {
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||
"attachment; filename=\"" + filename + "\"")
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.body(outputStream -> {
|
||||
//ObsClient obsClient = null;
|
||||
try {
|
||||
//obsClient = new ObsClient(accessKey, secretKey, endpoint);
|
||||
GetObjectRequest request = new GetObjectRequest(bucketName, objectKey);
|
||||
ObsObject obsObject = obsClient.getObject(request);
|
||||
|
||||
try (InputStream inputStream = obsObject.getObjectContent()) {
|
||||
byte[] buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
outputStream.flush();
|
||||
}
|
||||
} finally {
|
||||
if (obsClient != null) {
|
||||
obsClient.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description 临时使用产品的oss 公有读
|
||||
|
||||
Reference in New Issue
Block a user