Starting with Java 7, you can download a file with built-in features as simple as
Files.copy(
new URL("http://example.com/update/PubApp_2.0.jar").openStream(),
Paths.get("C:/PubApp_2.0/update/lib/kitap.jar"));
// specify StandardCopyOption.REPLACE_EXISTING as 3rd argument to enable overwriting
for earlier versions, the solution from Java 1.4 to Java 6 is
try(
ReadableByteChannel in=Channels.newChannel(
new URL("http://example.com/update/PubApp_2.0.jar").openStream());
FileChannel out=new FileOutputStream(
"C:/PubApp_2.0/update/lib/kitap.jar").getChannel() ) {
out.transferFrom(in, 0, Long.MAX_VALUE);
}
This code transfers a URL content to a file without any 3rd party library. If it’s still slow, you know that it is not the additional library’s and most probably not Java’s fault. At least there’s nothing you could improve here. So then you should search the reason outside the JVM.
Answer from Holger on Stack OverflowStarting with Java 7, you can download a file with built-in features as simple as
Files.copy(
new URL("http://example.com/update/PubApp_2.0.jar").openStream(),
Paths.get("C:/PubApp_2.0/update/lib/kitap.jar"));
// specify StandardCopyOption.REPLACE_EXISTING as 3rd argument to enable overwriting
for earlier versions, the solution from Java 1.4 to Java 6 is
try(
ReadableByteChannel in=Channels.newChannel(
new URL("http://example.com/update/PubApp_2.0.jar").openStream());
FileChannel out=new FileOutputStream(
"C:/PubApp_2.0/update/lib/kitap.jar").getChannel() ) {
out.transferFrom(in, 0, Long.MAX_VALUE);
}
This code transfers a URL content to a file without any 3rd party library. If it’s still slow, you know that it is not the additional library’s and most probably not Java’s fault. At least there’s nothing you could improve here. So then you should search the reason outside the JVM.
Starting with Java 8 you can do it like this:
ReadableByteChannel readChannel = Channels.newChannel(new URL("http://example.com/update/PubApp_2.0.jar").openStream());
FileOutputStream fileOS = new FileOutputStream("C:\\PubApp_2.0\\update\\lib\\kitap.jar");
FileChannel writeChannel = fileOS.getChannel();
writeChannel
.transferFrom(readChannel, 0, Long.MAX_VALUE);
This should be fast!
try {
// Get the directory and iterate them to get file by file...
File file = new File(fileName);
if (!file.exists()) {
context.addMessage(new ErrorMessage("msg.file.notdownloaded"));
context.setForwardName("failure");
} else {
response.setContentType("APPLICATION/DOWNLOAD");
response.setHeader("Content-Disposition", "attachment"+
"filename=" + file.getName());
stream = new FileInputStream(file);
response.setContentLength(stream.available());
OutputStream os = response.getOutputStream();
os.close();
response.flushBuffer();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Hope you got some idea...
Use java.net.URL and java.net.URLConnection classes.
Give Java NIO a try:
URL website = new URL("http://www.website.com/information.asp");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
Using transferFrom() is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.
Check more about it here.
Note: The third parameter in transferFrom is the maximum number of bytes to transfer. Integer.MAX_VALUE will transfer at most 2^31 bytes, Long.MAX_VALUE will allow at most 2^63 bytes (larger than any file in existence).
Use Apache Commons IO. It is just one line of code:
FileUtils.copyURLToFile(URL, File)