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 OverflowDownload file from server in java - Stack Overflow
java - How to download a file from a server - Stack Overflow
java - Loop for downloading file from the server (in client-server application) - Code Review Stack Exchange
ftp - java code to download a file from server - Stack Overflow
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.
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.
I am creating a web application that generates a PDF. Locally it works as expected using
String homepath = System.getProperty("user.home");
when I deploy this app on the server and visit the website from my computer, it is trying to send the file to the user in the server......
how can I fix this it so that when the application running on the server sends the generated file to the user/clients downloads folder not my home directory on the server?