You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Philippe Frangioni <pf...@wimba.com> on 2007/04/30 21:32:39 UTC
Attachment and closed stream opening a zip file
Hi all,
I'm trying to send a zip file using attachments. In fact it works. My
only problem is that I can't open the zip file I saved on the server.
This is my code:
////////////////////////////////////////////////////////////////////////////////////////////////
InputStream is = null;
FileOutputStream os = null;
try
{
// File names vector
Vector fileInfo = (Vector) obj;
// Get all the attachments
AttachmentPart[] attachments = getMessageAttachments();
// Put the logic in a loop for totalAttachments for multiple
attachments.
int totalAttachments = attachments.length;
System.out.println("Total Attachments Received Are: " +
totalAttachments);
// Extract the first attachment. (Since in this case we have
only one attachment sent)
DataHandler dh = attachments[0].getDataHandler();
// Extract the file name of the first attachment.
String name = (String) fileInfo.get(0);
System.out.println("File received on server is: " + name);
// An input stream on the uploaded file
is = dh.getInputStream();
.....
String contentDirectory = "...............";
.....
File file = new File(contentDirectory, name);
os = new FileOutputStream(file);
int i = 0;
while (i != -1)
{
i = is.read();
os.write(i);
}
is.close();
os.close();
//dh = null;
System.out.println("file.getName() = " +
file.getName()); // -> return the good name
System.out.println("file.getPath() = " +
file.getPath()); // -> return the good path
System.out.println("file.isFile() = " +
file.isFile()); // -> return true
System.out.println("file.length() = " +
file.length()); // -> return the good length
System.out.println("file.canRead() = " +
file.canRead()); // -> return true
System.out.println("file.canWrite() = " +
file.canWrite()); // -> return true
FileInputStream fis = new FileInputStream(contentDirectory +
"/" + name);
CheckedInputStream checksum = new CheckedInputStream(fis,
new Adler32());
ZipInputStream zis = new ZipInputStream(new
BufferedInputStream(checksum));
ZipEntry zEntry;
while ((zEntry = zis.getNextEntry()) != null)
{
if (!zEntry.isDirectory())
{
//InputStream isz = zFile.getInputStream(zEntry);
OutputStream osz = new BufferedOutputStream(new
FileOutputStream(contentDirectory.getAbsolutePath() + "/" +
zEntry.getName()));
byte[] buffer = new byte[2048];
int len = 0;
int count;
//while ((len = isz.read(buffer)) >= 0)
while ((count = zis.read(buffer, 0, buffer.length))
!= -1)
{
osz.write(buffer, 0, count);
}
osz.close();
zis.close();
}
else
{
new File(contentDirectory.getAbsolutePath() + "/" +
zEntry.getName()).mkdir();
}
}
}
catch (Exception e)
{
status = "File Could Not Be Saved: " + e.getMessage();
System.out.println("In Impl: " + e);
System.out.println("Message: " + e.getMessage());
e.printStackTrace();
}
return status;
////////////////////////////////////////////////////////////////////////////////////////////////
I get an exception on line :
while ((zEntry = zis.getNextEntry()) != null)
The exception is:
java.io.IOException: Stream closed
at java.util.zip.ZipInputStream.ensureOpen(ZipInputStream.java:43)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:67)
at
com.wimba.cgng.bb.ws.cgimport.WebServiceImport.getSaveCourse(WebServiceImport.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)
at
org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)
at
org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
at
org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
at
org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
at
org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
I read here:
http://marc.info/?l=axis-user&m=109603822200704&w=2
that there is a solution, but I wanted to know if there is a "cleaner"
one.... I'm using Axis 1.4.
Thank you,
Phil
---------------------------------------------------------------------
To unsubscribe, e-mail: axis-user-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-user-help@ws.apache.org