This discussion is archived
8 Replies Latest reply: Oct 31, 2006 6:09 PM by 807597 RSS

problem in download zip file using URLConnection class

807597 Newbie
Currently Being Moderated
hi,
in my application, client is sending request, and then server is creating one zip
file (which contains four files). whose size is around 2 kb.

after that server is sending that zip file to client, then there is problem.
client is getting that zip file. but size gets change and size is 1 kb.

my code is : servlet code :

     File tempFile = new File("OwnyITTemp.zip");
          
          if(tempFile.exists()){
          
               FileInputStream fStream = new FileInputStream(tempFile);
               DataInputStream datain = new DataInputStream(fStream);
          
               PrintStream ps = new PrintStream(response.getOutputStream());
                         
               while(datain.available() != 0){
                    ps.println(datain.readLine());
                    //System.out.println("data " + datain.readLine());
                    
               }
          
               ps.close();
                    
               datain.close();
               datain = null;
          
               fStream.close();
               fStream = null;
          }


and client side code is :

URLConnection con = null;

URL url = new URL(this.strURL);
               
con = url.openConnection();
               
con.setDoOutput(true);
                    
con.setDoInput(true);
                    
con.setRequestProperty("file-name", file.getName());
               
con.setRequestProperty("content-type", "binary/data");
InputStream in = con.getInputStream();
                         
FileOutputStream fout = new FileOutputStream("OwnyITRegistration.zip");
     
byte buffer1[] = new byte[1024*128];
int k=0;
               
     while( (k = in.read(buffer1)) != -1 ){
          System.out.println("buffer = " + buffer1.toString());
          fout.write(buffer1,0,k);
     }
               
     fout.close();
     fout = null;

so, what is the problem ?
pls , help me.......