The Artima Developer Community
Sponsored Link

Java Answers Forum
problem with the file transfer...

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
sakthivel

Posts: 7
Nickname: sakthivel
Registered: Oct, 2002

problem with the file transfer... Posted: Nov 9, 2002 5:44 AM
Reply to this message Reply
Advertisement
Dear All,
I am trying to zip a file in the client side and sending it to the server side machine......there, i am trying to unzip it and again writting it...for this i am using two programs at both the side.................................
In the client program, after opening all the necessary streams and all formalities, i am using the code...

"InputStream in = urlconnection.getInputStream();
while (in.read()!=-1);"

only if i use the above code, the zipped file is transfered to my server machine...but in this case, the link with the server is not getting disconnected...i mean the command prompt remains alive, without stopping...what i inferred is, my control is got into the above said while loop...indefinitely...
so i tried of removing the above said looop.....in this case, the zipped file is NOT getting written in my server machine....
what to do???
any suggestions please...
sakthivel s.

snippet code for ur reference...
************************
Client side
********
try
{
ZipOutputStream zipoutputstream = new ZipOutputStream(urlconnection.getOutputStream());
ZipEntry zipentry = new ZipEntry(filename);
zipentry.setMethod(ZipEntry.DEFLATED);
zipoutputstream.putN extEntry(zipentry);
byte bytearray[] = new byte[1024];
File file = new File(filenamedir);
FileInputStream fileinputstream = new FileInputStream(file);
BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream);
int length = 0;
while((length=bufferedinputstream.read(bytearray)) != -1)
{
zipoutputstream.write(bytearray,0,length);
}

fileinputstream.close();
bu fferedinputstream.close();
zipoutputstream.flush();
zipoutputstream.finish();
zi poutputstream.closeEntry();
zipoutputstream.close();

InputStream in = urlconnection.getInputStream();
while (in.read()!=-1); // the said while loop....................

System.runFinalization();
urlconnection.getInputStream().close();
urlconnecti on.disconnect();

the way of connecting witht the server : (just a snippet of the code)

URL serverURL = new URL("http://192.168.10.55:8001/servlet/uploadservlet");
HttpURLConnection.setFo llowRedirects(true);
urlconnection= (HttpURLConnection)serverURL.openConnection();
urlconnection.setDoOutput(true);
urlconnection.setRequestMethod("POST");
urlconnection.setDoOutput(true);
urlcon nection.setDoInput(true);
urlconnection.setUseCaches(false);
urlconnection.setAl lowUserInteraction(true);
urlconnection.setRequestProperty("Cookie",cookie);
url connection.connect();



Server Side:
*********
javax.servlet.ServletInputStream servletinputstream = httpservletrequest.getInputStream();
ZipInputStream zipinputstream = new ZipInputStream(servletinputstream);
ZipEntry zipentry = null;
String directory="c:\\test"; // the unzipped file should be written to this directory...
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);
FileOutputStream fostream = new FileOutputStream(file1);
BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(fostream);
byte abyte0[] = new byte[1024];
for(int index = 0; (index = zipinputstream.read(abyte0)) > 0;)
bufferedoutputstream.write(abyte0, 0, index);


servletinputstream.close();
bufferedoutputstream.flush();
bufferedout putstream.close();
zipinputstream.closeEntry();
zipinputstream.close();
fostream .flush();
fostream.close();
}
}
catch(IOException ioexception)
{System.out.println("IOException occured in the Server: " + ioexception);ioexception.printStackTrace();}


P.S: I am not getting any error in the server side or cleint side...the only problem is, the command prompt(where i am running my cleint program) is getting standing indefinitely...i have to use control-c to come to the command prompt again...this is because of the while looop said in the client machine....if i remove it...the file is not gettting transfered...what to do????

Topic: A problem with swings while running Previous Topic   Next Topic Topic: cookies and java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use