【问题标题】:Wicket - How to download a file through its stream?Wicket - 如何通过其流下载文件?
【发布时间】:2012-11-19 20:23:55
【问题描述】:

我有我期望的一项非常常见的任务,但我在任何地方都找不到任何直接的答案,因此非常感谢您提供任何帮助。

短版: 如何从 SmbFile 的 inputStream 创建 IResourceStream?

长版: 在我的 Wicket 应用程序中,我让用户填写一份问卷,然后在他们正确完成后下载一个文件。该文件位于 Samba 服务器上,所以我使用的是 SmbFile,它运行良好,所以没有问题。我正在使用由 Sven Meier、Ernesto Barreiro 和 Jordi Deu-Pons 编写的 AJAXDownload,因为我需要我的按钮具有 Ajax 功能来检查表单是否正确填写,然后开始下载。当我开始使用计算机上的文件时,我可以简单地使用

final AJAXDownload download = new AJAXDownload()
        {
            @Override
            protected IResourceStream getResourceStream()
            {                   
                IResourceStream resStream = new FileResourceStream(file);

                return resStream;
            }
            @Override
            protected String getFileName() {
                return fileName;
            }
        };
        form.add(download);

但是现在我通过 JCIFS 使用 SmbFile,我不能只创建一个新的 FileResourceStream。我发现我可以使用 java.io.File.createTempFile 从 smbFile 的 inputStream 中创建一个临时文件,然后像这样使用临时文件:

protected IResourceStream getResourceStream()
            {                   
                java.io.File tempFile = null;
                try {
                    tempFile = File.createTempFile(smbFile.getName(), smbFile.getContentType());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    Files.writeTo(tempFile, smbFile.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                IResourceStream resStream = new FileResourceStream(tempFile);
                return resStream;
            }

这适用于小文件,但我也会有一些相当大的文件,因为它会在发送文件之前制作文件的临时副本,因此需要很长时间并且设计不佳。 关于如何在 wicket 应用程序中下载我的 smbFile 的任何想法?

编辑: 感谢 Joop Eggen 和 Kent Ka lok Tong 的“Enjoying Web Development with Wicket”一书,我有以下解决方案,以防其他人遇到类似情况:

final AJAXDownload download = new AJAXDownload()
        {
            @Override
            protected IResourceStream getResourceStream()
            {                   
                IResourceStream resStream = new AbstractResourceStream() {
                    InputStream inStream;
                    public InputStream getInputStream() throws ResourceStreamNotFoundException{
                        try {
                            inStream = smbFile.getInputStream();
                        } catch (IOException e) {                               
                            e.printStackTrace();
                        }
                        return inStream;
                    }
                    public void close() throws IOException {
                        inStream.close();
                    }
                    public String getContentType() {
                        return smbFile.getContentType();
                    }
                };
                return resStream;
            }
            @Override
            protected String getFileName() {
                return smbFileName;
            }
        };
        form.add(download);

【问题讨论】:

    标签: java download wicket samba


    【解决方案1】:

    您应该派生一个扩展 AbstractResourceStream 的 SmbResourceStream,并通过覆盖 getInputStreamclose 来实现提供用于读取的 InputStream。你已经有smbFile.getInputStream()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2014-09-04
      • 2020-12-19
      相关资源
      最近更新 更多