www.pudn.com > htmlsaver.rar > SaveURLToFile.java
import java.net.*;
import java.io.*;
public class SaveURLToFile
{
protected Project prj;
protected URL url;
protected File file;
protected boolean isFinish;
public SaveURLToFile(Project prj, URLName u)
{
this.prj = prj;
isFinish = false;
try {
url = new URL(u.url);
}
catch (MalformedURLException e) {
return;
}
file = new File(new File(prj.mainDir), u.name);
if (file.exists())
file.delete();
try {
file.createNewFile();
}
catch (IOException e) {
return;
}
URLConnection uc;
try {
uc = url.openConnection();
}
catch (IOException e) {
return;
}
String type = uc.getContentType();
if (type.startsWith("text/html"))
isFinish = saveHTMLFile();
else
isFinish = saveBinaryFile();
}
public boolean isFinished()
{
return isFinish;
}
protected boolean saveBinaryFile()
{
try {
InputStream in = url.openStream();
OutputStream out = new FileOutputStream(file);
byte[] s = new byte[1024];
while (true)
{
int c = in.read(s, 0, 1024);
if (c == -1)
break;
out.write(s, 0, c);
out.flush();
}
in.close();
out.close();
}
catch (IOException e) {
return false;
}
return true;
}
protected boolean saveHTMLFile()
{
try {
InputStreamReader in = new InputStreamReader(url.openStream());
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(file));
FileURLChange change;
try {
change = new FileURLChange(url.toString());
}
catch (UnknowFileURL e) {
return false;
}
DealHTMLTagSaveURL save = new DealHTMLTagSaveURL(prj, change);
if (!DealHTMLStream.dealHTMLStream(in, out, save))
return false;
}
catch (IOException e) {
return false;
}
return true;
}
public static void main(String[] args)
{
Project project;
try {
project = new Project("f:\\other\\BTB.prj");
}
catch (Exception e) {
return;
}
URLName name = (URLName)project.names.elements().nextElement();
new SaveURLToFile(project, name);
}
}