问题
I am downloading a file using WGET through a java code, which takes around 10 mins to download 20 MB file. But on executing the wget download through command line, the same file gets downloaded in 7 seconds at 10MbPs speed. Does anyone know why this is? How can I improve my Java code?
Below is the code I have used to download a file using WGET. It takes around 10 minutes to download a 20 MB file. But when I run the wget command through the command line, it happens in seconds!!
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class WGETServer
{
public File download(URL sourceurl, String username, String password, String fileName)
{
//System.out.println("WGET download() is starting ...");
File file = null;
URLConnection urlConnection = null;
BufferedReader reader = null;
FileOutputStream outputStream = null;
try {
urlConnection = sourceurl.openConnection();
String userNameAndPassword = username +":"+ password;
String encoding = new sun.misc.BASE64Encoder().encode (userNameAndPassword.getBytes());
//The line which is supposed to add authorization data
urlConnection.setRequestProperty ("Authorization", "Basic " + encoding);
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
}
catch (IOException e) {
System.err.println("Internet connection failure or invalid Username/Password.");
return null;
}
try {
file = new File("file path");
outputStream = new FileOutputStream(file);
int character;
while((character = reader.read()) != -1)
{
outputStream.write(character);
}
outputStream.flush();
outputStream.close();
reader.close();
} catch (IOException e) {
System.err.println(e.getMessage());
return null;
}
System.out.println("downloading completed");
return file;
}
public static void main(String args[]) throws MalformedURLException
{
URL sourceurl = new URL("https:blablabla");
String username = "username";
String password = "password";
String filename = "filename";
WGETServer WGETdownload = new WGETServer();
WGETdownload.download(sourceurl, username, password, filename);
}
}
回答1:
Wrap the FileOutputStream with a BufferedOutputStream.
new BufferedOutputStream(new FileOutputStream(...))
Otherwise each and every character written is synchronized to disk by the underlying operating system which is a time consuming process. This is why buffering is so important.
回答2:
You have buffered reader (Good) but then you write the content char by char to the disk (BAD). That kills your performance. It's not the reading, it's the writing.
来源:https://stackoverflow.com/questions/9922758/wget-download-happens-faster-when-executed-through-command-line-and-slowly-when