代码如下:
String info;
public void init()
{
URL url;
URLConnection urlc; resize(200,100);
setBackground(Color.white);
try{
//建立站点连接
url = new URL("http://localhost/file/sample.html");//连接出现问题???
//可能是文件路径错误???如何选择文件路径??
urlc = url.openConnection();
urlc.connect();
info = getInfo(urlc);
}catch(MalformedURLException mfe){
System.out.println("URL form error!");
}catch(IOException ioe){
System.out.println("IO1 Exception!");
}
} public String getInfo(URLConnection urlc)
{ //读取文件信息
String txt ="";
InputStream is;
int i; try{
is = urlc.getInputStream();//获取输入流
i = is.read();
while(i != -1){
txt = txt + (char)i;
i = is.read();
}
is.close();
}catch(IOException ioe){
System.out.println("IO2 Exception!");
txt = new String("File read failed!");
} return txt;
}
public void paint(Graphics g)
{ //显示到applet
g.setColor(Color.red);
g.drawString(info,50,50);
}
我用eclipse做的,运行的时候报空字符串的异常,IO1 Exception!!是不是http://localhost/file/sample.html路径有错了,我的路径是f;/file/sample.html。它应该放在那???在线等,急啊!!
String info;
public void init()
{
URL url;
URLConnection urlc; resize(200,100);
setBackground(Color.white);
try{
//建立站点连接
url = new URL("http://localhost/file/sample.html");//连接出现问题???
//可能是文件路径错误???如何选择文件路径??
urlc = url.openConnection();
urlc.connect();
info = getInfo(urlc);
}catch(MalformedURLException mfe){
System.out.println("URL form error!");
}catch(IOException ioe){
System.out.println("IO1 Exception!");
}
} public String getInfo(URLConnection urlc)
{ //读取文件信息
String txt ="";
InputStream is;
int i; try{
is = urlc.getInputStream();//获取输入流
i = is.read();
while(i != -1){
txt = txt + (char)i;
i = is.read();
}
is.close();
}catch(IOException ioe){
System.out.println("IO2 Exception!");
txt = new String("File read failed!");
} return txt;
}
public void paint(Graphics g)
{ //显示到applet
g.setColor(Color.red);
g.drawString(info,50,50);
}
我用eclipse做的,运行的时候报空字符串的异常,IO1 Exception!!是不是http://localhost/file/sample.html路径有错了,我的路径是f;/file/sample.html。它应该放在那???在线等,急啊!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货