aspose file tools*
The moose likes I/O and Streams and the fly likes downloading a dll or a jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "downloading a dll or a jar" Watch "downloading a dll or a jar" New topic
Author

downloading a dll or a jar

Mohammad Farhat
Ranch Hand

Joined: Nov 17, 2005
Posts: 51
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
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
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

Joined: Nov 17, 2005
Posts: 51
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: downloading a dll or a jar