【问题标题】:Can static content on spring-boot-web application be dynamic (refreshed)?spring-boot-web 应用程序上的静态内容可以是动态的(刷新)吗?
【发布时间】:2021-10-16 02:23:41
【问题描述】:

我仍在搜索这个主题,但找不到简单的解决方案,我不确定它是否不存在。

第 1 部分

  • 我的应用程序上有一项服务,它通过动态 DB 数据生成 Excel 文档。

     public static void 
     notiSubscribersToExcel(List<NotificationsSubscriber> 
     data) {
     //generating the file dynamically from DB's data
    
     String prefix = "./src/main/resources/static";
     String directoryName = prefix + "/documents/";
     String fileName = directoryName + "subscribers_list.xlsx";
    
     File directory = new File(directoryName);
     if (! directory.exists()){
         directory.mkdir();
         // If you require it to make the entire directory path including parents,
         // use directory.mkdirs(); here instead.
     }
    
     try (OutputStream fileOut = new FileOutputStream(fileName)) {
    
         wb.write(fileOut);
         fileOut.close();
         wb.close();
    
     } catch (FileNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
    

第 2 部分

我想从浏览器访问它,所以当我调用它时它会被下载。 我知道对于静态内容,我需要做的就是像这样从浏览器调用文件:

http://localhost:8080/documents/myfile.xlsx

在我能够做到这一点之后,我只需要从我的客户端应用程序创建指向此 url 的链接。

问题 - 目前,如果我按上述方式调用文件,它只会下载在编译阶段已经存在的文件,但如果我在应用程序运行后生成新文件,则内容将不可用。

内容似乎(因为它被称为)“静态”,启动后无法更改。

所以我的问题是

  • 有没有办法在应用程序结构上定义一个动态的文件夹?我只想访问新生成的文件。

顺便说一句 我找到了this answer 和其他做配置方法或网络服务的人,但我不想要这一切。而且我也试了一些,结果都是一样的。

仅供参考我没有将我的客户端应用程序与服务器应用程序捆绑在一起,而是从不同的主机运行它们

【问题讨论】:

  • 不清楚你真正想要什么。你说你想要动态的文件,但文件已经生成。哪一部分是动态的?
  • @GaëlJ 我的意思是它是由客户端与其他内容一起生成的,但我得到的是旧内容
  • 哪个客户?请用更多细节更新您的问题,例如正在发生的情况和您的预期。
  • 几个想法: 1 - 不要写入src/main/resources,该文件夹不会存在于生产服务器上 2 - 不要使用文件的静态服务,而是实现一个控制器读取文件内容并发回给调用者
  • 如果您希望您的应用真正动态化,那么您应该在运行时使用 Java 库(例如 jexcelapi.sourceforge.net/resources/javadocs/current/docs/jxl/…)动态创建 Excel 文档。

标签: java spring spring-boot


【解决方案1】:

问题是从 Spring 应用下载包含动态内容的文件。

这可以通过 Spring BOOT 解决。这是如图所示的解决方案 - 当我点击下载报告时,我的应用程序会生成一个动态 Excel 报告并将其下载到浏览器:

从 JS 向 Spring Controller 发出 get 请求:

function DownloadReport(e){

     //Post the values to the controller
     window.location="../report" ;
}

这是带有 /report 的 Spring Controller GET 方法:

 @RequestMapping(value = ["/report"], method = [RequestMethod.GET])
    @ResponseBody
    fun report(request: HttpServletRequest, response: HttpServletResponse) {

       
        // Call exportExcel to generate an EXCEL doc with data using jxl.Workbook
        val excelData = excel.exportExcel(myList)
        try {

            // Download the report.
            val reportName  = "ExcelReport.xls"
            response.contentType  = "application/vnd.ms-excel"
            response.setHeader("Content-disposition", "attachment; filename=$reportName")
            org.apache.commons.io.IOUtils.copy(excelData, response.outputStream)
            response.flushBuffer()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

此代码在 Kotlin 中实现 - 但您也可以在 Java 中轻松实现。

【讨论】:

  • 酷!伙计,正是我需要的。我已经成功处理了服务器端的部分,但我被如何手动导航到链接所困。你的window.location,是绝妙的简洁。请考虑在stackoverflow.com/questions/35206589/… 此处发布您的答案,因为您无需所有这些夸张的解决方法即可解决问题。
猜你喜欢
  • 2017-03-01
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 2015-05-24
  • 2017-09-17
  • 1970-01-01
  • 2018-02-10
  • 2014-05-14
相关资源
最近更新 更多