import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ReadInternetFile
{
public ReadInternetFile()
{
// TODO Auto-generated constructor stub
}
public URL getUrl()
{
return this.url;
}
public void setUrl() throws MalformedURLException
{
String url = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
url = br.readLine();
this.url = new URL(url);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void setFileSaveName(URL url)
{
String file = url.getFile();
this.fileName = file.substring(file.lastIndexOf("/") + 1);
}
private String getFileName()
{
return this.fileName;
}
private URL url = null;
private static final String fileSavePath = "E:\\myftp\\download\\";
private String fileName = null;
public static void main(String []args)
{
Long start = System.currentTimeMillis();
System.out.println("please input the url of the file:");
ReadInternetFile fileReader = new ReadInternetFile();
try
{
fileReader.setUrl();
URL fileUrl = fileReader.getUrl();
fileReader.setFileSaveName(fileUrl);
HttpURLConnection conn = (HttpURLConnection)fileUrl.openConnection();
conn.setConnectTimeout(5000);
DataInputStream din = new DataInputStream(conn.getInputStream());
String savedFileName = fileReader.getFileName();
DataOutputStream dout = new DataOutputStream(new FileOutputStream(fileSavePath + savedFileName));
BufferedInputStream bin = new BufferedInputStream(din,4096);
BufferedOutputStream bout = new BufferedOutputStream(dout);
byte [] buffer = new byte[4*1024];
int count = 0;
while ((count = bin.read(buffer)) > 0)
{
//如果使用bout.write(buffer)方法图片会失真,而且下载的速度快慢与否与网络环境有很大的关系,
// dout.write(buffer, 0, count);
bout.write(buffer,0,count);
}
bout.close();
bin.close();
conn.disconnect();
System.out.println("file " + savedFileName + " successfully downloaded !");
System.out.println("Time used:" + (System.currentTimeMillis() - start));
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ReadInternetFile
{
public ReadInternetFile()
{
// TODO Auto-generated constructor stub
}
public URL getUrl()
{
return this.url;
}
public void setUrl() throws MalformedURLException
{
String url = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
url = br.readLine();
this.url = new URL(url);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void setFileSaveName(URL url)
{
String file = url.getFile();
this.fileName = file.substring(file.lastIndexOf("/") + 1);
}
private String getFileName()
{
return this.fileName;
}
private URL url = null;
private static final String fileSavePath = "E:\\myftp\\download\\";
private String fileName = null;
public static void main(String []args)
{
Long start = System.currentTimeMillis();
System.out.println("please input the url of the file:");
ReadInternetFile fileReader = new ReadInternetFile();
try
{
fileReader.setUrl();
URL fileUrl = fileReader.getUrl();
fileReader.setFileSaveName(fileUrl);
HttpURLConnection conn = (HttpURLConnection)fileUrl.openConnection();
conn.setConnectTimeout(5000);
DataInputStream din = new DataInputStream(conn.getInputStream());
String savedFileName = fileReader.getFileName();
DataOutputStream dout = new DataOutputStream(new FileOutputStream(fileSavePath + savedFileName));
BufferedInputStream bin = new BufferedInputStream(din,4096);
BufferedOutputStream bout = new BufferedOutputStream(dout);
byte [] buffer = new byte[4*1024];
int count = 0;
while ((count = bin.read(buffer)) > 0)
{
//如果使用bout.write(buffer)方法图片会失真,而且下载的速度快慢与否与网络环境有很大的关系,
// dout.write(buffer, 0, count);
bout.write(buffer,0,count);
}
bout.close();
bin.close();
conn.disconnect();
System.out.println("file " + savedFileName + " successfully downloaded !");
System.out.println("Time used:" + (System.currentTimeMillis() - start));
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}