Reputation: 1177
so, I have this set of code to download a file, it seems to think it works, but the downloaded file is corrupt.
try{ java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL(args[1]).openStream()); java.io.FileOutputStream fos = new java.io.FileOutputStream(args[2]); java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024); byte data[] = new byte[1024]; int count; while( (count = in.read(data,0,1024)) != -1){ bout.write(data,0,count); } fos.flush(); fos.close(); } catch(Exception e){ }
args[1] is the URL
Upvotes: 0
Views: 3332
Reputation: 7706
Empty catch blocks are usually not a good idea and can hide symptoms.
Change:
catch(Exception e){
}
To:
catch(Exception e){
e.printStackTrace();
}
Upvotes: 1
Reputation: 1964
After researching this simple code working fine for windows system while for mac or other you need to change:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.MalformedInputException;
import java.util.Scanner;
import org.apache.commons.io.FileUtils;
/**
*
* @author aleem
*/
public class Get {
public static void main (String[] args) throws IOException, InterruptedException {
/**Change the url with your jar file url**/
String fileUrl = "http://elambak.com/Supported.jar";
/**Assign any name to this newly downloaded file with destination location if required **/
String destinationFile = "abc1.jar";
saveFile(fileUrl, destinationFile);
}
public static void saveFile(String fileUrl, String destinationFile) throws IOException {
int BUFFER_SIZE = 4096;
URL url = new URL(fileUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
/** This user agent is using for windows for other OS need to change **/
httpConn.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
int responseCode = httpConn.getResponseCode();
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
InputStream is = httpConn.getInputStream();
OutputStream os = new FileOutputStream(destinationFile);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
}
}
Upvotes: 0
Reputation: 81684
The problem is that you're flushing and closing the FileOutputStream
, and that's leaving some bytes behind in the BufferedOutputStream
's buffer; your files will be missing somewhere between 0 and 1024 bytes at the end. Change both of those calls to flush and close bout
instead, and your problem will be fixed.
As an aside, this:
catch(Exception e){
}
is a programming sin of the highest order, and the source of many hard-to-diagnose problems -- not this one, this time, but it cold have been. If something is going wrong during your transfer, the error message will be silently ignored. Don't do this -- don't you ever, ever do this.
Upvotes: 3
Reputation: 121659
Sure you can download a .jar.
If you successfully download an .exe (which is nothing more than a binary stream of bytes, of arbitrary length), then I have no idea why you're having problems with a .jar (which is also just a binary stream). The two should be 100%$ equivalent, from the perspective of your data stream. My guess is that the .exe just seemed to work ... but you actually failed to copy some or all of the file.
Finally, it's worth noting that a ".jar" file is actually a ".zip" file. You can wrap your stream in a built-in Zip class, and literally interpet the .jar contents as you read the file. If you wish to do so.
Suggestion:
Check the file size and/or a checksum of the .exe's you copied. Try a couple of different sized .exe's. If you're satisified that the .exe is copying correctly, I assure you that the .jar should work, too.
Upvotes: 0