问题描述
服务器端代码:publicclasstableextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;publictable(){//TODOAuto-generatedconstructorstub}protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//TODOAuto-generatedmethodstubthis.doPost(request,response);}protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//创建数据输入流DataInputStreamdis=newDataInputStream(request.getInputStream());//读取客户端发送的查询数据intmode=dis.readInt();StringtableQuery=dis.readUTF();//StringtableQuery=request.getParameter("tableQuery");//StringtableQueryType=request.getParameter("tableQueryType");//response.setContentType("text/html;charset=gb2312");//构建输出流ByteArrayOutputStreambaos=newByteArrayOutputStream();DataOutputStreamdos=newDataOutputStream(baos);//查询处理Stringurl="jdbc:odbc:MSAccessDatabase;DBQ=D:\moveteach.mdb";try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");Connectionconn=DriverManager.getConnection(url);Statementstmt=conn.createStatement();ResultSetrs=stmt.executeQuery("select*fromcourse");//dos.writeUTF(tableQuery);System.out.println(tableQuery);while(rs.next()){//响应数据dos.writeUTF("星期:"+rs.getString("week"));dos.writeUTF("课程:"+rs.getString("coursename"));dos.writeUTF("任课老师:"+rs.getString("teacher"));dos.writeUTF("教室:"+rs.getString("classroom"));dos.writeUTF("班级:"+rs.getString("classes"));}//设置服务器响应参数response.setContentType("application/octet-stream");byte[]data=baos.toByteArray();response.setStatus(HttpServletResponse.SC_OK);response.setContentLength(data.length);//获得响应输出流,写出响应数据ServletOutputStreamsos=response.getOutputStream();OutputStreamos=newDataOutputStream(sos);System.out.println("Ok");os.write(data);dos.flush();rs.close();stmt.close();conn.close();os.close();}catch(Exceptione){}}客户端代码局部:publicvoidtableServer(){try{Stringurl="http://localhost:8080/moveteachservlet/table.do";//查询条件StringtableQuery=this.tableQuery.getString();//inttableQueryType=this.tableQueryType;HttpConnectionconn=(HttpConnection)Connector.open(url);//设置请求类型为POSTconn.setRequestMethod(HttpConnection.POST);//设置一般的请求属性conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");conn.setRequestProperty("User-Agent","Profile/MIDP-1.0Configuration/CLDC-1.0");conn.setRequestProperty("Content-Language","en-US");conn.setRequestProperty("Accept","application/octet-stream");conn.setRequestProperty("Connection","close");StringformData="tableQuery="+tableQuery;rTableForm.append(formData);//转换显字节流byte[]data=formData.getBytes();//设置写入流的长度conn.setRequestProperty("Content-Length",Integer.toString(data.length));OutputStreamos=conn.openOutputStream();os.write(data);os.flush();//获得响应状态intstatus=conn.getResponseCode();//读取服务器的响应数据rTableForm.append("11");DataInputStreamdism=newDataInputStream(conn.openInputStream());//读取服务器返回的字节流//Stringresult=dism.readUTF();//rTableForm.append(result);rTableForm.append("22");while(dism.readUTF()!=null)//判断当数据流不为空的时候{rTableForm.append(dism.readUTF());//用readUTF()方法构造字符串放在StringBuffer里rTableForm.append("/n");}rTableForm.append("33");dism.close();}catch(Exceptione){System.out.println(e);}}………………………………elseif(cmd==cmdTableQuery){//课表查询newThread(newRunnable(){synchronizedpublicvoidrun(){try{tableServer();}catch(Exceptione){e.printStackTrace();}}}).start();display.setCurrent(rTableForm);//sm=newsendTable(this);//调用sendMsg类,将本类作为参数传入//display.setCurrent(sm);//显示sendMsg类}………………
解决方案
解决方案二:
服务端获得响应输出流后,写出了data,客户端读取的时候,也要读取输入流InputStream,然后套上字节输入流DataInputStream,,然后再按顺序readUTF()..
解决方案三:
已经解决了,谢谢了,