package download;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class DownLoad {
static Frame f=new Frame("DownLoad");
static Label l=new Label("请输入URL地址: ");
static Panel p=new Panel();
static List ta=new List(6);
static TextField tf=new TextField("http://127.0.0.1",30);
//static TextArea ta=new TextArea(30,40);
static Button b=new Button("DownLoad"); public static void main(String[] args) {
p.add(l);
p.add(tf);
f.add(p,BorderLayout.NORTH);
f.add(ta,"Center");
f.add(b,"South");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str=tf.getText();
try {
URL url=new URL(str);
URLConnection uc=url.openConnection();
ta.add("主机名: "+url.getHost());
ta.add("使用端口: "+url.getDefaultPort());
ta.add("文件类型: "+uc.getContentType());
ta.add("文件长度: "+uc.getContentLength());
InputStream is=uc.getInputStream();
FileOutputStream fos=new FileOutputStream("f:\\1.html");
//BufferedOutputStream bos=new BufferedOutputStream(fos);
DataOutputStream dos=new DataOutputStream(fos);
int data=0;
while((data=is.read())!=-1)
{
dos.write(data);
}
dos.close();
is.close();
}
catch (Exception ex) {
ex.printStackTrace();
} }
});
f.setSize(400,300);
f.setLocation(150,150);
f.setVisible(true);
}
}
文件类型为NULL.
文件长度为-1
这是怎么回事.我输入的是http://127.0.0.1/jdk/A1.txt
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class DownLoad {
static Frame f=new Frame("DownLoad");
static Label l=new Label("请输入URL地址: ");
static Panel p=new Panel();
static List ta=new List(6);
static TextField tf=new TextField("http://127.0.0.1",30);
//static TextArea ta=new TextArea(30,40);
static Button b=new Button("DownLoad"); public static void main(String[] args) {
p.add(l);
p.add(tf);
f.add(p,BorderLayout.NORTH);
f.add(ta,"Center");
f.add(b,"South");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str=tf.getText();
try {
URL url=new URL(str);
URLConnection uc=url.openConnection();
ta.add("主机名: "+url.getHost());
ta.add("使用端口: "+url.getDefaultPort());
ta.add("文件类型: "+uc.getContentType());
ta.add("文件长度: "+uc.getContentLength());
InputStream is=uc.getInputStream();
FileOutputStream fos=new FileOutputStream("f:\\1.html");
//BufferedOutputStream bos=new BufferedOutputStream(fos);
DataOutputStream dos=new DataOutputStream(fos);
int data=0;
while((data=is.read())!=-1)
{
dos.write(data);
}
dos.close();
is.close();
}
catch (Exception ex) {
ex.printStackTrace();
} }
});
f.setSize(400,300);
f.setLocation(150,150);
f.setVisible(true);
}
}
文件类型为NULL.
文件长度为-1
这是怎么回事.我输入的是http://127.0.0.1/jdk/A1.txt
2.你访问的文件的路径是否正确
结果为:
主机名:127.0.0.1
端口:80
文件类型为NULL.
文件长度为-1