- - PR -
NullPointerExceptionです。ソースみてもらいたいですが
1
| 投稿者 | 投稿内容 |
|---|---|
|
投稿日時: 2004-04-09 15:43
いつもお世話になっております。
HttpURLConnectionで他のサーバーのExcelファイルのなかみを読み込みしたいですけど たまに(?)エラーが起きるので困ってます。 毎回起きるなら何とかしますけどたまにおきるのでどうすればいいか分からないです。 500エラーです。 エラー内容です。 java.lang.NullPointerException at org.apache.coyote.http11.InternalOutputBuffer.commit(InternalOutputBuffer.java:545) at org.apache.coyote.http11.Http11Processor.action(Http11Processor.java:662) at org.apache.coyote.Response.action(Response.java:220) at org.apache.coyote.http11.InternalOutputBuffer.endRequest(InternalOutputBuffer.java:371) at org.apache.coyote.http11.Http11Processor.action(Http11Processor.java:698) at org.apache.coyote.Response.action(Response.java:222) at org.apache.coyote.Response.finish(Response.java:343) at org.apache.coyote.tomcat4.OutputBuffer.close(OutputBuffer.java:326) at org.apache.coyote.tomcat4.CoyoteOutputStream.close(CoyoteOutputStream.java:125) ここからはソースの一部です。 ログインに成功した場合にはEXCELを表示して(res.setContentType("application/vnd.ms-excel; charset=Shift_JIS");) 失敗した場合には失敗したっていうメッセージを表示したいです。(res.setContentType("text/html; charset=Shift_JIS"); ) public void doPost( HttpServletRequest req, HttpServletResponse res ) throws IOException, ServletException { // BASIC認証 HttpSession session = req.getSession(); String authType = req.getHeader( "Authorization" ); // ユーザ認証 if(UserCheck( authType )){ URL url = null; HttpURLConnection urlConn = null; BufferedInputStream bi = null; if( param == null ) { res.setContentType("text/html; charset=Shift_JIS"); if( out != null ){ out.close(); out = null; } out = res.getOutputStream(); out.println("error"); session.invalidate() ; return; } res.setContentType("application/vnd.ms-excel; charset=Shift_JIS"); out = res.getOutputStream(); String strURL = "http://172.16.163.201:8080/sic/jsp/japan/jpn/PRODUCTS/Intora/"; String address = strURL + param; // URL の作成 url = new URL( address ); // コネクト urlConn = (HttpURLConnection)url.openConnection(); urlConn.setRequestMethod("GET"); // コンテンツの出力 bi = new BufferedInputStream(urlConn.getInputStream()); int i = bi.available(); if( out == null ){ out = res.getOutputStream(); } int length = 0; byte[] buf = new byte[i]; while( (length = bi.read(buf, 0, buf.length)) > 0 ) { out.write( buf, 0, length ); } // ディスコネクトする urlConn.disconnect(); urlConn = null; bi.close(); bi = null; out.close(); out = null; return; } else{ res.setStatus(res.SC_UNAUTHORIZED); res.setHeader("WWW-Authenticate", "BASIC realm=\"Renesas\"" ); res.setContentType("text/html; charset=Shift_JIS"); if( out != null ){ out.close(); out = null; } out = res.getOutputStream(); // HTML文書を送信する printHtml(); } session.invalidate() ; if( out != null ){ out.close(); out = null; } return; } // ユーザ認証 private boolean UserCheck( String auth ) throws IOException { if( auth == null ) return false; if( !auth.toUpperCase().startsWith("BASIC") ) return false; // "BASIC"の後のIDとパスワードを文字列に代入 String userpassEncoded = auth.substring( 6 ); // base 64 decodeの使用 sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder(); // IDとパスワードをデコードして文字列に代入 String passReq = new String( dec.decodeBuffer(userpassEncoded) ); int i = passReq.indexOf( ':' ); String ret[] = { passReq.substring( 0, i ), passReq.substring( i + 1 ) }; if( ret[0].equals("renesas") && ret[1].equals("renesas") ){ return true; } else{ return false; } } 汚いソースですがよろしくお願いします。 |
|
投稿日時: 2004-04-09 15:58
前のスレッドで説明しましたが
coasmさんがおっしゃったようにavailableで配列定義しないように bufの配列サイズはバッファの為なので固定で十分です。 500でたのはタイミングによってavailableが0でbufサイズが とれないからではないでしょうか? |
|
投稿日時: 2004-04-09 16:03
ご返答有難うございます。
availableのせいだったんですね。 ベテランの言うこと聞かなかった罰です。 本当に申し訳ありません。 |
1
