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?
*/
我试了网上的可以得到正确的信息
Source :http://images.dangdang.com/onlineplay/music/北京欢迎你.mp3Host :images.dangdang.comPort :-1FileName :/onlineplay/music/北京欢迎你.mp3FileType :audio/mpegFileLength :6231911
很明显,服务器发生错误了,根本没找到你要的东西,所以返回了一个错误页面给你
当然是 text/html 了http://127.0.0.1:6000/WebModule/天仙子.mp3
1 建议你直接在地址栏输入看看能否访问
2 把天仙子改名为 txz 。 不用用中文看看。
我前几天也有这种问题(但我是在本地获取文件)
原因就是我用了中文名,URL遇到中文就认为已经读取完了
要么就将中文转换下,也可以