aspose file tools*
The moose likes I/O and Streams and the fly likes problem with the urlconnection getInputStream() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem with the urlconnection getInputStream()" Watch "problem with the urlconnection getInputStream()" New topic
Author

problem with the urlconnection getInputStream()

sakthivel shanmugam
Greenhorn

Joined: Nov 10, 2002
Posts: 4
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();
try
{
tt.makeConnection();
tt.zipper(file1,name1);
tt.zipper(file2,name2);
tt.zipper(file3,name3);
tt.disconnectConnection();
}
catch(Exception e){}
}
public void zipper(String file,String name) throws Exception
{
try
{
ZipOutputStream zipoutputstream = new ZipOutputStream(urlconnection.getOutputStream());
ZipEntry zipentry = new ZipEntry(name);
zipentry.setMethod(ZipEntry.DEFLATED);
zipoutputstream.putNextEntry(zipentry);
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)
{
zipoutputstream.write(bytearray,0,length);
}
fileinputstream.close();
bufferedinputstream.close();
zipoutputstream.flush();
zipoutputstream.finish();
zipoutputstream.closeEntry();
zipoutputstream.close();
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="";
try
{
URL theurl = new URL("http://192.168.10.55:8001/names.nsf?login&username=sakthivel&password=12345");
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection hurl = (HttpURLConnection)theurl.openConnection();
hurl.connect();
setcookie = hurl.getHeaderField ("set-cookie");
int index=setcookie.indexOf(";");
cookie=(setcookie.substring(0,index)).trim();
hurl.disconnect();
hurl=null;
theurl=null;
}
catch(Exception exp){exp.printStackTrace();}
/* Authentication work is done over here - end */
/* to make the URL connection with the server - begin */
try
{
URL serverURL = new URL("http://192.168.10.55:8001/servlet/FileUploadServlet");
urlconnection= (HttpURLConnection)serverURL.openConnection();
urlconnection.setDoOutput(true);
urlconnection.setRequestMethod("POST");
urlconnection.setRequestProperty("Cookie",cookie);
}
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()
{
try
{
System.runFinalization();
urlconnection.disconnect();
urlconnection.getInputStream().close();
}
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";
try
{
File file = new File(directory);
if(!file.exists())
file.mkdirs();
}
catch(Exception exp)
{System.out.println("I am from Server: " + exp);}
try
{
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);
bufferedoutputstream.flush();
bufferedoutputstream.close();
zipinputstream.closeEntry();
zipinputstream.close();
}
}
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....
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
It looks like you are doing this:
Client: writes one zip file
Server: reads one zip file
Client: waits for server to return something before writing a second zip to the same stream
Server: doens't return anything
Client: still waiting..... waiting.... waiting....
You probably want to do one of two things:
Have your client write all three zips to the output stream and have the servlet read all three, then maybe reply (and the client can then read the reply).
Or
Have the client write one stream and the server read one, then handle the replies. Connect again for the second file, etc.
The connection to a servlet is not bi-directional, it is request/reply. So as soon as you getInputStream (on the client side) you have closed the "upstream/request" output stream.
And BTW: Domino != WebLogic
 
jQuery in Action, 2nd edition
 
subject: problem with the urlconnection getInputStream()
 
Similar Threads
Error sending files greater than 4MB please help urgent!!!!!
I/O streams - problem
Server Authentication - pl. help me yaar
Uploading Image from Java Desktop App to Server
very urgent - problem with the streams...is it a java bug