Hi All,
I'm using the FileUpload component in a JSPDynPage and the htmlb component seems to work fine but I cannot read the file (InputStream). I get the following error(IOException): "InputStream does not contain a serialized object".
Please let me know what is wrong with my code. This is a part of the code I used:
public FileInputStream sourceFileInput; public ObjectInputStream input; FileUpload fu; fu = (FileUpload) this.getComponentByName("myFileUpload"); IFileParam fileParam = ((FileUpload) getComponentByName("myFileUpload")).getFile(); File f = fileParam.getFile(); file = fu.getFile().getFile(); absolutepath = fu.getFile().getFile().getAbsolutePath(); this.sourceFileInput = new FileInputStream(file); input = new ObjectInputStream(sourceFileInput);
The last line of code seems to generate te error.
Hi,
I used the FileUpload component to store files from the client to CM so when I read the file I was able to read from a FileInputStream straight into a byte array. I'm not sure if this will be any help to you since I don't know what you're doing with the file once uploaded but here is some code;
<code>
FileUpload fu = (FileUpload)this.getComponentByName("myFileUpload");
if(fu!=null){
__IFileParam fileParam = fu.getFile();
__if(fileParam!=null){
____File f = fileParam.getFile();
____if(f!=null){
______FileInputStream fis = new FileInputStream(f);
______byte[] bytes = new byte[(int)f.length()];
______fis.read(bytes);
______...
______...
____}
__}
}
</code>
This read the file contents into the byte array.
SinceI was writing the file as a resource into CM I was able then to write;
<code>
IContent aContent = new Content(new ByteArrayInputStream(bytes), fileParam.getContentType(), (int)f.length());
</code>
and then store it as a resource in the CM.
I hope it helps.
P.
Hi Raymond,
the problem is the last line. An ObjectInputStream is for reading serialized java objects, see http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectInputStream.html
So just use the sourceFileInput to read from, as Patrick has stated.
Hope it helps
Detlev
FileOutputStream has a write(byte[] b) method, give it a go and see how it turns out. I'm working on a different project (different place) just now and don't have access to that code so I can't run the test myself.
Let us know how it goes.
Add a comment