on 01-15-2007 12:18 PM
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:
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.