1、系统临近上线,今天拿出来重新过了一遍,发现一个FTP上传的问题,就是一直报
java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) at java.net.SocketOutputStream.write(SocketOutputStream.java:153) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221) at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291) at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295) at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141) at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229) at java.io.BufferedWriter.flush(BufferedWriter.java:254) at org.apache.commons.net.ftp.FTP.__send(FTP.java:505) at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:479) at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:556) at org.apache.commons.net.ftp.FTP.port(FTP.java:881) at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:755) at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:602) at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:593) at org.apache.commons.net.ftp.FTPClient.storeUniqueFile(FTPClient.java:2027) at FtpMy.main(FtpMy.java:43)
2、于是乎就疯狂的google,而后,又度娘乎,无果,不得其解,沉思,好好的怎么会不行了呢。突然一道灵光向我劈来,妈的是不是刚装了系统,开着防火墙。
---还真开着,关之,重试,OK。
附上ftp代码
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.TimeZone; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.apache.log4j.Logger; public class FtpClient { private FTPClient ftpClient; private String strIp; private int intPort; private String user; private String password; private static Logger logger = Logger.getLogger(FtpClient.class.getName()); /* * * Ftp构造函数 */ public FtpClient(String strIp, int intPort, String user, String Password) { this.strIp = strIp; this.intPort = intPort; this.user = user; this.password = Password; this.ftpClient = new FTPClient(); } /** * @return 判断是否登入成功 * */ public boolean ftpLogin() { boolean isLogin = false; FTPClientConfig ftpClientConfig = new FTPClientConfig(); ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID()); this.ftpClient.setControlEncoding("GBK"); this.ftpClient.configure(ftpClientConfig); try { if (this.intPort > 0) { this.ftpClient.connect(this.strIp, this.intPort); } else { this.ftpClient.connect(this.strIp); } // FTP服务器连接回答 int reply = this.ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { this.ftpClient.disconnect(); logger.error("登录FTP服务失败!"); return isLogin; } this.ftpClient.login(this.user, this.password); // 设置传输协议 this.ftpClient.enterLocalPassiveMode(); this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); logger.info("恭喜" + this.user + "成功登陆FTP服务器"); isLogin = true; } catch (Exception e) { e.printStackTrace(); logger.error(this.user + "登录FTP服务失败!" + e.getMessage()); } this.ftpClient.setBufferSize(1024 * 2); this.ftpClient.setDataTimeout(30 * 1000); return isLogin; } /** * @退出关闭服务器链接 * */ public void ftpLogOut() { if (null != this.ftpClient && this.ftpClient.isConnected()) { try { boolean reuslt = this.ftpClient.logout();// 退出FTP服务器 if (reuslt) { logger.info("成功退出服务器"); } } catch (IOException e) { e.printStackTrace(); logger.warn("退出FTP服务器异常!" + e.getMessage()); } finally { try { this.ftpClient.disconnect();// 关闭FTP服务器的连接 } catch (IOException e) { e.printStackTrace(); logger.warn("关闭FTP服务器的连接异常!"); } } } } /*** * 上传Ftp文件 * * @param localFile * 当地文件 * @param romotUpLoadePath上传服务器路径 * - 应该以/结束 * */ public boolean uploadFile(File localFile, String romotUpLoadePath) { BufferedInputStream inStream = null; boolean success = false; try { this.ftpClient.changeWorkingDirectory(romotUpLoadePath);// 改变工作路径 inStream = new BufferedInputStream(new FileInputStream(localFile)); logger.info(localFile.getName() + "开始上传....."); success = this.ftpClient.storeFile(localFile.getName(), inStream); if (success == true) { logger.info(localFile.getName() + "上传成功"); return success; } } catch (FileNotFoundException e) { e.printStackTrace(); logger.error(localFile + "未找到"); } catch (IOException e) { e.printStackTrace(); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return success; } /*** * 下载文件 * * @param remoteFileName * 待下载文件名称 * @param localDires * 下载到当地那个路径下 * @param remoteDownLoadPath * remoteFileName所在的路径 * */ public boolean downloadFile(String remoteFileName, String localDires, String remoteDownLoadPath) { String strFilePath = localDires + remoteFileName; BufferedOutputStream outStream = null; boolean success = false; try { this.ftpClient.changeWorkingDirectory(remoteDownLoadPath); outStream = new BufferedOutputStream(new FileOutputStream( strFilePath)); logger.info(remoteFileName + "开始下载...."); success = this.ftpClient.retrieveFile(remoteFileName, outStream); if (success == true) { logger.info(remoteFileName + "成功下载到" + strFilePath); return success; } } catch (Exception e) { e.printStackTrace(); logger.error(remoteFileName + "下载失败"); } finally { if (null != outStream) { try { outStream.flush(); outStream.close(); } catch (IOException e) { e.printStackTrace(); } } } if (success == false) { logger.error(remoteFileName + "下载失败!!!"); } return success; } /*** * @上传文件夹 * @param localDirectory * 当地文件夹 * @param remoteDirectoryPath * Ftp 服务器路径 以目录"/"结束 * */ public boolean uploadDirectory(String localDirectory, String remoteDirectoryPath) { File src = new File(localDirectory); try { remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/"; this.ftpClient.makeDirectory(remoteDirectoryPath); // ftpClient.listDirectories(); } catch (IOException e) { e.printStackTrace(); logger.info(remoteDirectoryPath + "目录创建失败"); } File[] allFile = src.listFiles(); for (int currentFile = 0; currentFile < allFile.length; currentFile++) { if (!allFile[currentFile].isDirectory()) { String srcName = allFile[currentFile].getPath().toString(); uploadFile(new File(srcName), remoteDirectoryPath); } } for (int currentFile = 0; currentFile < allFile.length; currentFile++) { if (allFile[currentFile].isDirectory()) { // 递归 uploadDirectory(allFile[currentFile].getPath().toString(), remoteDirectoryPath); } } return true; } /*** * @下载文件夹 * @param localDirectoryPath本地地址 * @param remoteDirectory * 远程文件夹 * */ public boolean downLoadDirectory(String localDirectoryPath, String remoteDirectory) { try { String fileName = new File(remoteDirectory).getName(); localDirectoryPath = localDirectoryPath + fileName + "//"; new File(localDirectoryPath).mkdirs(); FTPFile[] allFile = this.ftpClient.listFiles(remoteDirectory); for (int currentFile = 0; currentFile < allFile.length; currentFile++) { if (!allFile[currentFile].isDirectory()) { downloadFile(allFile[currentFile].getName(), localDirectoryPath, remoteDirectory); } } for (int currentFile = 0; currentFile < allFile.length; currentFile++) { if (allFile[currentFile].isDirectory()) { String strremoteDirectoryPath = remoteDirectory + "/" + allFile[currentFile].getName(); downLoadDirectory(localDirectoryPath, strremoteDirectoryPath); } } } catch (IOException e) { e.printStackTrace(); logger.info("下载文件夹失败"); return false; } return true; } // FtpClient的Set 和 Get 函数 public FTPClient getFtpClient() { return ftpClient; } public void setFtpClient(FTPClient ftpClient) { this.ftpClient = ftpClient; } public static void main(String[] args) throws IOException { FtpClient ftp = new FtpClient("192.168.1.111", 21, "test", "123456"); ftp.ftpLogin(); // 上传文件夹 ftp.uploadDirectory("D:\\test\\2013", "/2013"); // 下载文件夹 // ftp.downLoadDirectory("d://tmp//", "/home/data/DataProtemp"); ftp.ftpLogOut(); } }
3、反思:遇到问题要冷静思考,切记急功近利。
附上官方的描述:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7077696
相关推荐
java ftp client server 代码 java ftp client server 代码 java ftp client server 代码 java ftp client server 代码
ftp server and ftp client
该代码总共有两部分 第一部分是FTPClient.java 第二部分是FtpSession.java
Swing+FtpClient实现 支持文件、文件夹上传 支持文件、文件夹下载 支持文件重命名 支持文件删除等功能
Very simple FTP-Client. Maybe interesting for someone
Java Socket 模拟 Ftp Server/Client
Java内置的FTP协议客户端。 仅命令行界面-没有GUI。
Client ftp (connects to a ftp server).
JAVA FTP客户端经典
vxWorks下ftp client源码
ftp客户端 java实现的 有详细分析步骤 功能模块图 原程序
ftp的server和client的代码 比较简单
基于Java语言的FTP Client程序设计.pdf
FTP是常用的网络功能,Java语言有很强的网络编程功能。使用Java语言开发FTP客户端程序很有实用价值。文章介绍了FTP协议的功能特点和FTP客户端的常用命令,阐述了如何使用Java程序设计语言来实现FTP客户端程序功能,并给...
资源讲了一个使用java socket编写FTP client 的实例,可以学习java socket编程的基本步骤和原理
FTP demo, FTP client access to FTP server, anh upload/download file.
项目启动文件为src/com/lzw/ftp/FTP_Client_Frame.java。 二、主要功能 运行程序,打开程序主界面,如图1所示。输入FTP地址、用户名及密码,单击“连接”按钮,连接FTP服务器。如果Ftp服务器支持匿名登录,选中...
1背景参考文章:Java使用SFTP和FTP两种连接方式实现对服务器的上传下载java使用JSCH连接FTP(Linux服务器)上传文件到Linux服务器Jav
java通过ftp三种方式 上传 下载文件: 1. 通过sun :sun.net.ftp.FtpClient 2. 通过apache:org.apache.commons.net.ftp.FTPClient 3. 通过edtftp: com.enterprisedt.net.ftp.FileTransferClient
一个Java Applet小程序。提供FTP上传文件(目录),支持拖放本地文件。可配置上传文件的格式等。 测试配置:Tomcat6.0 (IE控件) FTP服务端:TYPSoft FTP Server Client