67,515
社区成员
发帖
与我相关
我的任务
分享
public int SynNewsCateAnnex(List urlsList,Map ftpInfoMap) {
int result=1;
FTPClient ftp = new FTPClient();
try {
String localPath = "/newsCatetoryIcon";
String ip = ftpInfoMap.get("ip").toString();
int port = Integer.valueOf(ftpInfoMap.get("port").toString());
String userName = ftpInfoMap.get("userName").toString();
String password = ftpInfoMap.get("password").toString();
ftp.connect(ip, port);
ftp.login(userName, password);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftp.disconnect();
result=0;
} else {
System.out.println("FTP连接成功。");
}
for(int i=0;i<urlsList.size();i++){
File file = new File(urlsList.get(i).toString());
uploadAnnex(ftp, file, localPath);
}
ftp.logout();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result=0;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result=0;
} finally {
try {
if(ftp.isConnected()){
ftp.disconnect();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result=0;
}
}
return result;
}
private void uploadAnnex(FTPClient ftp, File file, String localPath) throws IOException {
FileInputStream fis = null;
OutputStream os = null;
ftp.makeDirectory(localPath); // 设置上传目录
ftp.changeWorkingDirectory(localPath); // 设置把上传的文件放到指定的目录
ftp.setControlEncoding("utf-8");
// 设置文件类型(二进制)
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.deleteFile(file.getName()); // 如果文件在该目录下存在,则删除
fis = new FileInputStream(file);
System.out.println("======"+file.getName());
os = ftp.appendFileStream(file.getName());
byte[] bytes = new byte[(int) file.length()];
int len = 0;
while ((len = fis.read(bytes)) != -1) {
os.write(bytes, 0, len);
}
os.flush();
IOUtils.closeQuietly(fis);
os.close();
}