gpt4 book ai didi

java - 如何使用java按上次更新时间对SFTP文件进行排序?

转载 作者:行者123 更新时间:2023-12-01 10:26:26 26 4
gpt4 key购买 nike

please click here to see the unwanted file name enter image description here我的 FTP 服务器中有一组文本文件。

我想阅读今天之前上传的所有文件。

其中我必须打印最后三个上传文件的属性。(名称、上传时间、大小)。现在我将能够打印 FTP 服务器中存在的文件的名称和属性,但它不是按顺序排列的,并且显示得像垃圾一样。现在我想打印最后三个上传文件的名称、大小、路径、上传时间。

有人可以帮助我实现这一目标吗?

这是我的片段:

package com;
import java.io.File;
import edu.vt.middleware.crypt.io.TeePrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ConnectException;
//import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.comparator.LastModifiedFileComparator;
import org.apache.commons.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.Protocol;
import com.enterprisedt.net.ftp.SecureFileTransferClient;
import java.io.File;
import java.io.FilenameFilter;
import edu.vt.middleware.crypt.io.TeePrintStream;
public class getFilesFTP
{
public static File dir=new File("D:/log_FTPCHECK");
public static String logname="output.txt";
public static File logfile=new File(dir,logname);
public static StringBuffer sb=new StringBuffer();
public static byte[] filesize;
public static void main(String args[]) throws Exception
{
sb.append("*************************************
************************************************
*********************************************************************");
sb.append("<p align=center><B><U>SCOPUS FILE UPLOAD CHECK AUTO
GENERATED LOG REPORT</U></B></p>");
sb.append("***********************************************
****************************************************************
*******************************************");
String host="example.com";
String username="john";
String password="doe";
int count=0;
File Filename;;
Date FileDate;
String invalidfilename=".";
String filetype="";
//DateFormat dateFormat = new SimpleDateFormat("dd/mm/year");
//String date1="";
String Lastmodifieddata="";
String Lastmodifieddata_time="";
Date todayDate;

try{

SimpleDateFormat dateformat=new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat Format_time=new SimpleDateFormat("yyyy/MM/dd
HH:mm:ss");
String timeStamp =
dateformat.format(Calendar.getInstance().getTime());
// System.out.println("I am executed2");
// System.out.println("Todays Date :"+timeStamp );
sb.append(System.lineSeparator());
//sb.append("Todays Date :"+timeStamp );
sb.append(System.lineSeparator());
// System.out.println("I am executed2");
SecureFileTransferClient client=new
SecureFileTransferClient();
client.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);

client.setRemoteHost(host);
client.setUserName(username);
client.setPassword(password);
client.setProtocol(Protocol.SFTP);
client.setRemotePort(22);

client.setContentType(FTPTransferType.BINARY);
// sb.append(System.lineSeparator());
System.out.println("connecting to sftp...");
// sb.append(System.lineSeparator());
sb.append("connecting to sftp...");
sb.append(System.lineSeparator());
client.connect();
System.out.println("SFTP Connection established
successfully.");
sb.append(System.lineSeparator());
sb.append("SFTP Connection established successfully.");
sb.append(System.lineSeparator());

String path1="/sftp/content-providers/tho-e/data/incoming
/scopusbk";
com.enterprisedt.net.ftp.FTPFile[] directroy =
client.directoryList(path1);
System.out.println("Total Number of Files Found
:"+directroy.length);

// Arrays.sort(directroy);
// FTpFileComparator[] comp = new
FTpFileComparator[files.length];

sb.append(System.lineSeparator());
//sb.append("Total Number of Files Found
:"+directroy.length);
sb.append(System.lineSeparator());
int x=0;

for (int i = 0; i < directroy.length; i++)
{
//System.out.println("entered in for loop");
Filename= new File(directroy[i].getName());
FileDate=(Date) directroy[i].lastModified();
//Filetype=getFileExtension(Filename);
// System.out.println("Name:"+Filename);

Lastmodifieddata=dateformat.format(directroy[i].lastModified());

Lastmodifieddata_time=Format_time.format(directroy[i].lastModified());

//filesize=directroy[i].getName().getBytes();
long size = directroy[i].size();

if(timeStamp.equalsIgnoreCase(Lastmodifieddata))


{
if ((directroy[i]).getName().endsWith("txt"))
{

System.out.println("File Name : "+Filename + " ||
Upload Time : "+Lastmodifieddata_time+" || Size : "+size+" kb");
sb.append(System.lineSeparator());
sb.append("File Name : "+Filename + " || Upload Time :
"+Lastmodifieddata_time+" || Size : "+size+" kb");
sb.append(System.lineSeparator());
//System.out.println();


// String path1="/sftp/suppliers/thomdi/signals
/ContentCAR";
// com.enterprisedt.net.ftp.FTPFile[] directroy =
client.directoryList(path1);
count++;
//}
}

}

else
{
//System.out.println("No todays files");
}
}
if(count>0)
{
System.out.println("Total Number of files :"+count);
sb.append(System.lineSeparator());
sb.append("Total Number of file :"+count);
sb.append(System.lineSeparator());

}
else
{
System.out.println("No Files uploaded today....!!!");
sb.append("No Files uploaded today....!!!");
}


// PrintStream out = new PrintStream(new
FileOutputStream("D:/output.txt"));
// System.setOut(out);
if(!logfile.exists())
{
logfile.createNewFile();
}
FileUtils.writeStringToFile(logfile,sb.toString());

FTPMailer.sendmailFTP();

count=0;


}
catch(SecurityException se)
{
System.out.println("Security credentials mismatch
issue...Unable to Login ");
sb.append(System.lineSeparator());
sb.append("Security credentials mismatch issue...Unable to
Login ");
sb.append(System.lineSeparator());
se.printStackTrace();

}
catch(ConnectException ce)
{
System.out.println("Unable to Reach FTP Server..");
System.out.println("Check the Internet Connectivity");
sb.append(System.lineSeparator());
sb.append("Unable to Reach FTP Server..");
sb.append(System.lineSeparator());
sb.append("Check the Internet Connectivity");
sb.append(System.lineSeparator());
}

}
}

|请帮忙,我在 google 上搜索了很多,但无法进行 FTP 文件排序。

任何帮助将不胜感激。

最佳答案

一种解决方案可能是对 FTPFile[] 进行排序按降序排列(假设最后修改时间为上传时间)。

Arrays.sort(ftpfiles, new Comparator<FTPFile>() {
@Override
public int compare(FTPFile o1, FTPFile o2) {
return o2.lastModified().compareTo(o1.lastModified());
}
});

打印最近上传的三个文件(对数组进行排序后)

for (int i = 0; i < 3) {
// amend the output for your needs
System.out.printn(ftpfiles[i]);
}

代码未经测试。根据FTPFile的javadoc编写.

编辑使用免费库版本测试的小片段。

import com.enterprisedt.net.ftp.FTPFile;
...
public static void main(String[] args) throws Exception {
// create an array of dummy files
Calendar cal = GregorianCalendar.getInstance();
FTPFile[] ftpfiles = new FTPFile[5];
cal.set(Calendar.SECOND, 1);
ftpfiles[0] = new FTPFile("raw", "file1", 111, false, cal.getTime());
cal.set(Calendar.SECOND, 5);
ftpfiles[1] = new FTPFile("raw", "file5", 555, false, cal.getTime());
cal.set(Calendar.SECOND, 3);
ftpfiles[2] = new FTPFile("raw", "file3", 333, false, cal.getTime());
cal.set(Calendar.SECOND, 4);
ftpfiles[3] = new FTPFile("raw", "file4", 444, false, cal.getTime());
cal.set(Calendar.SECOND, 2);
ftpfiles[4] = new FTPFile("raw", "file2", 222, false, cal.getTime());

System.out.println("unsorted file list");
for (FTPFile ftpfile : ftpfiles) {
printFileInfo(ftpfile);
}

// sort array by last modification time in descending order
Arrays.sort(ftpfiles, new Comparator<FTPFile>() {
@Override
public int compare(FTPFile o1, FTPFile o2) {
return o2.lastModified().compareTo(o1.lastModified());
}
});

System.out.println("sorted file list");
for (FTPFile ftpfile : ftpfiles) {
printFileInfo(ftpfile);
}

System.out.println("the three recent files only");
for (int i = 0; i < 3; i++) {
printFileInfo(ftpfiles[i]);
}

}

static void printFileInfo(FTPFile ftpfile) {
System.out.printf("name: %s mtime: %s size: %d%n",
ftpfile.getName(),
ftpfile.lastModified(),
ftpfile.size()
);
}

输出

unsorted file list
name: file1 mtime: Fri Feb 12 12:23:01 CET 2016 size: 111
name: file5 mtime: Fri Feb 12 12:23:05 CET 2016 size: 555
name: file3 mtime: Fri Feb 12 12:23:03 CET 2016 size: 333
name: file4 mtime: Fri Feb 12 12:23:04 CET 2016 size: 444
name: file2 mtime: Fri Feb 12 12:23:02 CET 2016 size: 222

sorted file list
name: file5 mtime: Fri Feb 12 12:23:05 CET 2016 size: 555
name: file4 mtime: Fri Feb 12 12:23:04 CET 2016 size: 444
name: file3 mtime: Fri Feb 12 12:23:03 CET 2016 size: 333
name: file2 mtime: Fri Feb 12 12:23:02 CET 2016 size: 222
name: file1 mtime: Fri Feb 12 12:23:01 CET 2016 size: 111

the three recent files only
name: file5 mtime: Fri Feb 12 12:23:05 CET 2016 size: 555
name: file4 mtime: Fri Feb 12 12:23:04 CET 2016 size: 444
name: file3 mtime: Fri Feb 12 12:23:03 CET 2016 size: 333

关于java - 如何使用java按上次更新时间对SFTP文件进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35335826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com