首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

这个程序运行结果为什么总不对呢,该如何处理

2012-04-11 
这个程序运行结果为什么总不对呢packagedownload/***p Title:/p*p Description:/p*p Copyright

这个程序运行结果为什么总不对呢
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.你访问的机器,有让你读写文件的权限吗,必须修改你访问机器的读写文件权限即修改java的本地安全策略
2.你访问的文件的路径是否正确
[解决办法]
http://127.0.0.1/jdk/是一个ftp服务吗?或者是一个http服务,你要保证服务是打开的,并监听正确的端口

热点排行