FTP-abot move file
public void ftpGetFile(String sapepFilePath,String ipqrsPath, String fileName) throws Exception {FTPClient ftp = this.getFtpConnect(HOSTNAME,USERNAME,PASSWORD);
InputStream ins = null;
try {
ins = ftp.retrieveFileStream(sapepFilePath);
this.saveFile(ins, ipqrsPath, fileName);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
} finally {
try {
if (ins != null)
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (ftp != null && ftp.isConnected()) {
ftp.logout();
ftp.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void saveFile(InputStream ins, String strDir, String strFile) throws Exception {
File file = new File(strDir);
FileOutputStream fos = null;
// Create a buffer for reading the files
byte[] buf = new byte;
try {
file.mkdirs();
file = new File(strDir + SLASH + strFile);
if (!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(file);
int len;
while ((len = ins.read(buf)) > 0) {
fos.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e.getMessage());
} finally {
fos.close();
}
}
public FTPClient getFtpConnect(String hostName, String userName, String password){
FTPClient ftp = new FTPClient();
try{
ftp.connect(hostName);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
}
ftp.login(userName, password);
ftp.setBufferSize(100000);
ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
} catch (Exception e) {
e.printStackTrace();
}
return ftp;
}
页:
[1]