Java 无法从web服务下载xlsx
我试图从基于RESTEasy的web服务下载使用ApacheXSSF生成的xlsx文件 我可以下载文件,但双击打开时,显示文件无法打开: 以下是源代码: Web服务控制器:Java 无法从web服务下载xlsx,java,web-services,download,resteasy,xssf,Java,Web Services,Download,Resteasy,Xssf,我试图从基于RESTEasy的web服务下载使用ApacheXSSF生成的xlsx文件 我可以下载文件,但双击打开时,显示文件无法打开: 以下是源代码: Web服务控制器: @GET @Path(/download) @Produces(MediaType.APPLICATION_OCTET_STREAM) public Response downloadFile() { ByteArrayOutputStream baos = myService.processGbiValidat
@GET
@Path(/download)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFile() {
ByteArrayOutputStream baos = myService.processGbiValidationExceptions();
ResponseBuilder response = Response.ok((Object) baos.toByteArray());
response.header("Content-Disposition", "attachment;filename=My_File.xlsx");
return response.build();
}
public ByteArrayOutputStream processGbiValidationExceptions() {
XSSFWorkbook workbook = new XSSFWorkbook();
// code to write to workbook
// Create stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
workbook.write(baos);
} catch (IOException e) {
LOGGER.error("Error occurred while writing workbook to stream", e);
throw new IptException(e);
}
return baos;
}
服务:
@GET
@Path(/download)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFile() {
ByteArrayOutputStream baos = myService.processGbiValidationExceptions();
ResponseBuilder response = Response.ok((Object) baos.toByteArray());
response.header("Content-Disposition", "attachment;filename=My_File.xlsx");
return response.build();
}
public ByteArrayOutputStream processGbiValidationExceptions() {
XSSFWorkbook workbook = new XSSFWorkbook();
// code to write to workbook
// Create stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
workbook.write(baos);
} catch (IOException e) {
LOGGER.error("Error occurred while writing workbook to stream", e);
throw new IptException(e);
}
return baos;
}
有没有线索表明我在做什么?谢谢
附言:我在Mac上 我以前下载文件时使用的客户端代码几乎没有问题。现在,我正在使用它,它的工作 代码如下:
import download from 'downloadjs';
.
.
.
fetch(FETCH_URL, {
method: 'GET',
dataType: 'json',
credentials: 'include'
}).then(response => {
if(response.status === 200) {
return response.blob();
}
}, () => {
// when error do something
}
).then(
// Following line helps download
blob => download(blob, "someexcel.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
)
此外,MIME类型需要设置为application/vnd.openxmlformats officedocument.spreadsheetml.sheet
,以便下载xlsx
文件:
@Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
干杯