我最近做了一个FTP的上传功能,不过发现一个问题,可以在服务器上创建文件夹,不能上传文件,只要上传就抛出connection reset by peer的异常,我的代码片段是: /**
* FTP上传单个文件测试
*
* @throws BusinessException
*/
public void upload(String toFtpName, InputStream fis) { try {
System.err.println("WorkingDirectory: "
+ ftpClient.printWorkingDirectory());
// 改变文件名中的编码,防止中文乱码
String decodeSaveFileName = decode(toFtpName);
boolean isStore = ftpClient.storeFile(decodeSaveFileName, fis);
if (isStore) {
System.err.println("文件:" + toFtpName + "上传成功!");
} else {
System.err.println("文件:" + toFtpName + "上传失败!");
throw new RuntimeException("文件上传失败!");
}
} catch (IOException e) {
e.printStackTrace();
System.err.println("FTP客户端出错!" + e);
throw new RuntimeException(e);
}
}
* FTP上传单个文件测试
*
* @throws BusinessException
*/
public void upload(String toFtpName, InputStream fis) { try {
System.err.println("WorkingDirectory: "
+ ftpClient.printWorkingDirectory());
// 改变文件名中的编码,防止中文乱码
String decodeSaveFileName = decode(toFtpName);
boolean isStore = ftpClient.storeFile(decodeSaveFileName, fis);
if (isStore) {
System.err.println("文件:" + toFtpName + "上传成功!");
} else {
System.err.println("文件:" + toFtpName + "上传失败!");
throw new RuntimeException("文件上传失败!");
}
} catch (IOException e) {
e.printStackTrace();
System.err.println("FTP客户端出错!" + e);
throw new RuntimeException(e);
}
}
连接的设置如下: /**
* @throws SocketException
* @throws IOException
*/
private void connect() throws SocketException, IOException {
ftpClient = new FTPClient(); ftpClient.connect(ip);
ftpClient.login(username, password);
ftpClient.setDefaultPort(21);
ftpClient.setBufferSize(8 * 1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
changeWorkingDirectory(remoteDir);
}