- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在构建一个 swing FTP 客户端。
这是我的 jframe 代码:-
package jframe;
import jframe.swing.download.*;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import test.FtpClient;
import javax.swing.*;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.event.*;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.swing.tree.DefaultMutableTreeNode;
public class UndecoratedFrameDemo {
private static Point point = new Point();
ArrayList arrayList;
static String server = "";
static int port = 21;
static String user = "";
static String pass = "";
JPopupMenu popup;
public UndecoratedFrameDemo()
{
final Ftp_by_apache ftpByApache=new Ftp_by_apache(server,user,pass);
arrayList=ftpByApache.getAllFile("/");
final JFrame frame = new JFrame("FTP Client By MPST");
frame.setUndecorated(false);
frame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
point.x = e.getX();
point.y = e.getY();
}
});
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point p = frame.getLocation();
frame.setLocation(p.x + e.getX() - point.x,
p.y + e.getY() - point.y);
}
});
frame.setSize(700, 500);
frame.setLocation(300, 150);
frame.setLayout(new BorderLayout());
Image icon = new javax.swing.ImageIcon("ftp-big-icon.jpg").getImage();
frame.setIconImage(icon);
Container pane=frame.getContentPane();
pane.setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
menuBar.add(file);
JMenuItem New = new JMenuItem("New Server");
New.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*code for new ftp server*/
}
});
file.add(New);
JMenuItem rename = new JMenuItem("Rename");
New.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*code for rename*/
}
});
file.add(rename);
JMenuItem delete = new JMenuItem("Delete");
New.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*code for delete*/
}
});
file.add(delete);
JMenuItem connect = new JMenuItem("Connect");
New.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*code for connect to differnt ftp*/
}
});
file.add(connect);
JMenuItem item = new JMenuItem("Exit");
item.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
file.add(item);
JMenu edit = new JMenu("Edit");
menuBar.add(edit);
JMenuItem copy = new JMenuItem("Copy");
item.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
/* code for copy*/
}
});
edit.add(copy);
JMenuItem paste = new JMenuItem("Paste");
item.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
/* code for Paste*/
}
});
edit.add(paste);
JMenuItem mkdir = new JMenuItem("New Directory");
New.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
file.add(mkdir);
popup = new JPopupMenu();
JMenuItem popupcopy= new JMenuItem("Copy");
popup.add(popupcopy );
JMenuItem popuprename= new JMenuItem("Rename");
popup.add( popuprename );
JMenuItem popupdelete= new JMenuItem("Delete");
popup.add( popupdelete );
JMenuItem menuItem = new JMenuItem("Open");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
Component c = (Component)event.getSource();
JPopupMenu popup = (JPopupMenu)c.getParent();
JTable table = (JTable)popup.getInvoker();
System.out.println(table.getSelectedRow() + " : " + table.getSelectedColumn());
if(table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()+1).equals("File Folder"))
{
System.out.println("File Folder Clicked");
SwingUtilities.updateComponentTreeUI(frame);
}
else
{
try {
ftpByApache.download(table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()).toString(),"X:/");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
});
popup.add(menuItem);
FileTableModel fileTableModel =new FileTableModel(arrayList) ;
JTable table = new JTable(fileTableModel)
{
public boolean isCellEditable(int data, int column)
{
return false;
}
};
table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("pressed");
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
JTable source = (JTable) e.getSource();
int row = source.rowAtPoint(e.getPoint());
int column = source.columnAtPoint(e.getPoint());
if (!source.isRowSelected(row))
source.changeSelection(row, column, false, false);
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
});
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
public static void main(String[] args) {
new UndecoratedFrameDemo();
}
public String[] getDir(String dir){ //Gets all of the directories from a given directory and returns them as a String Array
String[] directories;
try{
FTPClient ftpClient = new FTPClient();
FTPFile[] objects = ftpClient.listDirectories(dir);
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
directories = new String[objects.length];
for(int i=0; i<objects.length; i++){
directories[i]=objects[i].getName();
}
return directories;
} catch (IOException ex) {
String[] dir1={};
System.out.println("ERROR");
return dir1;
}
}
}
这是我的 ftp 类代码
package jframe;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.imageio.stream.FileImageInputStream;
import org.apache.commons.net.*;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class Ftp_by_apache {
FTPClient f = null;
//
public Ftp_by_apache(String url, String username, String password) {
f = new FTPClient();
//
f.enterLocalPassiveMode();
this.get_connection(url, username, password);
}
public String printTime(String time) {
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyyHHmmss");
try {
String timePart = time.split(" ")[1];
Date modificationTime = dateFormat.parse(timePart);
System.out.println("File modification time: " + modificationTime);
return modificationTime.toString();
} catch (ParseException ex) {
ex.printStackTrace();
return "undefined";
}
}
//
public void get_connection(String url, String username, String password) {
try {
f.connect(url);
System.out.println("connect success!");
f.setControlEncoding("GBK");
boolean login = f.login(username, password);
if (login)
System.out.println("logged In");
else
System.out.println("Login Failed!");
} catch (IOException e) {
e.printStackTrace();
}
}
public void close_connection() {
boolean logout = false;
try {
logout = f.logout();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (logout) {
System.out.println("Log out successfull!");
} else {
System.out.println("try again!");
}
if (f.isConnected())
try {
System.out.println("connection is alive!");
f.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
//
public ArrayList getAllFile(String dir) {
f.enterLocalPassiveMode();
/* try {
f.changeWorkingDirectory(dir);
}
catch (Exception e)
{
e.printStackTrace();
}*/
DateFormat dateFormater = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
ArrayList<ArrayList> arrayList= new ArrayList<ArrayList>();
FTPFile[] files = null;
try {
files = f.listFiles();
if(f.isAvailable())
{
System.out.println("connected");
}
System.out.println("length: "+files.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (FTPFile file : files) {
ArrayList arrayList1=new ArrayList();
arrayList1.add(file.getName());
if (file.isDirectory())
{
arrayList1.add("File Folder");
}
else if (file.getName().endsWith(".exe"))
{
arrayList1.add("Application");
}
else if (file.getName().endsWith(".zip"))
{
arrayList1.add("Compressed (Zip Folder)");
}
else if (file.getName().endsWith(".xls")||file.getName().endsWith(".xlsx")||file.getName().endsWith(".doc")||file.getName().endsWith(".docx")||file.getName().endsWith(".rtf"))
{
arrayList1.add("Microsoft Office Document");
}
else if (file.getName().endsWith(".sql"))
{
arrayList1.add("SQL File");
}
else if (file.getName().endsWith(".txt"))
{
arrayList1.add("Text Document");
}
else if (file.getName().endsWith(".pdf"))
{
arrayList1.add("PDF Document");
}
else
{
arrayList1.add("File");
}
arrayList1.add(dateFormater.format(file.getTimestamp().getTime()));
arrayList1.add(file.getSize()+" Kb");
arrayList.add(arrayList1);
if(file.isDirectory())
System.out.println(file.getName()+": Directory");
if(file.isFile())
System.out.println(file.getName()+": File");
}
System.out.println(arrayList);
return arrayList;
}
//
public void upload(String File_path) throws IOException {
InputStream input = null;
String[] File_name = null;
try {
input = new FileInputStream(File_path);
File_name = File_path.split("\\\\");
System.out.println(File_name[File_name.length - 1]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(File_name[File_name.length - 1]);
f.storeFile(File_name[File_name.length - 1], input);
System.out.println("file uploaded successfully");
if (input != null)
input.close();
}
public void download(String from_file_name, String to_path) throws IOException {
OutputStream output = null;
try {
output = new FileOutputStream(to_path + from_file_name);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
f.retrieveFile(from_file_name, output);
if (output != null) {
try {
if (output != null)
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我的输出就像
这些是我的 ftp 服务器的目录,现在我想在单击父目录时浏览子目录,我还想添加文件下载进度条有人可以帮忙吗?
最佳答案
我已经通过构造函数链接解决了我的问题。
这是第二个构造函数
public UndecoratedFrameDemo(final String childdir,Ftp_by_apache ftpByApache2)
{
final Ftp_by_apache ftpByApache12=ftpByApache2;
arrayList1=ftpByApache12.getSublFile(childdir);
System.out.println("initial directory: " + childdir);
final JFrame frame = new JFrame("FTP Client By MPST");
frame.setUndecorated(false);
frame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
point.x = e.getX();
point.y = e.getY();
}
});
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point p = frame.getLocation();
frame.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
}
});
frame.setSize(700, 500);
frame.setLocation(300, 150);
frame.setLayout(new BorderLayout());
Image icon = new javax.swing.ImageIcon("ftp-big-icon.jpg").getImage();
frame.setIconImage(icon);
Container pane=frame.getContentPane();
pane.setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
menuBar.add(file);
JMenuItem New = new JMenuItem("New Server");
New.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*code for new ftp server*/
}
});
file.add(New);
JMenuItem rename = new JMenuItem("Rename");
New.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*code for rename*/
}
});
file.add(rename);
JMenuItem delete = new JMenuItem("Delete");
New.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*code for delete*/
}
});
file.add(delete);
JMenuItem connect = new JMenuItem("Connect");
New.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
/*code for connect to differnt ftp*/
}
});
file.add(connect);
JMenuItem item = new JMenuItem("Exit");
item.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
file.add(item);
JMenu edit = new JMenu("Edit");
menuBar.add(edit);
JMenuItem copy = new JMenuItem("Copy");
item.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
/* code for copy*/
}
});
edit.add(copy);
JMenuItem paste = new JMenuItem("Paste");
item.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
/* code for Paste*/
}
});
edit.add(paste);
JMenuItem mkdir = new JMenuItem("New Directory");
New.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
file.add(mkdir);
popup = new JPopupMenu();
JMenuItem popupcopy= new JMenuItem("Copy");
popup.add(popupcopy );
JMenuItem popuprename= new JMenuItem("Rename");
popup.add( popuprename );
JMenuItem popupdelete= new JMenuItem("Delete");
popup.add( popupdelete );
JMenuItem menuItem = new JMenuItem("Open");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
Component c = (Component)event.getSource();
JPopupMenu popup = (JPopupMenu)c.getParent();
JTable table = (JTable)popup.getInvoker();
System.out.println(table.getSelectedRow() + " : " + table.getSelectedColumn());
if(table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()+1).equals("File Folder"))
{
System.out.println("File Folder Clicked");
new UndecoratedFrameDemo(childdir+"/"+table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()).toString(),ftpByApache12);
// frame.add(table1);
frame.hide();
}
else
{
try {
ftpByApache12.download(table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()).toString(),"X:/");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
});
popup.add(menuItem);
FileTableModel1 fileTableModel1 =new FileTableModel1(arrayList1) ;
JTable table = new JTable(fileTableModel1)
{
public boolean isCellEditable(int data, int column)
{
return false;
}
};
table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("pressed");
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
JTable source = (JTable) e.getSource();
int row = source.rowAtPoint(e.getPoint());
int column = source.columnAtPoint(e.getPoint());
if (!source.isRowSelected(row))
source.changeSelection(row, column, false, false);
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
});
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
// JFilePicker filePicker = new JFilePicker("Save file to: ","Browse...");
// frame.getContentPane().add(filePicker);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
关于java - 如何浏览 ftp 服务器上的子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27498475/
我遇到一个问题,即我的抓取工具正在跳过没有浏览引荐来源网址的浏览页面。我正在尝试解析 URL 中包含/browse/的所有页面,无论引用者如何。 以下是我的代码(根据 paul t 更新): from
我有4个屏幕。 X,A,B,C。导航模式应如下所示 这是代码 X 屏幕 class X extends StatelessWidget { @override Widget build(Bui
我想用 java 编写一个简单的网络浏览器,这是我的代码! import javax.swing.*; import java.io.*; import java.awt.*; import java
这个问题在这里已经有了答案: A home button in iOS 5, xcode 4.2, Story board (2 个回答) 8年前关闭。 来自 FirstView到SecondView
我使用 C#/ASP.Net 在 IIS7 中创建虚拟目录,以便外部人员可以浏览各种文档。 一切看起来都不错,除了浏览是一种沉闷的文本格式。当他们浏览到文件夹时,我如何创建更多的“Windows 资源
我想我在 Chrome 或 Internet Explorer 中见过 Gmail 这样做,但我从未在 Firefox 中见过它。我想我还是会问一下。是否可以在不需要的情况下进行文件上传?我看到你可以
我是 Java 的新手,作为第一次阅读,我阅读了几本有关 Java 语言的书籍。 我有几个关于 Java 文档的问题。如何“导航”它们?是否可以仅使用 Javadoc 来学习新概念? 这是一个示例 -
我有一个解析网络,现在我想浏览标签,或显示图表。我怎样才能得到图表?或者在树中导航。显示第一步然后其他等。并了解这棵树是如何 build 的。 import urllib from lxml impo
考虑以下情况: 杀戮戒指中的 N 项。需要拉取的项目是项目#k 数值论证解决方案不会真正起作用,因为计算或跟踪杀死环中事物的位置很烦人。 最佳答案 实际问题是什么?按 C-y 然后按 M-y k 次有
这是我之前的 question 的后续。 我试图从 here 了解 Haskell 中的列表拆分示例: foldr (\a ~(x,y) -> (a:y,x)) ([],[]) 我可以阅读 Haske
是否有任何 vim 工具通过 Latex 文档结构提供有效的导航。拥有像 NERDTree 面板这样的东西来表示 latex 文档的部分/子部分结构会非常有用。 最佳答案 扩展 mnosefishs
您好,我的应用程序中有一个模态视图 Controller ,当按下某个按钮时,该 Controller 会消失,并且其中带有 UITableView 的 View 会使用导航 Controller 滑
Glances v2.11.1 with psutil v5.4.3 /usr/lib/python3.6/site-packages/psutil/_pslinux.py:1152: Runtime
我正在使用 Eclipse,我希望我可以通过按 STRG 在以驼峰式书写的单词之间跳转。现在我正在使用 Sublime,我找不到这样做的快捷方式,也找不到实现它的插件。 下面的例子说明了我的问题 aF
我的主程序提示用户浏览文件以便使用 ffmpeg 进行转换。这是文件浏览的格式: 1. Select audio file for conversion ( mp3, wma):
我正在尝试浏览 SVN 存储库,而不必检查它: 是否可以在本地(Unix 上)执行此操作? 这可以通过 ssh 访问实现吗? 最佳答案 svn ls 有效。例如 svn ls http://my.sv
我有一个网站,其中列出了数据库中的企业列表。在每个页面上,您可以执行不同的操作,例如将其转发给 friend 、打印页面等。我的问题是我可以使用谷歌分析来跟踪每个列表的展示次数和浏览次数吗?因此,如果
在 Dart 我有一个嵌套的元素列表 void main() { var diction = {'1':'Alpha','2':'Beta','3':{'x':'Football','y':'
当我们的用户单击网页上的浏览按钮来上传任何文件时,我想更改窗口位置。我会给你一个场景 - 我的 html 表单上有一个浏览按钮,当用户单击一个弹出窗口时,该位置默认为“我的文档”。 实际上我想在单击“
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我是一名优秀的程序员,十分优秀!