my dog learned polymorphism
The moose likes Sockets and Internet Protocols and the fly likes I/O streams - problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "I/O streams - problem" Watch "I/O streams - problem" New topic
Author

I/O streams - problem

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....
Lewin Chan
Ranch Hand

Joined: Oct 10, 2001
Posts: 214
It's confusing w/o the code tags, but I believe the problem to be in the zipper method where you are doing

This, most likely, is closing the underlying socket connection, which means that you subsequent calls to tt.zipper() will fail.
Maybe you could try a tt.makeConnection() each time, possibly bypassing the authentication stage because you already got a cookie.

L


I have no java certifications. This makes me a bad programmer. Ignore my post.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: I/O streams - problem
 
Similar Threads
Uploading Image from Java Desktop App to Server
very urgent - problem with the streams...is it a java bug
problem with the urlconnection getInputStream()
Server Authentication - pl. help me yaar
Error sending files greater than 4MB please help urgent!!!!!