Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在其他wicket页面下载相同的excel文件_Java_Spring_Wicket_Wicket 8 - Fatal编程技术网

Java 在其他wicket页面下载相同的excel文件

Java 在其他wicket页面下载相同的excel文件,java,spring,wicket,wicket-8,Java,Spring,Wicket,Wicket 8,我已将wicket 1.x迁移到wicket 8.x 我为excel文件下载添加了以下代码,但在excel下载的所有其他页面中获得了第一个下载的文件 ResourceLink<Object> excelLink = new ResourceLink<>("excel", new ResourceReference("downloadExcel") { private static final long se

我已将wicket 1.x迁移到wicket 8.x

我为excel文件下载添加了以下代码,但在excel下载的所有其他页面中获得了第一个下载的文件

ResourceLink<Object> excelLink =  new ResourceLink<>("excel", new ResourceReference("downloadExcel") {
            private static final long serialVersionUID = 1L;

            @Override
            public IResource getResource() {
                byte [] exBytes = null;
                try {
                    exBytes = new byte[0]; // Some excel file into byte format
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return new ByteArrayResource(fileFormat.getContextType(), exBytes, fileName);
            }
        });
excelLink.setOutputMarkupId(true);
excelLink.add(new Label("excelLabel", new ResourceModel("excelLabel")));
return excelLink;
ResourceLink excelLink=new ResourceLink(“excel”),new ResourceReference(“下载excel”){
私有静态最终长serialVersionUID=1L;
@凌驾
公共IResource getResource(){
字节[]exBytes=null;
试一试{
exBytes=新字节[0];//将某些excel文件转换为字节格式
}捕获(例外e){
e、 printStackTrace();
}
返回新的ByteArrayResource(fileFormat.getContextType(),exBytes,fileName);
}
});
setOutputMarkupId(true);
添加(新标签(“excelLabel”)、新资源模型(“excelLabel”);
返回excelLink;
我在所有其他页面中使用相同的excel下载逻辑,在所有页面中使用相同的ResourceLink Id“excel”,在应用程序所有页面中使用相同名称的所有excel文件

如果它正在维护缓存,那么如何清除缓存以在每个页面中下载正确的excel文件


如果有人能帮我解决这个问题,请告诉我,这将是一个值得注意的问题。

要禁用此资源的缓存,您可以执行以下操作:

 return new ByteArrayResource(fileFormat.getContextType(), exBytes, fileName) {
   @Override 
   protected void configureCache(final ResourceResponse data, final Attributes attributes) {
       data.setCacheDuration(Duration.NONE);
       super.configureCache(data, attributes);
   }
 };

上面的代码可以很好地返回excel文件。在这里,我发现了excel文件名的问题,其中excel文件名在我的应用程序的所有页面中都是相同的,因为它在之前的Wicket版本中实现过,并且以前工作正常。但是在wicket从1.x版迁移到8.x版之后,它会在单击下载excel文件时返回旧的下载excel文件。因此,现在我在文件名中添加了时间戳,以便在每个页面上保留不同的文件名供excel下载

ResourceLink<Object> excelLink =  new ResourceLink<>("excel", new ResourceReference("downloadExcel") {
            private static final long serialVersionUID = 1L;

            @Override
            public IResource getResource() {
                byte [] exBytes = null;
                try {
                    exBytes = new byte[0]; // Some excel file into byte format
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return new ByteArrayResource(fileFormat.getContextType(), exBytes, fileName);
            }
        });
excelLink.setOutputMarkupId(true);
excelLink.add(new Label("excelLabel", new ResourceModel("excelLabel")));
return excelLink;
示例:在文件名为“UserData.xls”之前,现在在文件名“UserData\u 1002021\u 021311.xls”(UserData\u ddmmyyy\u HHmmss.xls)中添加时间戳之后。这解决了我的用例问题


我希望它能帮助同样面临同样问题的人。

您试过调试getResource()吗?是否每次单击“下载”按钮时都会执行?如果没有尝试在两次下载之间清理浏览器缓存?是的,每次单击“下载”按钮时都会执行。我尝试过清除缓存,但我先下载的文件中只有同一个文件会在其他页面上的其他下载点击中得到下载。在我的案例中,其他页面中的文件名也是一样的。我尝试使用上述代码,但对我的案例无效。在我的情况下,其他页面中的文件名也是相同的。如果这是您的要求,您可以使用不同的文件名。我的代码只是设置了正确的响应头来禁用缓存。您可能需要清除在“我的代码”之前已填充的浏览器缓存。我已在文件名中添加了时间戳,以便在每次单击下载时区分文件名。这解决了我的问题,因为所有页面中的文件名都是相同的。谢谢。嗨,马丁,请为这个问题投票。希望这能帮助其他人解决他们的问题。