Spring 启动 HTTP 获取请求 return 一个字符串和一个文件
Spring boot HTTP get request return a String and a file
我有一个使用 angular 4 首页的 Sprint 启动应用程序。我从我的首页发送一个带有 SQL 查询和一些其他参数的 HTTP get 请求。
现在我希望响应是 String
+zip file
.
我知道我可以发回这样的文件:
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
.body(file);
但我不知道如何在 ReponseEntity
.
中组合文件和字符串
到目前为止,这是我的代码(服务器端):
控制器:
@GetMapping("/sql")
public ResponseEntity<String> handleSqlRequest(
@RequestParam("sql") String sql,
@RequestParam("source") String source,
@RequestParam("genFile") Boolean genFile
) {
SqlToolResponse sqlToolResponse;
if(genFile) {
//Create a ZIP file and execute sql
sqlToolResponse = this.sqlToolService.executeSqlWithFiles(sql, source, "oracle.jdbc.OracleDriver");
} else {
//Only execute SQL
sqlToolResponse = this.sqlToolService.executeSql(sql, source, "oracle.jdbc.OracleDriver");
}
Resource file = new UrlResource(("MYPATH/"+sqlToolReponse.getZipName()).toUri());
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + sqlToolResponse.getZipName()) + "\"")
.body(file);
}
SqlToolResponse:
public class SqlToolResponse {
private String result;
private String zipName;
public SqlToolResponse(String someResult, String zip) {
this.result = someResult;
this.zipName = zip;
}
public SqlToolResponse(String someResult) {
this.result = someResult;
this.zipName = null;
}
public String getResult() {
return result;
}
public String getZipName() {
return zipName;
}
}
有什么想法吗?
您有多种方法可以做到这一点:
- return 一个 object 有 2 个字段(
String
和 File
)
- return一个
Map<String, Object>
- 在 header
中设置字符串值
将您的方法更改为
MultiValueMap<String, Object> handleSqlRequest(@RequestParam("sql") String sql,
@RequestParam("source") String source, @RequestParam("genFile") Boolean genFile) {
// ......
MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>();
HttpHeaders headers1 = new HttpHeaders();
headers1.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""
+ sqlToolResponse.getZipName()) + "\"");
HttpEntity<Resource> part1 = new HttpEntity<>(file, headers1);
HttpHeaders headers2 = new HttpHeaders();
headers2.setContentType(MediaType.TEXT_PLAIN);
HttpEntity<String> part2 = new HttpEntity<>(sqlToolReponse.getResult(), headers2);
body.add("file", part1);
body.add("string", part2);
return body;
}
我有一个使用 angular 4 首页的 Sprint 启动应用程序。我从我的首页发送一个带有 SQL 查询和一些其他参数的 HTTP get 请求。
现在我希望响应是 String
+zip file
.
我知道我可以发回这样的文件:
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
.body(file);
但我不知道如何在 ReponseEntity
.
到目前为止,这是我的代码(服务器端):
控制器:
@GetMapping("/sql")
public ResponseEntity<String> handleSqlRequest(
@RequestParam("sql") String sql,
@RequestParam("source") String source,
@RequestParam("genFile") Boolean genFile
) {
SqlToolResponse sqlToolResponse;
if(genFile) {
//Create a ZIP file and execute sql
sqlToolResponse = this.sqlToolService.executeSqlWithFiles(sql, source, "oracle.jdbc.OracleDriver");
} else {
//Only execute SQL
sqlToolResponse = this.sqlToolService.executeSql(sql, source, "oracle.jdbc.OracleDriver");
}
Resource file = new UrlResource(("MYPATH/"+sqlToolReponse.getZipName()).toUri());
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + sqlToolResponse.getZipName()) + "\"")
.body(file);
}
SqlToolResponse:
public class SqlToolResponse {
private String result;
private String zipName;
public SqlToolResponse(String someResult, String zip) {
this.result = someResult;
this.zipName = zip;
}
public SqlToolResponse(String someResult) {
this.result = someResult;
this.zipName = null;
}
public String getResult() {
return result;
}
public String getZipName() {
return zipName;
}
}
有什么想法吗?
您有多种方法可以做到这一点:
- return 一个 object 有 2 个字段(
String
和File
) - return一个
Map<String, Object>
- 在 header 中设置字符串值
将您的方法更改为
MultiValueMap<String, Object> handleSqlRequest(@RequestParam("sql") String sql,
@RequestParam("source") String source, @RequestParam("genFile") Boolean genFile) {
// ......
MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>();
HttpHeaders headers1 = new HttpHeaders();
headers1.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""
+ sqlToolResponse.getZipName()) + "\"");
HttpEntity<Resource> part1 = new HttpEntity<>(file, headers1);
HttpHeaders headers2 = new HttpHeaders();
headers2.setContentType(MediaType.TEXT_PLAIN);
HttpEntity<String> part2 = new HttpEntity<>(sqlToolReponse.getResult(), headers2);
body.add("file", part1);
body.add("string", part2);
return body;
}