【问题标题】:Downloading Excel using POST Rest Service使用 POST Rest 服务下载 Excel
【发布时间】:2017-04-02 04:13:48
【问题描述】:

我正在使用 Spring Framework 提供的 REST Web 服务。 我需要下载一个 Excel 表格,但我还需要根据一些选定的参数下载表格。我将请求类对象作为 Body 发送到 POST Rest 调用(@RequestBody)

我无法使用 POST 方法下载 excel。请帮助我实现这一目标。

@RequestMapping(value = "/search/export", method = RequestMethod.POST,, produces = MediaType.APPLICATION_JSON_VALUE)
public void searchResultToExcel(@RequestBody SearchRequest searchRequest, HttpServletResponse response, HttpServletRequest request) throws Exception 

这是我的方法签名

【问题讨论】:

  • 你为什么使用 POST?为什么不在查询字符串中使用带有搜索参数的 GET?
  • 搜索参数是对象的类型:(
  • 您是说您无法控制前端应用程序吗?
  • 快速愚蠢的问题。如果您不传递参数,您通常会使用 fetch/get 方法吗?

标签: excel rest post


【解决方案1】:

ResponseBuilder 响应 = Response.ok(file); response.header("Content-Disposition", "attachment; filename=\"" + fileName2 + "\""); 返回 response.build();

【讨论】:

    【解决方案2】:
    @Post
    @Path("downloadMyReport")
    @Produces("application/excel")
    public static Response generatemyExcelReport()throws BusinessException     {
        try {
            File file=null;
            Date reportDate=new Date() ;
    
            path="/home/Documents/excelReport/"
            file=getReportByName(path);
    
            if(file==null){
                logger.info("File is null");
    
            else{
                    name=capitalizeFirstLater(name);
    
                getReportSummary(reportDate);
                FileUtils.writeByteArrayToFile(new File(path),createExcelForReport(fileName,path));
                file=getReportByName(path);
            }   
    
    
            ResponseBuilder response = Response.ok(file);
            response.header("Content-Disposition", "attachment; filename=\"" + fileName2 + "\"");
            return response.build();
    
        }
        }catch (BusinessException e) {
            throw e;
        }catch (Exception e) {
            logger.error("Exception while generating ExcelSheetForMyReport {}",Utils.getStackTrace(e));
            throw new BusinessException("Error in downloading ExcelSheetForMyReport");
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      我发现这个帖子 Return Excel downloadable file from Spring 可能有用。

      我还认为您强制的内容类型(产生 = MediaType.APPLICATION_JSON_VALUE)可能会妨碍您,至少就我能理解的问题而言。我认为您应该在那里强制使用 EXCEL 内容类型 (application/vnd.ms-excel)。

      上面写着:

      您需要设置Content-Disposition 标头。

      response.setHeader("Content-disposition","attachment; filename=" + yourFileName);
      

      并将您的字节直接写入响应OutputStream

      File xls = new File("exported.xls"); // or whatever your file is
      FileInputStream in = new FileInputStream(xls);
      OutputStream out = response.getOutputStream();
      
      byte[] buffer= new byte[8192]; // use bigger if you want
      int length = 0;
      
      while ((length = in.read(buffer)) > 0){
           out.write(buffer, 0, length);
      }
      in.close();
      out.close();
      

      上面的比较老了。你现在可以用FileSystemResource 构造一个ResponseEntity。然后,ResourceHttpMessageConverter 将为您复制字节,正如我上面建议的那样。 Spring MVC 让您更简单,而不是让您与 Servlet 规范中的接口进行交互。

      【讨论】:

        猜你喜欢
        • 2020-01-02
        • 1970-01-01
        • 2013-06-07
        • 2017-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多