gpt4 book ai didi

发送http请求时Java java.io.EOFException?

转载 作者:行者123 更新时间:2023-12-02 08:05:19 26 4
gpt4 key购买 nike

嘿,我陷入了登录问题

这是我的 servlet

  /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
*
* @author amolbh
*/
public class AreaServlet extends HttpServlet {

private Connection connection = null;
private PreparedStatement prepStmt=null;
private String farid,password,farid1;
private ResultSet rst=null;



/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
if(request.getServletPath().equals("/login"))
loginValidation(request,response);

} finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
public String getServletInfo() {
return "Short description";
}// </editor-fold>



public void loginValidation(HttpServletRequest request, HttpServletResponse response)throws IOException
{
DataInputStream in = new DataInputStream((InputStream)request.getInputStream());
byte indata[] = new byte[request.getContentLength()];
in.readFully(indata,0,indata.length);
in = new DataInputStream(new ByteArrayInputStream(indata));
farid = in.readUTF();
password = in.readUTF();

DataOutputStream daos = new DataOutputStream((OutputStream)response.getOutputStream());

String s1,s2;

ByteArrayOutputStream baosdata = new ByteArrayOutputStream();
DataOutputStream doasdata = new DataOutputStream(baosdata);


try
{
connection=getConnection();
prepStmt=connection.prepareStatement("Select Username,Password from Login ");
rst=prepStmt.executeQuery();

int c = 0;

while(rst.next())
{

s1=rst.getString(1);
s2=rst.getString(2);
doasdata.writeUTF(s1);
doasdata.writeUTF(s2);
c++;

}

ByteArrayOutputStream baoshdata = new ByteArrayOutputStream();
DataOutputStream doashdata = new DataOutputStream(baoshdata);
if(c != 0)
{
doashdata.writeUTF("successfull");
// doashdata.writeInt(c);
doashdata.write(baosdata.toByteArray());
daos.write(baoshdata.toByteArray());
}
else{
doashdata.writeUTF("Invalid User");

daos.write(baoshdata.toByteArray());
}

}
catch(Exception e)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream daose = new DataOutputStream(baos);
daose.writeUTF(e.toString());
daos.write(baos.toByteArray());
}


}

public Connection getConnection() throws NamingException, SQLException
{
try {
/*InitialContext ic=new InitialContext();
* DataSource ds=(DataSource)ic.lookup("java:comp/env/jdbc/ImageCapture");
* connection = ds.getConnection();
* return connection;*/

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Step 2: Establish the connection to the database.
String url = "jdbc:odbc://localhost:8080/ImageCapture";
Connection conn = DriverManager.getConnection(url);
return conn;

} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return null;
}
}

}

以及我得到响应的 LoginSrv 类

String su = indat.readUTF(); 处获取“javaioEOF 异常”异常

 /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.marico.model;

import com.sun.lwuit.Dialog;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import com.marico.esaff.AppConstants;
/**
*
* @author sandipp
*/
public class LoginSrv {
private ServCon srv;
public static String st1,st2,log,pas;
public static int it1;

public LoginSrv(String s1, String s2)
{
log = s1;
pas = s2;
it1=0;
try
{
Dialog.show("Successfull","i am in Loginsrv" , null,Dialog.TYPE_INFO,null,3000);
ByteArrayOutputStream baosdata = new ByteArrayOutputStream();
DataOutputStream daosdata = new DataOutputStream(baosdata);

daosdata.writeUTF(s1);
daosdata.writeUTF(s2);
srv = new ServCon(new AppConstants().str1,null,baosdata.toByteArray(),false,false,null);
ByteArrayOutputStream obj = (ByteArrayOutputStream)srv.startTransfer();
byte[] record = obj.toByteArray();
ByteArrayInputStream instr = new ByteArrayInputStream(record);
DataInputStream indat = new DataInputStream(instr);

if(srv.getRc() == 200)
{
Dialog.show("Successfull","i am in Loginsrv "+srv.getRc() , null,Dialog.TYPE_INFO,null,3000);
String su = indat.readUTF();
// if(su.equals("successfull"))
{

st1 =indat.readUTF();
st2 =indat.readUTF();
System.out.println("st1 and st2 "+ st1 + " , "+st2);
Dialog.show("Successfull",su , null,Dialog.TYPE_INFO,null,3000);
}
// else
{
// Dialog.show("Error",su , null,Dialog.TYPE_INFO,null,5000);

}
}
else
{
Dialog.show("Error",srv.getRc() + " " + srv.getRm(), null,Dialog.TYPE_INFO,null,5000);

}
}
catch(Exception e)
{
Dialog.show("Error","I am in catch "+e.toString(), null,Dialog.TYPE_INFO,null,5000);
System.out.println("Im from LoginSrv constructor: "+e);
}

}

}

