org.apache.commons.net.io.CopyStreamException: IOException caught while copying

在用FTPClient上传文件时遇到异常,

异常内容如下:

2016-04-18 19:20:24.055 [Thread-47] ERROR com.al.crm.channel.bmo.impl.CrmSynBMOImpl [1201] - 增量文件同步异常:
org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
	at org.apache.commons.net.io.Util.copyStream(Util.java:135) ~[commons-net-3.1.jar:3.1]
	at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:583) ~[commons-net-3.1.jar:3.1]
	at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:557) ~[commons-net-3.1.jar:3.1]
	at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1795) ~[commons-net-3.1.jar:3.1]
	at com.al.crm.channel.bmo.impl.CrmSynBMOImpl.dealJust(CrmSynBMOImpl.java:1592) ~[CrmSynBMOImpl.class:na]
	at com.al.crm.channel.bmo.impl.CrmSynBMOImpl.iHandleNodeFn(CrmSynBMOImpl.java:1175) ~[CrmSynBMOImpl.class:na]
	at com.al.crm.channel.util.NodeInfoOperateThread.run(NodeInfoOperateThread.java:38) [NodeInfoOperateThread.class:na]
Caused by: java.net.SocketException: Connection reset by peer: socket write error
	at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.6.0_45]
	at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92) ~[na:1.6.0_45]
	at java.net.SocketOutputStream.write(SocketOutputStream.java:136) ~[na:1.6.0_45]
	at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105) ~[na:1.6.0_45]
	at org.apache.commons.net.io.Util.copyStream(Util.java:123) ~[commons-net-3.1.jar:3.1]
	... 6 common frames omitted

代码片段如下:

FTPClient ftpClientG = new FTPClient();
ftpClientG.connect(Constant.CONST_CSB_FTP_IP, Constant.CONST_CSB_FTP_PORT);
ftpClientG.login(Constant.CONST_CSB_FTP_PIC_EDA_USERNAME, Constant.CONST_CSB_FTP_PIC_EDA_PASSWORD);
int replyG = ftpClientG.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyG)) {
	ftpClientG.disconnect();
	logger.error("FTP连接失败!");
	return;
} else {
	ftpClientG.enterLocalPassiveMode();
	ftpClientG.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
	ftpClientG.setFileType(FTP.BINARY_FILE_TYPE);

	ftpClientG.changeWorkingDirectory(Constant.CONST_EDA_FTP_DIR);
	File fileDir = new File(Constant.CONST_INCRE_TEMP_FILE);
	for (File files : fileDir.listFiles()) {
		if (!files.isDirectory()) {// 非文件夹类型
			FileInputStream fileIS = new FileInputStream(files);
			ftpClientG.storeFile(files.getName(), fileIS);
			fileIS.close();
		}
	}
	// 修改文件名
	for (String fileName : ftpClientG.listNames()) {
		if (fileName.endsWith(".txt.ing") || fileName.endsWith(".zip.ing")) {
			ftpClientG.rename(fileName, fileName.substring(0, fileName.indexOf(".ing")));
		}
	}
	ftpClientG.logout();
	// 关闭连接
	if (ftpClientG.isConnected()) {
		ftpClientG.disconnect();
	}
}


抛异常代码行:

ftpClientG.storeFile(files.getName(), fileIS);

需要上传的文件有1个txt和多个zip,每次上传时,txt可以成功上传,zip上传2M之后就报以上的异常。


解决办法:

多次核对代码debug之后依然找不到代码里面的错,然后用FTP工具直接上传文件,发现上传zip时会多次提示文件已存在,是否续传……

联系FTP提供方才知道是FTP服务器限制了文件大小造成的。




你可能感兴趣的:(Java)