cancel
Showing results for 
Search instead for 
Did you mean: 

downloading file

Former Member
0 Kudos

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....

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

Accepted Solutions (1)

Accepted Solutions (1)

Sigiswald
Contributor
0 Kudos

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

Former Member
0 Kudos

Hi Sigiswald,

That was exactly what i wanted to do...download a zipfile through http and stuff it into a binary context element, that it is available for the FileDownload UI element.

The code seems to work but when i put the binary stuff into the context element for the DownloadFile UI element and it tries to render it, i get the following exception:

Processing HTTP request to servlet [dispatcher] finished with error. The error is: java.lang.IllegalArgumentException

at com.sap.dictionary.runtime.DdTypeBinary.format(DdTypeBinary.java:62)

at com.sap.tc.webdynpro.clientserver.data.DataContainer.doFormat(DataContainer.java:1388)

at com.sap.tc.webdynpro.clientserver.data.DataContainer.getAndFormat(DataContainer.java:1082)

at com.sap.tc.webdynpro.clientserver.data.DataContainer.getAndFormat(DataContainer.java:1054)

at com.sap.tc.webdynpro.clientimpl.html.uielib.standard.uradapter.FileDownloadAdapter.getReference(FileDownloadAdapter.java:260)

at com.sap.tc.ur.renderer.ie6.LinkRenderer.render(LinkRenderer.java:42)

at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:384)

at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:128)

at com.sap.tc.ur.renderer.ie6.FlowLayoutRenderer.renderFlowLayoutItemFragment(FlowLayoutRenderer.java:255)

at com.sap.tc.ur.renderer.ie6.FlowLayoutRenderer.renderFlowLayoutFragment(FlowLayoutRenderer.java:192)

at com.sap.tc.ur.renderer.ie6.FlowLayoutRenderer.render(FlowLayoutRenderer.java:38)

at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:384)

at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:128)

at com.sap.tc.ur.renderer.ie6.ScrollContainerRenderer.renderScrollContainerFragment(ScrollContainerRenderer.java:540)

at com.sap.tc.ur.renderer.ie6.ScrollContainerRenderer.render(ScrollContainerRenderer.java:58)

at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:384)

an illegalargumentexception....cant see where it comes from...anyone a clue?

regards,

Hugo

Former Member
0 Kudos

Hugo,

Make sure that you are using code suggested by Ladislav:


IWDModifiableBinaryType binaryType =
(IWDModifiableBinaryType) attributeInfo.getModifiableSimpleType();

This code is critical, it should be placed to wdDoInit of controller where binary attribute is defined.

Valery Silaev

SaM Solutions

http://www.sam-solutions.net

Sigiswald
Contributor
0 Kudos

Indeed, you should call this method in the wdDoInit method of your view controller:


  private IWDModifiableBinaryType getFileBinaryType() {
    return (IWDModifiableBinaryType) wdContext
      .getNodeInfo()
      .getAttribute(IPrivateAttachmentView.IContextElement.FILE)
      .getModifiableSimpleType();
  }

if you have, in a view with the name "AttachmentView", a "File" attribute of type binary on the root node.

BTW, I think that if the binary File attribute is defined in your component controller and you use context mapping to map it to the view context, it won't work either. At least that's what I experienced and I think it has something to do with the fact that the metadata for the binary attribute is contained at node level and not at element level. At least for NW2004. I'm not 100% sure though...

Kind regards,

/Sigiswald

BeGanz
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hallo,

the ModifiableBinaryType modfication of a context attribute (AttributeInfo) should be implemented in the controller which comprises the data context. That's not the responsibilty of a controller which maps its context to the data context in another controller.

The ModifiableBinaryType modfication is no longer needed within SAP NetWeaver 04s. I will publish a new tutorial on File Upload/Download and on using on-demand streams within tables based on SAP NetWeaver 04s soon.

Regards, Bertram

Former Member
0 Kudos

Valery,

Thanks, that was needed to make it work.

Cheers all,

Hugo

Former Member
0 Kudos

thanks all

Message was edited by:

Hugo Hendriks

Answers (1)

Answers (1)

former_member188498
Active Participant
0 Kudos

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

Former Member
0 Kudos

Hi Ladislav,

Seems like thats the problem for opening it. It was a problem when creating the zip...this works now but is still get the filenotfound. It seems when creating a File with the URL, it removes the / of the http:// and replaces all forward slashes by backslashes. Thats the reason for the file not found. I'm know looking how to input a URL into the FileInputStream....any ideas?

regards,

Hugo

former_member188498
Active Participant
0 Kudos

Hi,

maybe try java.net.URL class?

Regards,

Ladislav