这里是我正在做连接的 ServCon.java

    /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.marico.model;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.util.Resources;
import com.sun.lwuit.Label;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

/**
*
* @author sandipp
*/
public class ServCon {
private String url,sendparam,indata;
private byte sendparamb[];
private boolean instring,outstring;
private String error;
private int rc;
private String rm;
private Resources r;
//private View.DisplayProgress dp;

public static com.sun.lwuit.TextArea textobj;
public ServCon(String url,String sendparam,byte[]sendparamb,boolean instring,boolean outstring,Resources r)
{
this.url = url;
this.instring = instring;
this.outstring = outstring;
this.r = r;
//this.dp = dp;
if(this.instring)
{
this.sendparam = sendparam;
}
else
{
this.sendparamb = sendparamb;
}
}
public Object startTransfer() throws Exception
{


HttpConnection c = (HttpConnection)Connector.open(url);
c.setRequestProperty("User-Agent","Profile/MIDP-1.0, Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language","en-US");
c.setRequestMethod(HttpConnection.POST);
DataOutputStream os = (DataOutputStream)c.openDataOutputStream();



if(instring)
os.writeUTF(sendparam);
else
os.write(sendparamb,0,sendparamb.length);

rc = c.getResponseCode();//getResponseCode();
rm = c.getResponseMessage();
int len = (int)c.getLength();
System.out.println( getRm() + " " + len);

DataInputStream is =(DataInputStream)c.openDataInputStream();

Object outobj = null;
if(outstring)
{
StringBuffer rdata = new StringBuffer();
int ch;

while ((ch = is.read()) != -1)
{
rdata.append((char)ch);
}

indata = rdata.toString();
outobj = indata;
}
else
{

sendparamb = new byte[len];
is.readFully(sendparamb,0,sendparamb.length);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream((OutputStream)baos);
dout.write(sendparamb, 0, sendparamb.length);
outobj = baos;
}

is.close();
c.close();
os.flush();
os.close();

System.out.println("Reached at the end of the httpdatafetch");
return outobj;



}
/**
* @return the rc
*/
public int getRc() {
return rc;
}
/**
* @return the rm
*/
public String getRm() {
return rm;
}





}

最佳答案

在发送方中,基本上有 writeUTF("successfull") ,后面跟着两个 write() 字节数组。在接收器中,您基本上有 if readUTF() == "successfull" 那么如果是这样,您再执行两个 readUTF()。但是,readUTF() 只能读取 writeUTF() 写入的内容:请参阅 Javadoc。它当然无法读取带有任意 byte[] 数组的 write() 结果。您需要弄清楚您的应用程序协议(protocol)实际上是什么。

对于我的喜好,您还有太多的 ByteArrayInputStreamByteArrayOutputStream。这些只会增加延迟和出现上述错误的机会。如果有东西要写,就直接写到输出流;如果您认为有东西要读,请从输入流中读取它。

关于发送http请求时Java java.io.EOFException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8277319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com