我正在使用 apache compress 來創建多個檔案并將其壓縮到一個 tar.gz 檔案中。然后,我嘗試創建另一個應用程式可以聯系的端點,以便接收 tar.gz 檔案。
我嘗試了多種不同的方法,但似乎無法獲得所需的輸出。目前我可以將壓縮檔案作為 .gz 檔案回傳,但我需要將其作為 .tar.gz 檔案。
我的控制器方法看起來像:
public ResponseEntity<?> getBundle(){
BundleService bundleService = new BundleService();
List<File>fileList = new ArrayList<File>();
List<String> implementationIds = policyService.getImplementationIdListForBundle(EnvironmentType.DEV, PlatformEngineType.REGO);
List<PolicyImplementation> implementationList = policyService.getImplementationsForBundle(implementationIds);
fileList = bundleService.createImplementationFiles(implementationList);
bundleService.createTarBall(fileList);
File tarball = new File("./policyadmin/bundleDirectory/bundle.tar");
//File gzippedTarball = bundleService.gzipTarBall();
String gzippedLocation = tarball.getAbsolutePath() ".gz";
//File gzippedFile = new File(gzippedLocation);
Resource tarResource = new FileSystemResource(tarball);
//bundleService.deleteTarball(tarball);
return new ResponseEntity<Object>(tarResource, HttpStatus.OK); //! If I return a File instead of a resource gives me No converter for [class java.io.File] with preset Content-Type 'null']
}
我還有以下代碼來處理請求:
//GET: Endpoint for OPA to retrieve on the fly gzipped tarball for Bundles
@ApiOperation(value = "Method to retrieve on the fly gzipped tarball for bundles", nickname = "getBundle", notes="", response = Resource.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Request for Bundle.tar.gz received", response = Resource.class),
@ApiResponse(code = 404, message = "Unable to find requested Bundle.tar.gz")
})
@GetMapping(value = "/policyadmin/bundle", produces= { "application/gzip" })
default ResponseEntity<?> getBundle(){
return new ResponseEntity<File>(HttpStatus.NOT_IMPLEMENTED);
}
uj5u.com熱心網友回復:
注意:確保你有 tar.gz 檔案
設定內容型別,如:
public ResponseEntity<?> getBundle(HttpServletResponse response){
// Your other code
response.setContentType("application/gzip");
response.setHeader("Content-Disposition", "attachment; filename=\"bundle.tar.gz\"");
return new ResponseEntity<Object>(tarResource, HttpStatus.OK); //! If I return a File instead of a resource gives me No converter for [class java.io.File] with preset Content-Type 'null']
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/331666.html
下一篇:Spring控制器不適用于映射
