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....................
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);
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????