Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

downloading a dll or a jar

 
Mohammad Farhat
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
i am trying to download dll files to the client's machine, so i simply created a URL object, read the bytes, and written them to the client's machine, yet they won't work
here is the code:

URLConnection conn = myURL.openConnection();
conn.connect();
DataInputStream data = new DataInputStream(new BufferedInputStream(conn.getInputStream()));
String line;
StringBuffer buf = new StringBuffer();
while ((line = data.readLine()) != null) {
buf.append(line + "\n");
}
data.close();
BufferedWriter out = new BufferedWriter(new FileWriter("c:\\clib_jiio.dll"));
out.write(buf.toString());
out.close();

does the transfer corrupt the file?
how can we download a dll/jar properly?
Thanks guys,
Mohammad
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DataInputStream is for reading Java primimitives from a stream, while Writers are for writing character-oriented data; both make assumptions about the data they're dealing with, and are inappropriate for reading/writing binary data. Use BufferedIn[Out]putStream instead.
 
Mohammad Farhat
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf,
you were correct about the classes, in fact they do change some bytes (i compared the files, and noticed the differences)
i used the following code instead:

try{
URL url = new URL("http://cstation6.compressus.com:8080/eStation/clibwrapper_jiio.jar");
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(stream);
FileOutputStream file = new FileOutputStream("d:\\version to replace\\clibwrapper_jiio.jar");
BufferedOutputStream out = new BufferedOutputStream(file);
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
out.flush();
}catch(Exception e){
System.out.println("Error:"+e.getMessage());
}

which i'm posting here if anyone would like to use it
btw, the code here i found on
http://www.jguru.com/faq/view.jsp?EID=13198
Best,
Mohammad
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic