Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

java - How to use Wicket's DownloadLink with a file generated on the fly?

DownloadLink is nice and handy for creating a button/link for downloading a file, along these lines:

add(new DownloadLink("downloadButton", getReportFile(), "report.pdf"));

and

<input type="button" wicket:id="downloadButton" value="Download" />

However, I would like to trigger the generation of the file to download only when the button/link is clicked. In other words, upon click, I'd call a method that generates the file (a Pentaho report in our case), puts it in a temp place and returns a File pointing to it. Then I'd tell the DownloadLink to use that File. Question is, is this possible somehow?

Currently we have something like the code below, which works, but I'm interested in whether DownloadLink could be used instead.

add(new Link<Void>("downloadButton") {
  @Override
  public void onClick() {
    IResourceStream resourceStream = new AbstractResourceStreamWriter() {
      @Override 
      public void write(OutputStream output) {
        try {
          reportService.generateReport(output, report);
        } catch (IOException e) {
          // ...
        }
      }

      @Override
      public String getContentType() {                        
        return CONTENT_TYPE_PDF;
      }
    };

    getRequestCycle()
      .setRequestTarget(new ResourceStreamRequestTarget(resourceStream)
      .setFileName("report.pdf"));
  }
});

(Wicket 1.4.18, if it makes a difference.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Can't you use the constructor that takes a Model as argument? And make the Model generate the File in its getObject(). A LoadableDetachableModel is a good choice, given that load(), and therefore file generation, will be invoked only once.

If the file is to be freshly generated every time the link is clicked, use DownloadLink.setDeleteAfterDownload(true) to ensure the File is automatically deleted once it is served.

I don't use 1.4, but the source code in 1.3 shows that the File is retrieved by means of getModelObject() in the onClick() method of the Link.

IModel fileModel = new AbstractReadOnlyModel(){
    public Object getObject() { 
        return generateFile();
    }
};

DownloadLink link = new DownloadLink(linkId, fileModel, "report.pdf");

Source code of DownloadLink.onClick()

public void onClick()
{
    final File file = (File)getModelObject();
            ...
    IResourceStream resourceStream = new FileResourceStream(
            new org.apache.wicket.util.file.File(file));
    getRequestCycle().setRequestTarget(.../* uses resourceStream */...);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...