• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in download zip file using URLConnection class

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.......
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya.....it's working . Thanks for help
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic