我有一个使用角度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;
}
}
任何想法?
答案 0 :(得分:0)
您有多种方法可以做到这一点:
String
和File
)Map<String, Object>
答案 1 :(得分:0)
将您的方法更改为
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;
}