求解!!!

gaoliang21 2002-06-15 01:44:40
请帮忙看一下这个程序,不知为何在我标注*****的那行总是抛出一个IOException,实在调不出来了,那位好心的大哥帮忙指点一下?先谢了!!!!!!!!!
public int getCount() throws Exception
{
// Get the server URL
java.net.URL url =null;
java.net.URLConnection con =null;
byte buf[]= null;
DataOutputStream dataOut=null;

url = new java.net.URL(m_url);

// Attempt to connect to the host
con = url.openConnection();

// Set the session ID if necessary
if (m_cookie != null) {
con.setRequestProperty("cookie", m_cookie);
}

// Initialize the connection
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);


// Data will always be written to a byte array buffer so
// that we can tell the server the length of the data
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

// Create the output stream to be used to write the
// data to our buffer
DataOutputStream out = new DataOutputStream(byteOut);

// Send any data to the servlet here

// Flush the data to the buffer
out.flush();

// Get our buffer to be sent
buf = byteOut.toByteArray();

// Set the content that we are sending
con.setRequestProperty("Content-type",
"application/octet-stream");

// Set the length of the data buffer we are sending
con.setRequestProperty("Content-length",
"" + buf.length);

// Get the output stream to the server and send our
// data buffer
***** dataOut = new DataOutputStream(con.getOutputStream());


dataOut.write(buf);
// Flush the output stream and close it
dataOut.flush();
dataOut.close();

// Get the input stream we can use to read the response
DataInputStream in =
new DataInputStream(con.getInputStream());

// Read the data from the server
int count = in.readInt();

// Close the input stream
in.close();

// Get the session cookie if we haven't already
if (m_cookie == null) {
String cookie = con.getHeaderField("set-cookie");
if (cookie != null) {
m_cookie = parseCookie(cookie);
System.out.println("Setting session ID=" + m_cookie);
}
}

return count;
}
...全文
101 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
seani 2002-06-17
  • 打赏
  • 举报
回复
对象不存在
gaoliang21 2002-06-17
  • 打赏
  • 举报
回复
谢谢各位!以下是Exception的内容,对不起,不是IOException,是我记错了(不过java.net.ConnectException是从IOException继承来的,也不算错)
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:320)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:133)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:120)
at java.net.Socket.<init>(Socket.java:273)
at java.net.Socket.<init>(Socket.java:100)
at sun.net.NetworkClient.doConnect(NetworkClient.java:50)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:331)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:517)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:267)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:277)
at sun.net.www.http.HttpClient.New(HttpClient.java:289)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection
.java:408)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLCo
nnection.java:455)
at javaservlets.session.CounterApp.getCount(CounterApp.java:134)
at javaservlets.session.CounterApp.main(CounterApp.java:55)
Press any key to continue . . .
drinkant 2002-06-16
  • 打赏
  • 举报
回复
对象不存在不会抛IOException吧,应该是Null甚么exception吧......
叫他把exception的具体内容贴出来看看就知道了
wdydt163 2002-06-16
  • 打赏
  • 举报
回复
我也觉得是这样的:con = url.openConnection();你应该检查一下con 是否是空的?
diaopeng 2002-06-16
  • 打赏
  • 举报
回复
据我所知,不是什么权限问题,而是对象不存在,至于怎么解决我到现在都还没想出来
diaopeng 2002-06-16
  • 打赏
  • 举报
回复
估计是这样的错误:NONEPOINTER
gaoliang21 2002-06-16
  • 打赏
  • 举报
回复
我嵌入try语句测试,结果程序在这条语句抛出IOException异常。我是准备往URLConnection里写一些内容,目标肯定存在而且URL肯定没错,我估计也可能是权限不够,我是在weblogic 7.0上调试的,可不知道如何进行权限之类的设置,请明示!!!谢谢!!!!!!!!!
littlecong 2002-06-16
  • 打赏
  • 举报
回复
try to conver con to HttpURLConnection
drinkant 2002-06-15
  • 打赏
  • 举报
回复
IOException的具体内容 ?
你是企图往这个URL写,可能目标不存在,可能权限不够,反正乱七八糟的都有可能
gaoliang21 2002-06-15
  • 打赏
  • 举报
回复
我问的是为什么会抛出异常?顺便说一下:我给出的URL肯定没有错。
dylanwolf 2002-06-15
  • 打赏
  • 举报
回复
用 try

{
dataOut = new ...
...

}

catch{}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