在响应 REST 中发送文件时出现 HTTP 204 错误

HTTP 204 error when sending File in response REST

这是我写入 excel 的方法 returns javax.ws.rs.core.Response

public  Response writeToExcel(UserDeatilsVOWrapper listBook) {
    XSSFWorkbook  workbook = new XSSFWorkbook();
    XSSFSheet spreadsheet = workbook.createSheet("Resource Information");

    int rowCount = 0;
    createHeaderRow(spreadsheet);
    for (UserDetailsVO detailsVO : listBook.getUserDetailsList()) {
        Row row = spreadsheet.createRow(++rowCount);
        writeBook(detailsVO, row);
    }
    Response response = null;

    try (FileOutputStream outputStream = new FileOutputStream(new File("ResourceInformation.xlsx"))) {
        workbook.write(outputStream);

        // header required to enable download pop-up and set file name
        Response.ok().header("Content-Disposition", "attachment; filename=" + "ResourceInformation.xlsx").build();
    }   

    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return response;
}

这是我的网络服务:

@POST
    @Path(WebServiceConstants.DOWNLOAD_EXCEL)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response getFile(UserDeatilsVOWrapper wrapper) {
   Response respose=new ExportToExcel().writeToExcel(wrapper);
    return respose;}

我收到 HTTP204 错误。我正在使用邮递员。我知道,我在写入 excel 方法以及尝试发送文件和响应时犯了一个大错误。 还有什么可能的方法可以在不将文件保存在服务器上的情况下在 REST 响应上写入文件对象?我在这里做得很糟糕。感谢您的帮助。

我没有看到您将文件设置为响应的位置。通常你会做这样的事情

File file = new File("ResourceInformation.xlsx"))

// Do your excel-writing here...

ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment; filename=" + "ResourceInformation.xlsx");
return response.build();