• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in download zip file using URLConnection class  RSS feed

 
sachin kataria
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.......
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your DataInputStream and PrintStream work with printable characters but your zip file has lots of unprintable bytes in it. Try working with the bare FileInputStream and OutputStream read(byte[]) and write(byte[]) methods.

Also, available() doesn't do what you really want. It only tells you if there are bytes available RIGHT NOW, not what might be available to a read that is willing to block for a while. The read() method will signal end of file by returning -1 bytes read.

See if those things get you closer.
 
sachin kataria
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya.....it's working . Thanks for help
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!