【问题标题】:HTTP 204 error when sending File in response REST在响应 REST 中发送文件时出现 HTTP 204 错误
【发布时间】:2016-06-04 03:01:04
【问题描述】:

这是我写到返回 javax.ws.rs.core.Response 的 excel 方法

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 apache-poi restful-architecture restful-url http-error


    【解决方案1】:

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

    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();
    

    【讨论】:

    • 非常感谢,伙计,你是救世主。我知道,我犯了一个错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 2018-01-19
    • 2021-12-24
    • 2015-08-27
    • 2016-10-14
    • 2020-03-24
    • 2011-06-11
    相关资源
    最近更新 更多