Java 下载时如何设置文件名为zip?
我有一个RESTAPI,它允许我将多个ID传递给一个资源,以便从特定表下载记录并压缩它。MSSQL是后端主控消息 因此,当ID作为param传递时,它调用数据库表以返回消息数据。代码如下:Java 下载时如何设置文件名为zip?,java,spring,spring-boot,zip,Java,Spring,Spring Boot,Zip,我有一个RESTAPI,它允许我将多个ID传递给一个资源,以便从特定表下载记录并压缩它。MSSQL是后端主控消息 因此,当ID作为param传递时,它调用数据库表以返回消息数据。代码如下: @GetMapping("message/{ids}") public void downloadmessage(@PathVariable Long[] ids, HttpServletResponse response) throws Exception { List<Multiplemes
@GetMapping("message/{ids}")
public void downloadmessage(@PathVariable Long[] ids, HttpServletResponse response) throws Exception {
List<MultiplemessageID> multiplemessageID = auditRepository.findbyId(ids);
String xml = new ObjectMapper().writeValueAsString(MultiplemessageID);
String fileName = "message.zip";
String xml_name = "message.xml";
byte[] data = xml.getBytes();
byte[] bytes;
try (ByteOutputStream bout = new ByteOutputStream(); ZipOutputStream zout = new ZipOutputStream(bout)) {
for (Long id : ids) {
zout.setLevel(1);
ZipEntry ze = new ZipEntry(xml_name);
ze.setSize(data.length);
ze.setTime(System.currentTimeMillis());
zout.putNextEntry(ze);
zout.write(data);
zout.closeEntry();
}
bytes = bout.getBytes();
}
response.setContentType("application/zip");
response.setContentLength(bytes.length);
response.setHeader("Content-Disposition", "attachment; " + String.format("filename=" + fileName));
ServletOutputStream outputStream = response.getOutputStream();
FileCopyUtils.copy(bytes, outputStream);
outputStream.close();
}
我需要创建一个带有头和输入参数的文件名。一旦文件名设置好,我想用正确的文件名下载个人记录并压缩
Zip文件名为message.Zip。解压时,它应该包含单个文件,如Domain_1.xml、Domain_2.xml、Domain_3.xml、Domain_4.xml等
我如何做到这一点?请告知。我需要一些关于java的有限知识的指导。谢谢。这是你要找的东西吗?@codeMan我也在找类似的东西,但我在Java方面的知识有限,无法让它工作。我需要帮助。非常感谢。
MSG_ID C_ID NAME INSERT_TIMESTAMP MSG CONF F_NAME POS ID INB HEADERS
0011d540 EDW,WSO2,AS400 invoicetoedw 2019-08-29 23:59:13 <invoice>100923084207</invoice> [iden1:SMTP, iden2:SAP, service:invoicetoedw, clients:EDW,WSO2,AS400, file.path:/c:/nfs/store/invoicetoedw/output, rqst.message.format:XML,] p3_pfi_1 Pre 101 MES_P3_IN [clients:EDW,WSO2,AS400, UniqueName:Domain]
private static String serviceNameHeadersToMap(String headers) {
String sHeaders = headers.replace("[", "");
sHeaders = sHeaders.replace("]", "");
String res = Arrays.stream(sHeaders.split(", "))
.filter(s->s.contains("serviceNameIdentifier"))
.findFirst()
.map(name->name.split(":")[1])
.orElse("Not Present");
return res;