How to download a file from an url?

Hello,

in relation with
thread
I try to download a file from a server, which is not the vaadin server. I am not very familiar with streams and so on and I do not know how to do this. According the
OnDemandFileDownloader
my code looks something like this:

private class BaseOnDemandStreamResource implements OnDemandStreamResource { @Override public InputStream getStream() { try { return new FileInputStream( new File( getFilename() ) ); } catch ( FileNotFoundException fnfe ) { throw new RuntimeException( fnfe ); } } ... } The "Problem"is that
getFilename()
is not a path on the vaadin server (local file system), it looks something like this: “http://MyServer/tempDok/20140218_153312154.pdf”. Therefore
new File()
throws a
FileNotFoundException
. Which input stream should I use? Or do I have to get first the file via ftp, place it on the vaadin server (local file system) and than I can use the
FileInputStream
?

Hi Philipp,

to download a file you need to use URL.openStream().

Your code would look like:

private class BaseOnDemandStreamResource
implements OnDemandStreamResource
{
    @Override
    public InputStream getStream()
    {
        try
        {
            return new URL("http://www.someurl.de/a.txt").openStream();
        }
        catch ( IOException e )
        {
            throw new RuntimeException( e );
        }
    }
...
}

Thanks, sometimes it can be so easy…