• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

very urgent - problem with the streams...is it a java bug  RSS feed

Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
I have a problem over here, in the transfer of file from my client machine to the server machine....I am zipping the file in the client machine and sending to the server machine....In the server machine I am trying to unzip it and write it over there....
For contacting my server, initially i am getting the authentication.....it is all correct....no problem with the authentication work...the problem over here is.............i am having 3 files to be transfered to my server machine...the first file is getting transfered....after that the command prompt is just standing idle....it is not even coming to the prompt state...it is not even thowing any errors in the server console or in the client machine....

Below is my complete code....
Client program: (simple java program)
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
import java.sql.*;
class testprogram
HttpURLConnection urlconnection = null;
public static void main(String args[])
String file1="F:/Domino/Data/Applications/Groupware/HP/input/Invcor02/param/new1.txt";
String file2="F:/Domino/Data/Applications/Groupware/HP/input/Invcor02/param/new2.txt";
String file3="F:/Domino/Data/Applications/Groupware/HP/input/Invcor02/param/new3.txt";
String name1="new1.txt";
String name2 ="new2.txt";
String name3="new3.txt";
testprogram tt=new testprogram();
catch(Exception e){}
public void zipper(String file,String name) throws Exception
ZipOutputStream zipoutputstream = new ZipOutputStream(urlconnection.getOutputStream());
ZipEntry zipentry = new ZipEntry(name);
byte bytearray[] = new byte[1024];
File filedir = new File(file);
FileInputStream fileinputstream = new FileInputStream(filedir);
BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream);
int length = 0;
while((length=bufferedinputstream.read(bytearray)) != -1)
System.out.println("I am here...");
InputStream in = urlconnection.getInputStream();
while (in.read()!=-1);
catch(Exception exp)
{System.out.println("I am from client: " + exp);}
/* to make the URL connection with the server - begin */
public void makeConnection()
/* Authentication work is done over here - begin */
String setcookie="";
String cookie="";
URL theurl = new URL("");
HttpURLConnection hurl = (HttpURLConnection)theurl.openConnection();
setcookie = hurl.getHeaderField ("set-cookie");
int index=setcookie.indexOf(";");
catch(Exception exp){exp.printStackTrace();}
/* Authentication work is done over here - end */
/* to make the URL connection with the server - begin */
URL serverURL = new URL("");
urlconnection= (HttpURLConnection)serverURL.openConnection();
catch(Exception exp)
{System.out.println("Client machine: Error in Making Connection" + exp );}
/* to make the URL connection with the server - end */
/* to disconnect the URL connection with the server - begin */
public void disconnectConnection()
catch(Exception exp)
{System.out.println("Client machine: Error gets fired while disconnecting the connection" + exp);}
/* to disconnect the URL connection with the server - end */
My Server side Program(servlet program)
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FileUploadServlet extends HttpServlet
public void init() throws ServletException
public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) throws ServletException, IOException
try{UnzipAndWriteToDir(httpservletrequest, httpservletresponse);}
catch(Exception exp){System.out.println("I am from server............" + exp);}
public void UnzipAndWriteToDir(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) throws Exception
javax.servlet.ServletInputStream servletinputstream = httpservletrequest.getInputStream();
ZipInputStream zipinputstream = new ZipInputStream(servletinputstream);
ZipEntry zipentry = null;
String directory="c:\\test";
File file = new File(directory);
catch(Exception exp)
{System.out.println("I am from Server: " + exp);}
zipentry = zipinputstream.getNextEntry();
if(zipentry != null)
int slash = zipentry.getName().lastIndexOf(File.separator) + 1;
String filename = zipentry.getName().substring(slash);
File file1 = new File(directory + File.separator + filename);
BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file1));
byte abyte0[] = new byte[1024];
for(int index = 0; (index = zipinputstream.read(abyte0)) > 0
bufferedoutputstream.write(abyte0, 0, index);
catch(IOException ioexception)
{System.out.println("IOException occured in the Server: " + ioexception);}
} // end of the method...
} // end of the class
P.S: Here, I am using the Domino Server, which is same as the Web Logic Server......Pl. post a reply....hanging over here for the past one week...any suggestion is highly appreciated....

After wasting around 5 hrs, i have found that the statement
"InputStream in = urlconnection.getInputStream();" (in the client program)
is making the comand prompt to be idle....is it a bug with sun java....or is there any solution for this......i mean the link with the server, is just as it is without disconnecting....
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just my guess answer based on my knowledge of HTTP protocol. You might have to test my suggestions. Basically in HTTP protocol, (in Java) you cannot get the OutputStream(for writing a file content through it) again, once you got the InputStream. In your program, you have made a single connection only and tried to reuse it again and again using the zipper method. You might have done this with optimized usage of resource (connection) in mind but this is not allowed in HTTP protocol. Another mistake in your code is that you are closing the zippedoutputstream. The zippedoutputstream is a wrapper around outputstream of the HTTPUrlconnection. So closing zippedoutputstream will automatically close the outputstream of HTTPUrlconnection also which means you cannot use the outputstream of HTTPUrlconnection again.
Ok. Let me stop throwing theories behind the problem with the program. I will suggest you a possible solution.
For every file that you send open a new httpurlconnection and close it as soon as you are finished sending the file. I am sure this will solve your problem. One more suggestion is to print the stack trace of the exception on the command line when you catch the exception. This will help you to identify any problem.
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks friend...thanks a lot!!! i did it finally...the changes i made for this to work are,
opening and closing the connection for each & every file....
second thing is, i am trying to send some dummy string from my server side, as follows...
String s="111";
ObjectOutputStream obj = new ObjectOutputStream(httpservletresponse.getOutputStream());
bye & have a nice day,
Sakthivel S.
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!