Skip to Content
avatar image
Former Member

downloading file

Hi,

I'm trying to download a zipfile from a server using the downloadUI element. I have a context element of a binary type linked to the downloadUI element. I try to fill it like:

try{

FileInputStream in = new FileInputStream(url);

ByteArrayOutputStream out = new ByteArrayOutputStream();

int length;

byte[] part = new byte [10 * 1024];

while ((length = in.read(part)) != -1) {

out.write(part, 0, length);

}

in.close();

IPrivateBasketView.IContextElement element = wdContext.currentContextElement();

element.setDownloadURL(out.toByteArray());

}catch(IOException e){

wdThis.wdGetTPD_DistributionController().reportApplicationException(e);

}

The url of the zipfile is created through a webservice and looks something like this:

http://mycompany.com:10010/TPD_Download/NewZip-20070115_121624-TPD?location=NewZip_tgGjKQIQJXjepwCE.zip

The problem is that i get the message that it cannot resolve the url (FileNotFoundException). The strange thing is that when i insert the url in the browser, it opens the zipfile but as text showing a screen with only wobbly characters instead giving the "save" dialog.

Can anyone tell me why i get the file not found and why it opens the zipfile as a textfile?

much thanks,

Hugo

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • Best Answer
    Jan 15, 2007 at 06:58 PM

    Hi Hugo,

    In order to read the content of a file through HTTP, you have to use a URLConnection instead; you can't use a FileInputStream.

    I'm not sure what your intention is, but it looks like you're trying to read the content of a file over HTTP on the server. You can't use a FileInputStream for that, you have to use a (Http)URLConnection instead. e.g.

    import java.io.*;
    import java.net.*;
    
      private static final int BUFFER_SIZE = 4 * 1024; // 4 KB
    
      // e.g. "http://www.iowaconsumercase.org/010807/PLEX_7264.pdf"
      private byte[] getContent(String url) throws IOException {
        InputStream in = new URL(url).openStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[BUFFER_SIZE];
    
        try {
          for(int i = 0; (i = in.read(buf)) != -1;) {
            out.write(buf, 0, i);
          }
        } finally {
          in.close();
        }
    
        return out.toByteArray();
      }
    

    Then if you want to allow your clients to download that file you can indeed use a FileDownload UI element.

    Kind regards,

    /Sigiswald

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 15, 2007 at 03:09 PM

    Hi,

    maybe you need to set mime type using IWDModifiableBinaryType, sth like:

    // get attribute info for context attribute 'FileResource'

    IWDAttributeInfo attributeInfo =

    wdContext.getNodeInfo().getAttribute(

    IPrivateBasketView.IContextElement.DOWNLOADURL);

    // create a modifiable binary type for the context attribute

    // which stores the MIME-object.

    IWDModifiableBinaryType binaryType =

    (IWDModifiableBinaryType) attributeInfo.getModifiableSimpleType();

    binaryType.setMimeType(...)

    binaryType.setFileName(...)

    //store file data

    etc...

    Regards,

    Ladislav

    Add comment
    10|10000 characters needed characters exceeded