⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 foranesourcetableclass.java

📁 具有Ftp的基本功能
💻 JAVA
字号:
package FtpForaneSource;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import com.jcat.ftp.*;

/**
 * 此类为一个第一列带图标和字符串的数据表。
 * @author 张永结。
 *
 */

public class ForaneSourceTableClass extends JTable
{
	private final Icon iconDirectory = new ImageIcon("Directory.png");
	private final Icon iconFile = new ImageIcon("File.png");
	
	/**
	 * 无参构造函数,用于设置数据表第一列渲染器等。
	 */
	public ForaneSourceTableClass()
	{
		super(new MyTableModel());
		setShowGrid(false);
		getColumnModel().getColumn(0).setCellRenderer(new DefaultImagedTableCellRenderer());
		setFillsViewportHeight(true);
	}
	
	/**
	 * 由一个抽象路径名数组创建一个JTable实例。
	 * @param file 抽象路径名数组。
	 */
	public ForaneSourceTableClass(FTPFile[] file)
	{
		this();
		showFTPFile(file);
	}
	
    /**
     * 构造一个JTable来显示 FTPFile[]中抽象路径名的信息。
     * @param file 抽象路径名数组。
     */
	public void showFTPFile(FTPFile[] file)
	{
		MyTableModel tempModel = (MyTableModel)getModel();
		tempModel.setData(getInformation(file));
	} 
	
	/**
	 * 返回一个以Vector为元素的Vector,它的每个元素包含有其抽象路径名表示的文件或目录的图标、名称、大小、类型、最后修改时间,作为数据表信息。
	 * @param file 抽象路径名数组。
	 * @return 返回一个以Vector为元素的Vector,它的每个元素包含有其抽象路径名表示的文件或目录的图标、名称、大小、类型、最后修改时间,作为数据表信息。
	 */
	private Vector<Vector> getInformation(FTPFile[] file)
	{
		
		Vector<Vector> source = new Vector<Vector>();
		
		for(int i=0; i<file.length; i++)
		{
		    ImagedTableCell cell = new ImagedTableCell(getFileIcon(file[i]), file[i].getName());
		    Vector rowsource = new Vector();
			rowsource.addElement(cell);
			rowsource.addElement((getFileSize(file[i])));
			rowsource.addElement(getFileType(file[i]));
			rowsource.addElement(getModifiedTime(file[i]));
			source.addElement(rowsource);
		}
		return source;
	}
	
	/**
	 * 返回表示由此抽象路径名表示的文件或目录的图标,仅分为文件夹和文件两种图标。
	 * @param file 抽象路径名。
	 * @return 表示由此抽象路径名表示的文件或目录的图标,仅分为文件夹和文件两种图标。
	 */
	private Icon getFileIcon(FTPFile file)
	{
		if(file.isDirectory())
		{
			return iconDirectory;
		}
		else
			return iconFile;
	}
	

	
    /**
     * 返回由此抽象路径名表示的文件或目录的类型,若是为文件夹,返回类型“文件夹”;若是为无扩展名的文件,则返回“未知文件”;
     * 其余文件返回由其扩展名加上字符“文件”所表示的字符串。
     * @param file 抽象路径名。
     * @return  由此抽象路径名表示的文件或目录的类型,若是为文件夹,返回类型“文件夹”;若是为无扩展名的文件,则返回“未知文件”;
     * 其余文件返回由其扩展名加上字符“文件”所表示的字符串。
     */
	private String getFileType(FTPFile file)
	{
		if(file.isDirectory())
		{
			return "文件夹";
		}
		else
		{
			int a = file.getName().lastIndexOf(".");
			if(a == -1)
			{
				return "未知文件";
			}
			else
			{
				return file.getName().substring(a+1) + "文件";
			}
		}
	}
	
	/**
	 * 返回表示由此抽象路径名表示的文件的大小字符串,单位分别为bytes、KB、M,保留小数点后两位。
	 * @param file 抽象路径名。
	 * @return 返回表示由此抽象路径名表示的文件的大小字符串,单位分别为bytes、KB、M,保留小数点后两位。
	 */
	private String getFileSize(FTPFile file)
	{
		if(file.isDirectory())
		{
			return "";
		}
		else
		{
			String size;
		    long temp = file.length();
		    DecimalFormat decformat = new DecimalFormat("0.00");
		    
		    if(temp>=0 &&temp <1024)
		    {
		    	return size = temp +"bytes";
		    }
		    else if(temp>=1024 && temp <1024*1024)
		    {
		    	return size = decformat.format(temp/1024) + "KB";
		    }
		    else
		    {
		    	return size = decformat.format(temp/1024/1024) + "M";
			}
		 }
	}
	
	/**
	 * 返回表示由此抽象路径名表示的文件或目录的本地话的最后修改时间的字符串。
	 * @param file 抽象路径名。
	 * @return 表示由此抽象路径名表示的文件或目录的本地话的最后修改时间的字符串。
	 */
	private String getModifiedTime(FTPFile file)
	{
			Date date = new Date(file.lastModified());
			return DateFormat.getDateInstance().format(date);
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -