使用socket方式传输文件的时候,经常会碰到Software caused connection abort: socket
write error
的错误,个人测试的时候,发现跟传输的文件大小总和有一定关系,所以就有了下面的一个简单的解决方案,它只是针对多个小文件的总和大于传输限制的情况能够
起一定的作用,如果一个文件的大小就超过了传输的限制,那这个方法,也不起左右。
避免错误的方法就是,在传送完每个文件后重置一下ObjectOutputStream对象。
this.outObjectStream.reset();
下面是一段程序源码:
package test.socket.sockettransfer;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Iterator;
import test.soa.message.MessagePOJO;
public class SocketClient {
private
Socket socket = null;
private
ObjectOutputStream outObjStream = null;
private
ObjectInputStream inObjStream = null;
public
SocketClient(TransferObject p_obj, String p_strIP, int p_iPort)
throws
Exception {
this.socket = new Socket(p_strIP, p_iPort);
this.outObjStream = new
ObjectOutputStream(this.socket.getOutputStream());
this.inObjStream = new
ObjectInputStream(this.socket.getInputStream());
this.transfer(p_obj);
this.close();
}
public
SocketClient(String p_strIP, int p_iPort) throws Exception {
this.socket = new Socket(p_strIP, p_iPort);
this.socket.setPerformancePreferences(1,0,0);
this.socket.setKeepAlive(true);
this.socket.setSoTimeout(600 * 10000);
this.socket.setTcpNoDelay(true);
this.socket.setOOBInline(true);
//this.socket.set
//System.out.print("Socket==" + this.socket.isConnected());
try {
if(this.outObjStream == null && !socket.isOutputShutdown())
{
this.outObjStream = new
ObjectOutputStream(this.socket.getOutputStream());
}
}catch(Exception e){}
//System.out.println("cccccccccccccccccccc");
}
public
String sendPOJO(MessagePOJO messagepojo) throws Exception
{
try{
//System.out.println("Trans Object");
outObjStream.writeObject(messagepojo);
//System.out.println("Trans Object End");
// 如果TransferObject实例存在附件
TransferObject transferobject=null;
if(messagepojo!=null &&
messagepojo.getTransferObjectMap()!=null &&
messagepojo.getTransferObjectMap().get("transferobject")!=null){
transferobject =
((TransferObject)messagepojo.getTransferObjectMap().get("transferobject"));
}else{
throw new Exception("TransferObject
Error!");
}
if (transferobject.hasFile())
{
Iterator fileListItem =
transferobject.getTransferFileList().values().iterator();
while(fileListItem!=null &&
fileListItem.hasNext())
{
// 传输附件
transferFile( (TransferFile)fileListItem.next());
//重置一下对象,清空对象缓存
this.outObjStream.reset();
//this.socket.
}
}
this.outObjStream.flush();
}catch(Exception ex){
throw ex;
}
String returnType="Error!";
returnType=this.readSocketType();//取得接收方最后的返回状态值
if(returnType!=null &&
!"".equals(returnType)){//连接成功
return returnType;
}else{
return new
String("Error!");
}
}
public void
transfer(TransferObject p_obj) throws Exception
{
//System.out.println("Trans Object");
outObjStream.writeObject(p_obj);
//System.out.println("Trans Object End");
// 如果TransferObject实例存在附件
if (p_obj.hasFile())
{
Iterator fileListItem =
p_obj.getTransferFileList().values().iterator();
while(fileListItem!=null &&
fileListItem.hasNext())
{
// 传输附件
transferFile( (TransferFile)fileListItem.next());
//this.socket.
}
}
//
TransferObject transObj = this.readTransferObject();
//
p_obj.exchangeObject(transObj);
}
private
TransferObject readTransferObject() throws Exception
{
Object obj = inObjStream.readObject();
if (! (obj instanceof TransferObject))
throw new IOException();
TransferObject transObj = (TransferObject) obj;
return transObj;
}
private void
transferFile(TransferFile p_transferFile)
{
FileInputStream fileInStream = null;
int iByteNum = 0;
try {
//System.out.println("p_transferFile.getFilePath()="+p_transferFile.getFilePath());
//System.out.println("p_transferFile.getFileName()="+p_transferFile.getFileName());
// 创建本读文件输入流
fileInStream = new FileInputStream(p_transferFile.getFilePath()
+
p_transferFile.getFileName());
TransferFileBegin begin = new TransferFileBegin();
outObjStream.writeObject(begin);
try{
outObjStream.flush();
}catch(Exception e){
//e.printStackTrace();
}
// 从本地读取附件,封装到TransferFileBlock类中。