【发布时间】:2015-06-28 17:37:54
【问题描述】:
This is the image that causes an error
我遇到了这个问题,我尝试了我所知道的一切,但没有任何效果。 我正在尝试通过套接字从数据库向客户端应用程序发送多个图像(一次图像),有时一切正常,但有时它会声明此“无效的 JPEG 文件结构:两个 SOI 标记”错误?
客户端:
for(User user : users){
int cmpt=0;
byteToread =in.readInt();
bytesOut=new ByteArrayOutputStream();
bos = new BufferedOutputStream(bytesOut);
while (byteToread >cmpt) {
cmpt = in.read(dataEntre);
bos.write(dataEntre, 0, cmpt);
bos.flush();
byteToread-=cmpt;
}
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(bytesOut.toByteArray()));
user.setPhoto(new ImageIcon(bi));
System.out.println("------------------end");
}
bos.close();
bytesOut.close();
服务器端:
InputStream input =null;
Statement myStmt = null;
ResultSet myRs = null;
BufferedInputStream bis=null;
try {
myStmt = Conn.createStatement();
myRs = myStmt.executeQuery("select Photo from users order by Name");
byte[] buffer;
int k =1;
while (myRs.next()) {
input=myRs.getBinaryStream("Photo");
bis = new BufferedInputStream(input);
buffer = new byte[1024];
try {
int byteToread = 0;
int cmpt=0;
byteToread=input.available();
out.writeInt(byteToread);
out.flush();
int i=0;
while (byteToread>cmpt) {
cmpt = bis.read(buffer);
out.write(buffer, 0, cmpt);
out.flush();
byteToread -= cmpt;
}
} catch (IOException ex) {
return ;
}
}
【问题讨论】:
-
这个问题最近才出现,我的应用程序正常工作了将近 2 个月,但现在这个错误不知从何而来
-
可能是您使用的图像在 2 个月内没有触发问题。你在问什么并不完全清楚。还建议链接导致失败的图像,以防有人想自己测试。 (当然假设图像没有版权)
-
好吧,我会感谢你的,那我写对了代码呢,特别是客户端
-
SOI 标记出现在流中的什么位置?您是否运行了一些 JPEG 转储程序来查找市场订单?
-
使用
ImageIO.read(new File("path/to/your/file.jpg"))可以很好地读取图像(并且应该只有一个 SOI 标记)。因此,正如 EJP 和 JoeBlade 都指出的那样,错误出在您的字节洗牌代码中......