我必须改进一段必须与Java 1.6兼容的Java代码,并且我正在寻找下面函数中fileoutputstream的替代方案。我正在使用apache.commons FTP包。Java 1.6的FileOutputStream替代方案
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
FTPClient ftp = null;
public FTPFetch(String host, int port, String username, String password) throws Exception
{
ftp = new FTPClient();
ftp.setConnectTimeout(5000);
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host, port);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
if (ftp.login(username, password))
{
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalActiveMode();
} else
{
disconnect();
errorLog.fatal("Remote FTP Login Failed. Username or Password is incorrect. Please update in configuration file.");
System.exit(1);
}
}
try (FileOutputStream fos = new FileOutputStream(destination))
{
if (this.ftp.retrieveFile(source, fos))
{
return true;
} else
{
return false;
}
} catch (IOException e)
{
return false;
}
'FileOutputStream' exists si nce JDK 1.0 – Berger
注意:'if(condition){return true; } else {return false; }'和return条件相同;' –
谢谢,我已经修改了代码以在本地修复此问题。 – Tacitus86