public boolean getFileInformation(String strurl)
    {
        this.txtFileInfo.setText("");//txtFileInfo是一个JTextArea
        String wrapLine=System.getProperty("line.separator");
        try{
            URL url=new URL(strurl);
            URLConnection urlcon=url.openConnection();
            this.txtFileInfo.append("Source :"+strurl+wrapLine+wrapLine);
            this.txtFileInfo.append("Host :"+url.getHost()+wrapLine+wrapLine);
            this.txtFileInfo.append("Port :"+url.getPort()+wrapLine+wrapLine);
            this.txtFileInfo.append("FileName :"+url.getFile()+wrapLine+wrapLine);
            this.txtFileInfo.append("FileType :"+urlcon.getContentType()+wrapLine+wrapLine);
            this.txtFileInfo.append("FileLength :"+urlcon.getContentLength());
            return true;
        }catch(MalformedURLException e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(this,"非法的URL!","错误",JOptionPane.ERROR_MESSAGE);
            return false;
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this,"连接失败!","错误",JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            return false;
        }
    }
//调用:  boolean isOpen=getFileInformation("http://127.0.0.1:6000/WebModule/天仙子.mp3");/*得到如下结果 *Source :http://127.0.0.1:6000/WebModule/天仙子.mp3 *Host :127.0.0.1 *Port :6000 *FileName :/WebModule/天仙子.mp3 *FileType :text/html;charset=utf-8          //  这里为什么是text/html而部是mp3格式呢? *FileLength :1033        //源文件大小是 5024937字节,可这里为什么是1033?
 
 */

解决方案 »

  1.   

    urlconnection的信息肯定是text/html了
      

  2.   

    楼主肯定是哪里搞错了
    我试了网上的可以得到正确的信息
    Source :http://images.dangdang.com/onlineplay/music/北京欢迎你.mp3Host :images.dangdang.comPort :-1FileName :/onlineplay/music/北京欢迎你.mp3FileType :audio/mpegFileLength :6231911
      

  3.   

    我测试了一下,假如你的文件路径不对,则返回text/html类型,就是找不到你的路径下的文件的返回类型
      

  4.   

    先看看http响应头的状态码,看看是不是200
      

  5.   

     *FileType :text/html;charset=utf-8          //  这里为什么是text/html而部是mp3格式呢?
    很明显,服务器发生错误了,根本没找到你要的东西,所以返回了一个错误页面给你
    当然是 text/html 了http://127.0.0.1:6000/WebModule/天仙子.mp3
    1 建议你直接在地址栏输入看看能否访问
    2 把天仙子改名为 txz 。 不用用中文看看。
      

  6.   

    同意楼上的兄台的意见
    我前几天也有这种问题(但我是在本地获取文件)
    原因就是我用了中文名,URL遇到中文就认为已经读取完了
    要么就将中文转换下,也可以