Windows服务器上运行一个获取图片的程序,获取图片采用的是FTP方式;
准备条件:
Linux服务器上创建一个FTP的用户:ftppic
这个账号要有权限才可以,然后编写Windows端代码:
public static boolean downFile(Stringurl,intport, Stringusername,
Stringpassword, StringremotePath, StringfileName,Stringpath
) {
booleansuccess= false;
FTPClientftp= new FTPClient();
intreply;
try{
ftp.connect(url,port);
ftp.login(username,password);
//文件类型为二进制文件
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply= ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
returnsuccess;
}
//本地模式
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(remotePath);
FTPFile[]fs= ftp.listFiles();
for(FTPFileff: fs) {
if(ff.getName().equals(fileName)) {
FilelocalFile= new File(path+ff.getName());
OutputStream is= new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(),is);
is.close();
}
}
ftp.logout();
success= true;
}catch(SocketExceptione) {
e.printStackTrace();
}catch(IOExceptione) {
e.printStackTrace();
}finally{
if(ftp.isConnected()) {
try{
ftp.disconnect();
}catch(IOExceptione) {
e.printStackTrace();
}
}
}
return success;
}
亲测好使。
浙公网安备 33010602011771号