package ip;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class studnetURL {
private static URL url = null;
private String toPath = null;
public void getURL(String spec) throws MalformedURLException {
url = new URL(spec);
}
public void getURL(String protocol ,String hostName, String file) throws MalformedURLException {
url = new URL(protocol ,hostName,file);
}
public void getURL(String protocol, String hostName, int port, String file) throws MalformedURLException {
url = new URL(protocol, hostName, port, file);
}
public void readURLFileToLocalhost(String toPath) {
panDuanToPathExists(new File(toPath));
this.toPath = toPath;
getURLFileToLocalhost();
}
/*]
* 判断保存网络文件的目录是否存在,不存在就创建
*/
private void panDuanToPathExists(File toPath) {
if(!toPath.exists()) {
toPath.mkdirs();
}
}
private void getURLFileToLocalhost() {
InputStreamReader isr = null;
FileWriter fw = null;
PrintWriter pw = null;
try {
String urlFileName = getUrlFileName(); //取得保存文件的名字
isr = new InputStreamReader(url.openStream());
fw = new FileWriter(new File(this.toPath).getPath()+"/"+urlFileName);
pw = new PrintWriter(fw);
int data = -1;
while((data = isr.read()) != -1) {
pw.write(data);
pw.flush();
}
pw.close();
fw.close();
isr.close();
} catch(IOException ioe) {
}
}
private String getUrlFileName() throws IOException {
if(url.getProtocol().equals("file")) {
return new File(url.getFile()).getName();
}
if(url.getProtocol().equals("http")) {
URLConnection urlConnection = url.openConnection();
String url = urlConnection.getURL().toString();
return url.substring(url.indexOf(".")+1)+".html";
}
return "www.txt";
}
public static void main(String [] args) throws IOException {
studnetURL urls = new studnetURL();
urls.getURL("http://www.hao123.com");
urls.readURLFileToLocalhost("D:/URl/content/");
urls.getUrlFileName();
//System.out.println(url.getPort());
}
}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class studnetURL {
private static URL url = null;
private String toPath = null;
public void getURL(String spec) throws MalformedURLException {
url = new URL(spec);
}
public void getURL(String protocol ,String hostName, String file) throws MalformedURLException {
url = new URL(protocol ,hostName,file);
}
public void getURL(String protocol, String hostName, int port, String file) throws MalformedURLException {
url = new URL(protocol, hostName, port, file);
}
public void readURLFileToLocalhost(String toPath) {
panDuanToPathExists(new File(toPath));
this.toPath = toPath;
getURLFileToLocalhost();
}
/*]
* 判断保存网络文件的目录是否存在,不存在就创建
*/
private void panDuanToPathExists(File toPath) {
if(!toPath.exists()) {
toPath.mkdirs();
}
}
private void getURLFileToLocalhost() {
InputStreamReader isr = null;
FileWriter fw = null;
PrintWriter pw = null;
try {
String urlFileName = getUrlFileName(); //取得保存文件的名字
isr = new InputStreamReader(url.openStream());
fw = new FileWriter(new File(this.toPath).getPath()+"/"+urlFileName);
pw = new PrintWriter(fw);
int data = -1;
while((data = isr.read()) != -1) {
pw.write(data);
pw.flush();
}
pw.close();
fw.close();
isr.close();
} catch(IOException ioe) {
}
}
private String getUrlFileName() throws IOException {
if(url.getProtocol().equals("file")) {
return new File(url.getFile()).getName();
}
if(url.getProtocol().equals("http")) {
URLConnection urlConnection = url.openConnection();
String url = urlConnection.getURL().toString();
return url.substring(url.indexOf(".")+1)+".html";
}
return "www.txt";
}
public static void main(String [] args) throws IOException {
studnetURL urls = new studnetURL();
urls.getURL("http://www.hao123.com");
urls.readURLFileToLocalhost("D:/URl/content/");
urls.getUrlFileName();
//System.out.println(url.getPort());
}
}