文件类型和长度为何有错 20
请问:下面这段程序是哪里出错了,为什么文件类弄始终显示为null,文件长度始终为-1我实在没看出问题来,请各位指点谢谢!importjavax.swing.*;impor...
请问:下面这段程序是哪里出错了,为什么文件类弄始终显示为null,文件长度始终为-1
我实在没看出问题来,请各位指点
谢谢!
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
public class Wy extends JFrame implements ActionListener{
JButton b=new JButton( "ok");
JTextField t=new JTextField(30);
JTextArea ta=new JTextArea();
public static void main(String args[]){
new Wy();
}
Wy(){
getContentPane().add(t,"North");
getContentPane().add(ta,"Center");
getContentPane().add(b,"South");
b.addActionListener(this);
setVisible(true);
setSize(400,300);
}
public void actionPerformed(ActionEvent e){
String s=t.getText();
try{
URL url=new URL(s);
System.out.println(s);
URLConnection uc=url.openConnection();
String line=System.getProperty("line.separator");
ta.setText("协议: "+url.getProtocol());
ta.append(line);
ta.append("主机名:"+url.getHost());
ta.append(line);
ta.append("文件类型:"+uc.getContentType());
ta.append(line);
ta.append("端口:"+url.getDefaultPort());
ta.append(line);
ta.append("文件长度: "+uc.getContentLength());
}catch(Exception ee){}
}
} 展开
我实在没看出问题来,请各位指点
谢谢!
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
public class Wy extends JFrame implements ActionListener{
JButton b=new JButton( "ok");
JTextField t=new JTextField(30);
JTextArea ta=new JTextArea();
public static void main(String args[]){
new Wy();
}
Wy(){
getContentPane().add(t,"North");
getContentPane().add(ta,"Center");
getContentPane().add(b,"South");
b.addActionListener(this);
setVisible(true);
setSize(400,300);
}
public void actionPerformed(ActionEvent e){
String s=t.getText();
try{
URL url=new URL(s);
System.out.println(s);
URLConnection uc=url.openConnection();
String line=System.getProperty("line.separator");
ta.setText("协议: "+url.getProtocol());
ta.append(line);
ta.append("主机名:"+url.getHost());
ta.append(line);
ta.append("文件类型:"+uc.getContentType());
ta.append(line);
ta.append("端口:"+url.getDefaultPort());
ta.append(line);
ta.append("文件长度: "+uc.getContentLength());
}catch(Exception ee){}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询