📄 ftpsession.java
字号:
ftpclient.login(user,pass);
this.lblStatus.setText("status: login ok");
//set type to ascii
ftpclient.ascii();
ftpclient.cd("/");
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
return true;
}
catch(Exception ex)
{
this.lblStatus.setText("status: login fail to"+host);
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
ex.printStackTrace();
disconnect();
return false;
}
}
//disconnect from ftp server
protected void disconnect(){
if (ftpclient != null)
{
try{
this.lblStatus.setText("status: Disconnecting from "+host);
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
ftpclient.closeServer();
this.lblStatus.setText("status: Disconnect ok");
}
catch(IOException ex)
{
this.lblStatus.setText("status: Disconnect fail.");
ex.printStackTrace();
}
ftpclient = null;
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
//list remote server files
/*protected String getDir(String path) {
String dirName;
int ch;
int begin = 55;
dirName = path.substring(begin).trim();
return dirName;
}
protected void listFTPFile() {
StringBuffer buf = new StringBuffer();
int ch;
remoteList.removeAll();
try {
TelnetInputStream t = ftpclient.list();
t.setStickyCRLF(true);
while ( (ch = t.read()) >= 0) {
if (ch == '\n') {
// remoteList.add(getDir(buf.toString()));
buf.setLength(0);
}
else {
buf.append( (char) ch);
}
}
}
catch (IOException e) {
e.printStackTrace();
}
remoteList.validate();
}
*/
protected void listFTPFile(){
DefaultListModel model = new DefaultListModel();
String strTemp = "",token = "",path ="";
boolean isDirectory = false;
try{
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
this.lblStatus.setText("status:listing....");
//reset the JList
remoteList.setModel(model);
sun.net.TelnetInputStream input = ftpclient.list();
model.addElement("..");
while(true)
{
int c = input.read();
char ch = (char)c;
if (c < 0 || ch =='\n')
{
StringTokenizer stringtoken = new StringTokenizer(strTemp);
int index = 0;
while(stringtoken.hasMoreTokens())
{
token = stringtoken.nextToken();
char mc;
System.out.println(token);
if (index == 0 )
{
mc = token.charAt(0);
System.out.println(mc);
if (mc == 'd')
isDirectory = true;
else
isDirectory = false;
}
if (index == 8)
{
if (isDirectory)
path = token +"/";
else
path = token;
model.addElement(path);
index=0;
}
index++;
}
strTemp = "";
}
if (c <= 0)
break;
strTemp +=ch;
}
input.close();
remoteList.setModel(model);
this.lblStatus.setText("status: List complete.");
}
catch(IOException ioex)
{
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
ioex.printStackTrace();
}
catch(Exception ex)
{
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
ex.printStackTrace();
}
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
//Upload file
protected void upload(){
String localFile = txtFile.getText();
if (!localFile.equals(""))
{
System.out.println("Local File:"+localFile);
String filesep = System.getProperty("file.separator");
String remoteFile = localFile.substring(localFile.lastIndexOf(filesep)+1);
System.out.println("remote file:"+remoteFile);
try{
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
java.io.FileInputStream inFile = new FileInputStream(localFile);
sun.net.TelnetOutputStream outFile = ftpclient.put(remoteFile);
byte[] buffer = new byte[1024];
while(true)
{
int bytes = inFile.read(buffer);
if (bytes < 0)
break;
outFile.write(buffer,0,bytes);
}
inFile.close();
inFile = null;
outFile.close();
outFile = null;
listFTPFile();
this.lblStatus.setText("status: Upload"+txtFile +"successfully");
String dMessage = "Upload" + txtFile + "successfully";
String dTitle = "FTP Upload";
int dType = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component) null,dMessage,dTitle,dType);
}
catch(Exception ex)
{
this.lblStatus.setText("status :Upload " + txtFile +"fail");
if (ex.toString().indexOf("Access is demied")!= -1)
{
String dMessage = "Access is defined";
String dTitle = "ftp Upload error";
int dType = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog((Component) null,dMessage,dTitle,dType);
}
ex.printStackTrace();
System.gc();
}
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
else
{
String dMessage = "please select a local file";
String dTitle = "ftp Upload error";
int dType = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog((Component) null,dMessage,dTitle,dType);
}
}
//download file
protected void download(){
String remoteFile= "";
Object selObj = null;
selObj = remoteList.getSelectedValue();
if (selObj != null)
{
remoteFile = selObj.toString();
}
System.out.println(remoteFile);
if (!remoteFile.endsWith("/")&&!remoteFile.equals("..")&&!remoteFile.equals(""))
{
try{
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
sun.net.TelnetInputStream inFile = ftpclient.get(remoteFile);
java.io.FileOutputStream outFile = new FileOutputStream(remoteFile);
byte[] buffer = new byte[1024];
while(true)
{
int bytes = inFile.read(buffer);
if (bytes < 0)
break;
outFile.write(buffer,0,bytes);
}
outFile.close();
outFile = null;
this.lblStatus.setText("status: Download"+txtFile +"successfully");
String dMessage = "Download" + remoteFile + "successfully";
String dTitle = "FTP Download";
int dType = JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component) null,dMessage,dTitle,dType);
}
catch(Exception ex)
{
this.lblStatus.setText("status :Download" + remoteFile +"fail");
ex.printStackTrace();
System.gc();
}
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
else
{
String dMessage = "please select a remote file";
String dTitle = "ftp Download error";
int dType = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog((Component) null,dMessage,dTitle,dType);
}
}
//set type
protected void setType()
{
try{
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
if (btn_Type.getText().equalsIgnoreCase("ASCII"))
{
ftpclient.ascii();
btn_Type.setText("Binary");
btn_Type.setMnemonic('B');
this.lblStatus.setText("status: Type set to A.");
}
else
{
ftpclient.binary();
btn_Type.setText("ASCII");
btn_Type.setMnemonic('A');
this.lblStatus.setText("status: Type set to I.");
}
}
catch(Exception ex)
{
this.lblStatus.setText("status: Type error");
ex.printStackTrace();
}
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
//change Directory
protected void changeDirectory(){
String dMessage;
String dTitle= "ftp cd error";
int dType = JOptionPane.ERROR_MESSAGE;
String remoteDir = "";
Object selObj = remoteList.getSelectedValue();
String remotePath = selObj.toString();
System.out.println(remotePath);
if (remotePath.endsWith("/")||remotePath.equals(".."))
{
if (remotePath.endsWith("/"))
{
remoteDir = remotePath.substring(0,remotePath.length()-1);
}
else if (remotePath.equals(".."))
{
remoteDir = "..";
}
try{
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
ftpclient.cd(remoteDir);
listFTPFile();
this.lblStatus.setText("status: cd successfully");
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
catch(Exception ex)
{
this.lblStatus.setText("status:cd to "+remoteDir + "fail");
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
ex.printStackTrace();
}
}
else if (!remotePath.equals(""))
{
String linesep = System.getProperty("line.separator");
dMessage = remotePath +"is not a directory "+linesep+"please select a directory";
JOptionPane.showMessageDialog((Component) null,dMessage,dTitle,dType);
}
else if (remotePath.equals(""))
{
dMessage = "Please select a directory";
JOptionPane.showMessageDialog((Component) null,dMessage,dTitle,dType);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -