我正在进行客户端/服务器套接字编程。我的笔记本电脑上的Android客户端和Java Server。我想通过套接字从Android设备发送几个图像到Java服务器,它将每个图像保存在MySQL数据库中作为blob。我关闭每个图像传输的客户端套接字,然后重新连接以进行下一次图像传输。
我的第一个图像传输工作正常并保存在MySQL数据库中但是当我尝试重新连接套接字到服务器并传输不同的图像时,我得到连接重置。
java.sql.SQLException: Error reading from InputStream java.net.SocketException
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.PreparedStatement.readblock(PreparedStatement.java:3099)
at com.mysql.jdbc.PreparedStatement.streamToBytes(PreparedStatement.java:5106)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2575)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2366)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2350)
at Uploadpic.run(Uploadpic.java:113)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at com.mysql.jdbc.PreparedStatement.readblock(PreparedStatement.java:3097)
... 7 more
public class Uploadpic extends Thread {
private Socket socket1 =null;
//public Client user = new Client();
public Uploadpic(Socket socket1)
{
this.socket1 = socket1;
}
public void run() {
int imagenum = 0;
String mes = null;
BufferedReader buff;
int imagesize = 0;
InputStream is = null;
try {
buff = new BufferedReader(newInputStreamReader(socket1.getInputStream()));
while ((mes = buff.readLine())!= null)
{
if(!mes.equals(null))
break;
}
System.out.println("image size is =" + mes);
} catch (IOException e) {
e.printStackTrace();
}
imagesize = Integer.parseInt(mes);
try {
is = socket1.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
String connectionUrl = "jdbc:mysql://localhost:3306/users";
String dbUser = "root";
String dbPwd = "daniere";
boolean check = false ;
Connection conn = null;
ResultSet rs;
PreparedStatement ps = null;
PreparedStatement stmt = null;
byte [] mybytearray = new byte [imagesize];
int rowCount = -1;
String INSERT_PICTURE =
"INSERT INTO USERPICS (id_PIC,user_id,picname)values (?, ?, ?)";
try
{
conn = DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
try {
while(check == false)
conn.setAutoCommit(true);
ps = (PreparedStatement)conn.prepareStatement(INSERT_PICTURE);
System.out.println(" ");
ps.setInt(1,imagenum);
ps.setInt(2, DataBase.user.getClientId());
ps.setBinaryStream(3, is, (int) mybytearray.length);
**ERROR HAPPENS HERE**int s = ps.executeUpdate();
if(s>0) {
System.out.println("Uploaded successfully !");
}
else {
System.out.println("unsucessfull to upload image.");
}
} finally {
try {
ps.close();
ps = null;
is.close();
//socket1.close();
socket2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null)
{
conn.close();
conn = null;
}
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
}
}
答案 0 :(得分:0)
我想这是由于多线程问题。我猜你因为Uploadpic extends Thread
(btw it's recommended to implement Runnable instead of extending Thread)而产生一个用于插入每张图片的线程,而且我怀疑
DriverManager.getConnection(connectionUrl, dbUser, dbPwd)
,则返回相同的连接。
如果是这种情况,可能会出现一种可能的竞争条件,其中两个线程使用Connection
引用的相同conn
对象,一个关闭它而另一个仍在使用它。< / p>
解决方案:无需打开/关闭每张图片的连接,这是无用的工作:启动服务器时打开数据库连接,退出时关闭它。