?? mainframe.java
字號:
System.out.println(cmd);
ftp.sendServer(cmd);
ftp.binary();
int reply = ftp.readServerResponse();//等待返回結果
if(reply==200){
loadRemoteFile();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "刪除文件失敗 異常信息("+e1.getMessage()+")","錯誤",JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
}
}
}
if(e.getSource()==Rproperty){
InstallData d = (InstallData)list_remote.getSelectedValue();
if(d!=null){
FileBean fb = (FileBean)d.getValue();
new PropertyWindow(fb,Rx,Ry).setVisible(true);
}
}
if(e.getSource()==Rload){
loadRemoteFile();
}
if(e.getSource()==Rdownload){
InstallData d = (InstallData)list_remote.getSelectedValue();
if(d!=null){
FileBean fb = (FileBean)d.getValue();
FileDownLoadUtil down = new FileDownLoadUtil();
down.addObserver(this);
if(fb.getType().equals("文件夾")){
down.download(remotePath+fb.getFileName()+"/", localPath+"/"+fb.getFileName(), fb, host, userName, passWord);
}else{
down.download(remotePath, localPath, fb, host, userName, passWord);
}
}
}
}
public class removeTB implements Runnable{
boolean canBeClear = true;
@Override
public void run() {
// TODO Auto-generated method stub
Label:while(ftp.serverIsOpen()){
if(upList!=null&&upList.size()>0){//所有線程已經執行完畢
for(int i=0;i<tb_progress.getRowCount();i++){
if(!"上傳完畢".equals(tb_progress.getModel().getValueAt(i, 4))){
continue Label;
}//end if
}//end for
clearInfo();
} //end if
} //end while
} //end run
}
/***
* 文件上傳處理類
* @author lzkj
*
*/
public class upLoadThread implements Runnable{
private UpLoadBean ub;
private FtpClient ftpClient;
public upLoadThread(UpLoadBean ub){
this.ub = ub;
this.ftpClient = new FtpClient();
ftpClient = getFtp();
}
public void upLoadFile(FileBean fb,String path){
TelnetOutputStream os = null;
FileInputStream is = null ;
try {
ftpClient.cd(path);
ftpClient.binary();
os = ftpClient.put(fb.getFileName());
is = new FileInputStream(new File(fb.getFilePath()));
byte[] bytes = new byte[1024];
int tbt = 0;
int size = (int)fb.getSize();
int c;
long s = System.currentTimeMillis();
long e;
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
e = System.currentTimeMillis()+10;
tbt+=c;
tb_progress.getModel().setValueAt(size<1?"100%":((tbt*100)/size)+"%", ub.getRowIndex(), 2);
tb_progress.getModel().setValueAt(e-s<1?"0 (kb/s)":(1024*tbt)/((e-s)*1000)+" (kb/s)", ub.getRowIndex(), 3);
tb_progress.updateUI();
}
tb_progress.getModel().setValueAt("上傳完畢", ub.getRowIndex(), 4);
tb_progress.updateUI();
// repaint();
os.close();
is.close();
} catch (IOException e1) {
tb_progress.getModel().setValueAt("0%", ub.getRowIndex(), 2);
tb_progress.getModel().setValueAt("0 (kb/s)", ub.getRowIndex(), 3);
tb_progress.getModel().setValueAt("上傳失敗("+e1.getMessage()+")", ub.getRowIndex(), 4);
tb_progress.updateUI();
e1.printStackTrace();
}finally{
try {
if(os!=null&&is!=null){
os.close();
is.close();
// ftpClient.closeServer();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
if(ftpClient.serverIsOpen()){
if(ub.getFb().getType().equals("文件")){
upLoadFile(ub.getFb(),ub.getRemotePath());
}
}//end if
else{//服務器已經關閉
JOptionPane.showMessageDialog(MainFrame.this, "服務器已經關閉,操作失敗","錯誤",JOptionPane.ERROR_MESSAGE);
}
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//end run
}
/**
* 文件下載處理類
* @author lzkj
*
*/
public class downLoadThread implements Runnable{
private FileBean fb;
public downLoadThread(FileBean fb){
this.fb = fb;
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
@Override
public void update(Observable o, Object arg) {
// TODO Auto-generated method stub
if(arg instanceof String){
String temp = (String)arg;
System.out.println(arg);
if(temp.equals("LOCAL_RENAME_OK")){
loadLocalFile();
}
if(temp.equals("REMOTE_RENAME_OK")){
loadRemoteFile();
}
if(temp.equals("DOWNLOAD_FINISH")){
loadLocalFile();
// JOptionPane.showMessageDialog(null, "下載文件成功","提示",JOptionPane.DEFAULT_OPTION);
}
}
}
public boolean upLoad(String Rpath,String fileName,FtpClient ftp){
System.out.println("Rpath:"+Rpath);
System.out.println("fileName:"+fileName);
String localRootPath = PathUtil.rePlace(fileName);
System.out.println("localRootPath:"+localRootPath);
String remoteRootPath = Rpath;
File rootFile = new File(fileName);
if(rootFile.isDirectory()){
if(!createDir(remoteRootPath,PathUtil.getFolderName(fileName),ftp)){
System.out.println("創建遠程根文件夾"+remoteRootPath+fileName+"/失敗");
return false;
}
remoteRootPath = remoteRootPath+PathUtil.getFolderName(fileName)+"/";
String[] fs = rootFile.list();
for(int i=0;i<fs.length;i++){
File subFile = new File(localRootPath+"/"+fs[i]);
if(subFile.isDirectory()){
upLoad(remoteRootPath,localRootPath+"/"+fs[i],ftp);//遞歸
}//end if
else{
//文件上傳
FileBean fb = new FileBean();
fb.setFileName(subFile.getName());
fb.setFilePath(subFile.getAbsolutePath());
fb.setSize(subFile.length());
fb.setTime(new Long(subFile.lastModified()).toString());
fb.setType("文件");
// new Thread(new upLoadThread(fb,remoteRootPath)).start();
UpLoadBean ub = new UpLoadBean();
ub.setFb(fb);
ub.setRemotePath(remoteRootPath);
upList.add(ub);
int size = (int)fb.getSize();
int bk = (size/1024)<1?1:(size/1024);
String bsize = bk+"K";
if(bk>1024){
bk = bk/1024;
bsize = bk+"M";
}
Object[] obj = new Object[]{fb.getFileName(),bsize,"0%","0 (kb/s)","準備上傳"};
((javax.swing.table.DefaultTableModel)(tb_progress.getModel())).addRow(obj);
tb_progress.updateUI();
ub.setRowIndex(RowCount++);
}
}//end for
}else{
//文件上傳
FileBean fb = new FileBean();
fb.setFileName(rootFile.getName());
fb.setFilePath(rootFile.getAbsolutePath());
fb.setSize(rootFile.length());
fb.setTime(new Long(rootFile.lastModified()).toString());
fb.setType("文件");
// new Thread(new upLoadThread(fb,remoteRootPath)).start();
UpLoadBean ub = new UpLoadBean();
ub.setFb(fb);
ub.setRemotePath(remoteRootPath);
upList.add(ub);
int size = (int)fb.getSize();
int bk = (size/1024)<1?1:(size/1024);
String bsize = bk+"K";
if(bk>1024){
bk = bk/1024;
bsize = bk+"M";
}
Object[] obj = new Object[]{fb.getFileName(),bsize,"0%","0 (kb/s)","準備上傳"};
((javax.swing.table.DefaultTableModel)(tb_progress.getModel())).addRow(obj);
tb_progress.updateUI();
ub.setRowIndex(RowCount++);
}
System.err.println(RowCount);
return true;
}
public boolean createDir(String Rpath,String fileName,FtpClient ftp){
boolean flag = false;
if(!isExsitDir(Rpath+fileName,ftp)){
try {
String cmd = "MKD "+Rpath+fileName+"/"+"\r\n";
System.err.println("CMD : "+cmd);
ftp.sendServer(cmd);
ftp.binary();
int reply = ftp.readServerResponse();
System.err.println("Reply : "+reply);
flag = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
public boolean isExsitDir(String dir,FtpClient ftpClient){
try {
ftpClient.cd(dir);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件夾不存在.......................");
// e.printStackTrace();
return false;
}
return true;
}
public FtpClient getFtp(){
try {
FtpClient fc = new FtpClient();
fc.openServer(host);
fc.login(userName, passWord);
return fc;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -