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

解决方案 »

  1.   

    难道CSDN里只有问问题的.没有回答问题的?更没有跟贴的?
      

  2.   

    你A1.txt这个文件都不知道放在哪?
      

  3.   

    1.你访问的机器,有让你读写文件的权限吗,必须修改你访问机器的读写文件权限即修改java的本地安全策略
    2.你访问的文件的路径是否正确
      

  4.   

    http://127.0.0.1/jdk/是一个ftp服务吗?或者是一个http服务,你要保证服务是打开的,并监听正确的端口
      

  5.   

    输入http://127.0.0.1/A.java
    结果为:
    主机名:127.0.0.1
    端口:80
    文件类型为NULL.
    文件长度为-1