Commit f37d9af2 authored by 王业明's avatar 王业明
Browse files

Fix:SFtp上传功能修复

parent b4305d80
...@@ -159,7 +159,7 @@ public class FtpUploadJob extends Job implements IJobChangeListener { ...@@ -159,7 +159,7 @@ public class FtpUploadJob extends Job implements IJobChangeListener {
} catch (Exception e) { } catch (Exception e) {
if (status == Status.OK_STATUS) if (status == Status.OK_STATUS)
status = new Status(Status.ERROR, FtpActivator.PLUGIN_ID, status = new Status(IStatus.ERROR, FtpActivator.PLUGIN_ID,
FtpActivator.getPluginResource().getString(FtpConstants.FTP_I18N_J_D_E_UP_F) + "\n" FtpActivator.getPluginResource().getString(FtpConstants.FTP_I18N_J_D_E_UP_F) + "\n"
+ e.getMessage()); + e.getMessage());
else else
......
...@@ -6,11 +6,7 @@ import cn.com.bankit.ide.common.ftp.SSHRemoteCall; ...@@ -6,11 +6,7 @@ import cn.com.bankit.ide.common.ftp.SSHRemoteCall;
import cn.com.bankit.ide.common.ftp.console.ConsoleFactory; import cn.com.bankit.ide.common.ftp.console.ConsoleFactory;
import cn.com.bankit.ide.common.ftp.model.ConnNode; import cn.com.bankit.ide.common.ftp.model.ConnNode;
import cn.com.bankit.ide.common.ftp.model.UploadModel; import cn.com.bankit.ide.common.ftp.model.UploadModel;
import cn.com.bankit.ide.common.resources.ResourceProvider;
import java.io.File;
import java.util.List; import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
public class SFtpUploadJob extends FtpUploadJob { public class SFtpUploadJob extends FtpUploadJob {
...@@ -23,6 +19,7 @@ public class SFtpUploadJob extends FtpUploadJob { ...@@ -23,6 +19,7 @@ public class SFtpUploadJob extends FtpUploadJob {
ConsoleFactory.regist(connNode); ConsoleFactory.regist(connNode);
} }
@Override
protected void doRun(IProgressMonitor monitor) throws Exception { protected void doRun(IProgressMonitor monitor) throws Exception {
uploadFile(monitor); uploadFile(monitor);
} }
...@@ -36,7 +33,7 @@ public class SFtpUploadJob extends FtpUploadJob { ...@@ -36,7 +33,7 @@ public class SFtpUploadJob extends FtpUploadJob {
this.total = this.list.size(); this.total = this.list.size();
label244: for (UploadModel up : this.list) for (UploadModel up : this.list)
try { try {
monitor.beginTask( monitor.beginTask(
FtpActivator.getPluginResource() FtpActivator.getPluginResource()
...@@ -51,9 +48,11 @@ public class SFtpUploadJob extends FtpUploadJob { ...@@ -51,9 +48,11 @@ public class SFtpUploadJob extends FtpUploadJob {
this.connNode.setConsoleMessage("文件 " + up.getLocalJavaFile().getAbsolutePath() + "上传到 " this.connNode.setConsoleMessage("文件 " + up.getLocalJavaFile().getAbsolutePath() + "上传到 "
+ this.ssh.getHome() + up.getRemotePath() + "成功"); + this.ssh.getHome() + up.getRemotePath() + "成功");
} }
if (!(monitor.isCanceled())) System.out.println("monitor.isCanceled(): " + monitor.isCanceled());
break label244; if (monitor.isCanceled()){
return; break;
}
//return;
} catch (Exception e) { } catch (Exception e) {
this.connNode.setConsoleError(e.getMessage()); this.connNode.setConsoleError(e.getMessage());
throw e; throw e;
......
package cn.com.bankit.ide.common.ftp.jobs; package cn.com.bankit.ide.common.ftp.jobs;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import javax.swing.filechooser.FileSystemView; import javax.swing.filechooser.FileSystemView;
import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPFile;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent; import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener; import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.Job; import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Display;
import cn.com.bankit.ide.common.ftp.FtpActivator; import cn.com.bankit.ide.common.ftp.FtpActivator;
import cn.com.bankit.ide.common.ftp.FtpConstants; import cn.com.bankit.ide.common.ftp.FtpConstants;
import cn.com.bankit.ide.common.ftp.controls.OverwriteDialog; import cn.com.bankit.ide.common.ftp.controls.OverwriteDialog;
import cn.com.bankit.ide.common.ftp.model.ConnNode; import cn.com.bankit.ide.common.ftp.model.ConnNode;
import cn.com.bankit.ide.common.ftp.views.RemoteView; import cn.com.bankit.ide.common.ftp.views.RemoteView;
public class UploadFileJob extends Job implements IJobChangeListener public class UploadFileJob extends Job implements IJobChangeListener
{ {
private RemoteView rv; private RemoteView rv;
private File localFile; private File localFile;
private String remoteDir; private String remoteDir;
private volatile boolean isOverload = false; private volatile boolean isOverload = false;
private volatile boolean isRemeber = false; private volatile boolean isRemeber = false;
public UploadFileJob(RemoteView rv, File localFile, String remoteDir) public UploadFileJob(RemoteView rv, File localFile, String remoteDir)
{ {
super(FtpActivator.getPluginResource().getString( super(FtpActivator.getPluginResource().getString(
FtpConstants.FTP_I18N_ACTION_UPLOAD)); FtpConstants.FTP_I18N_ACTION_UPLOAD));
this.localFile = localFile; this.localFile = localFile;
this.addJobChangeListener(this); this.addJobChangeListener(this);
this.remoteDir = remoteDir; this.remoteDir = remoteDir;
this.rv = rv; this.rv = rv;
} }
@Override @Override
protected IStatus run(IProgressMonitor monitor) protected IStatus run(IProgressMonitor monitor)
{ {
IStatus status = Status.OK_STATUS; IStatus status = Status.OK_STATUS;
FTPClient ftpClient = null; FTPClient ftpClient = null;
try try
{ {
if (!rv.isDownloadable()) if (!rv.isDownloadable())
{ {
throw new Exception(FtpActivator.getPluginResource().getString( throw new Exception(FtpActivator.getPluginResource().getString(
FtpConstants.FTP_I18N_J_D_E_U_R)); FtpConstants.FTP_I18N_J_D_E_U_R));
} }
// ftpClient = FTPClientFactory.createFTPClient(rv.getNode()); // ftpClient = FTPClientFactory.createFTPClient(rv.getNode());
// ftpClient.connect(rv.getNode().getHost(), rv.getNode().getPort()); // ftpClient.connect(rv.getNode().getHost(), rv.getNode().getPort());
// if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) // if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
// { // {
// if (!ftpClient.login(rv.getNode().getUsername(), rv.getNode().getPassword())) // if (!ftpClient.login(rv.getNode().getUsername(), rv.getNode().getPassword()))
// { // {
// throw new Exception(FtpActivator.getPluginResource().getString(FtpConstants.FTP_I18N_J_D_E_C_F)); // throw new Exception(FtpActivator.getPluginResource().getString(FtpConstants.FTP_I18N_J_D_E_C_F));
// } // }
if (rv.getNode().getStatus() == ConnNode.DISCONNECT) if (rv.getNode().getStatus() == ConnNode.DISCONNECT)
{ {
rv.getNode().aysconnect(true); rv.getNode().aysconnect(true);
Thread.currentThread().sleep(2000); Thread.currentThread();
} Thread.sleep(2000);
ftpClient = rv.getNode().getFtpClient(); }
if (ftpClient != null) ftpClient = rv.getNode().getFtpClient();
{ if (ftpClient != null)
ftpClient.changeWorkingDirectory(remoteDir); {
uploadFile(ftpClient, localFile, monitor); ftpClient.changeWorkingDirectory(remoteDir);
} uploadFile(ftpClient, localFile, monitor);
return status; }
// } return status;
// throw new Exception(FtpActivator.getPluginResource().getString(FtpConstants.FTP_I18N_J_D_E_C_F)); // }
} catch (Exception e) // throw new Exception(FtpActivator.getPluginResource().getString(FtpConstants.FTP_I18N_J_D_E_C_F));
{ } catch (Exception e)
try {
{ try
rv.getNode().reconnect(); {
ftpClient = rv.getNode().getFtpClient(); rv.getNode().reconnect();
if (ftpClient != null) ftpClient = rv.getNode().getFtpClient();
{ if (ftpClient != null)
ftpClient.changeWorkingDirectory(remoteDir); {
uploadFile(ftpClient, localFile, monitor); ftpClient.changeWorkingDirectory(remoteDir);
} uploadFile(ftpClient, localFile, monitor);
return status; }
} catch (IOException e1) return status;
{ } catch (IOException e1)
// TODO Auto-generated catch block {
e = e1; // TODO Auto-generated catch block
} catch (Exception e2) e = e1;
{ } catch (Exception e2)
// TODO Auto-generated catch block {
e = e2; // TODO Auto-generated catch block
} e = e2;
rv.getNode().setConsoleError(e.getMessage()); }
status = new Status(Status.ERROR, FtpActivator.PLUGIN_ID, rv.getNode().setConsoleError(e.getMessage());
FtpActivator.getPluginResource().getString( status = new Status(IStatus.ERROR, FtpActivator.PLUGIN_ID,
FtpConstants.FTP_I18N_J_D_E_UP_F), e); FtpActivator.getPluginResource().getString(
} finally FtpConstants.FTP_I18N_J_D_E_UP_F), e);
{ } finally
// try {
// { // try
// if (ftpClient != null) // {
// { // if (ftpClient != null)
// ftpClient.disconnect(); // {
// ftpClient = null; // ftpClient.disconnect();
// } // ftpClient = null;
// } catch (IOException e) // }
// { // } catch (IOException e)
// e.printStackTrace(); // {
// } // e.printStackTrace();
monitor.done(); // }
} monitor.done();
return status; }
} return status;
}
@Override
public void aboutToRun(IJobChangeEvent event) @Override
{ public void aboutToRun(IJobChangeEvent event)
// TODO Auto-generated method stub {
// TODO Auto-generated method stub
}
}
@Override
public void awake(IJobChangeEvent event) @Override
{ public void awake(IJobChangeEvent event)
// TODO Auto-generated method stub {
// TODO Auto-generated method stub
}
}
@Override
public void done(IJobChangeEvent event) @Override
{ public void done(IJobChangeEvent event)
if (event.getResult() == Status.OK_STATUS) {
{ if (event.getResult() == Status.OK_STATUS)
new RefreshRemoteJob(rv.getNode()).schedule(); {
} new RefreshRemoteJob(rv.getNode()).schedule();
} }
}
@Override
public void running(IJobChangeEvent event) @Override
{ public void running(IJobChangeEvent event)
} {
}
@Override
public void scheduled(IJobChangeEvent event) @Override
{ public void scheduled(IJobChangeEvent event)
// TODO Auto-generated method stub {
// TODO Auto-generated method stub
}
}
@Override
public void sleeping(IJobChangeEvent event) @Override
{ public void sleeping(IJobChangeEvent event)
// TODO Auto-generated method stub {
// TODO Auto-generated method stub
}
}
public File getLocalDir()
{ public File getLocalDir()
return localFile; {
} return localFile;
}
public void setLocalDir(File localDir)
{ public void setLocalDir(File localDir)
this.localFile = localDir; {
} this.localFile = localDir;
}
private void uploadFile(FTPClient ftpClient, final File localFile,
IProgressMonitor monitor) throws Exception private void uploadFile(FTPClient ftpClient, final File localFile,
{ IProgressMonitor monitor) throws Exception
if (monitor.isCanceled()) {
return; if (monitor.isCanceled())
if (localFile.exists() return;
&& FileSystemView.getFileSystemView().isFileSystem(localFile) if (localFile.exists()
&& !isRemeber) && FileSystemView.getFileSystemView().isFileSystem(localFile)
{ && !isRemeber)
ftpClient.enterLocalPassiveMode(); {
for (final FTPFile rf : ftpClient.listFiles()) ftpClient.enterLocalPassiveMode();
{ for (final FTPFile rf : ftpClient.listFiles())
// if (rf.getName().equals(localFile.getName())) {
// { // if (rf.getName().equals(localFile.getName()))
// throw new Exception(FtpActivator.getPluginResource().getString(FtpConstants.FTP_I18N_J_D_E_EXIST) // {
// + rf.getName()); // throw new Exception(FtpActivator.getPluginResource().getString(FtpConstants.FTP_I18N_J_D_E_EXIST)
// } // + rf.getName());
if (rf.getName().equals(localFile.getName())) // }
{ if (rf.getName().equals(localFile.getName()))
Display.getDefault().syncExec(new Runnable() {
{ Display.getDefault().syncExec(new Runnable()
{
@Override
public void run() @Override
{ public void run()
// MessageBox messageBox = new MessageBox(Display.getDefault().getShells()[0], SWT.OK {
// | SWT.CANCEL); // MessageBox messageBox = new MessageBox(Display.getDefault().getShells()[0], SWT.OK
// messageBox // | SWT.CANCEL);
// .setText("FTP" // messageBox
// + FtpActivator.getPluginResource().getString( // .setText("FTP"
// FtpConstants.FTP_I18N_ACTION_DOWNLOAD)); // + FtpActivator.getPluginResource().getString(
// messageBox.setMessage(FtpActivator.getPluginResource().getString( // FtpConstants.FTP_I18N_ACTION_DOWNLOAD));
// FtpConstants.FTP_I18N_OTHER_X_OVRD, // messageBox.setMessage(FtpActivator.getPluginResource().getString(
// new Object[] { rf.getName() })); // FtpConstants.FTP_I18N_OTHER_X_OVRD,
// new Object[] { rf.getName() }));
OverwriteDialog dialog = new OverwriteDialog(
Display.getDefault().getShells()[0], OverwriteDialog dialog = new OverwriteDialog(
FtpActivator Display.getDefault().getShells()[0],
.getPluginResource() FtpActivator
.getString( .getPluginResource()
FtpConstants.FTP_I18N_ACTION_UPLOAD), .getString(
localFile, rf); FtpConstants.FTP_I18N_ACTION_UPLOAD),
int status = dialog.open(); localFile, rf);
isRemeber = dialog.getRemeber(); int status = dialog.open();
isRemeber = dialog.getRemeber();
if (status == IDialogConstants.OK_ID)
isOverload = true; if (status == IDialogConstants.OK_ID)
else isOverload = true;
{ else
isOverload = false; {
} isOverload = false;
} }
}); }
});
if (!isOverload)
return; if (!isOverload)
return;
break;
} break;
} }
if (localFile.isDirectory()) }
{ if (localFile.isDirectory())
ftpClient.makeDirectory(localFile.getName()); {
ftpClient.changeWorkingDirectory(localFile.getName()); ftpClient.makeDirectory(localFile.getName());
for (File f : localFile.listFiles()) ftpClient.changeWorkingDirectory(localFile.getName());
{ for (File f : localFile.listFiles())
uploadFile(ftpClient, f, monitor); {
if (monitor.isCanceled()) uploadFile(ftpClient, f, monitor);
break; if (monitor.isCanceled())
} break;
ftpClient.changeToParentDirectory(); }
} else ftpClient.changeToParentDirectory();
{ } else
ftpClient.enterLocalPassiveMode(); {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode();
monitor.beginTask( ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FtpActivator monitor.beginTask(
.getPluginResource() FtpActivator
.getString( .getPluginResource()
FtpConstants.FTP_I18N_OTHER_X_NOW, .getString(
new Object[] { FtpActivator FtpConstants.FTP_I18N_OTHER_X_NOW,
.getPluginResource() new Object[] { FtpActivator
.getString( .getPluginResource()
FtpConstants.FTP_I18N_ACTION_UPLOAD) }) .getString(
+ localFile.getName(), (int) (localFile FtpConstants.FTP_I18N_ACTION_UPLOAD) })
.length() / 1024)); + localFile.getName(), (int) (localFile
//注意这个是追加不是覆盖 .length() / 1024));
//OutputStream out = ftpClient.appendFileStream(localFile.getName()); //注意这个是追加不是覆盖
//覆盖 //OutputStream out = ftpClient.appendFileStream(localFile.getName());
OutputStream out = ftpClient.storeFileStream(localFile //覆盖
.getName()); OutputStream out = ftpClient.storeFileStream(localFile
if (out == null) .getName());
{ if (out == null)
throw new Exception(FtpActivator.getPluginResource() {
.getString(FtpConstants.FTP_I18N_OTHER_IO_E_W)); throw new Exception(FtpActivator.getPluginResource()
} .getString(FtpConstants.FTP_I18N_OTHER_IO_E_W));
InputStream in = new FileInputStream(localFile); }
byte[] buf = new byte[FtpConstants.FTP_TRANS_PER_SIZE]; InputStream in = new FileInputStream(localFile);
int count = 0; byte[] buf = new byte[FtpConstants.FTP_TRANS_PER_SIZE];
int tail = 0; int count = 0;
while ((count = in.read(buf)) > 0) int tail = 0;
{ while ((count = in.read(buf)) > 0)
out.write(buf, 0, count); {
out.flush(); out.write(buf, 0, count);
tail += count % 1024; out.flush();
monitor.worked(count / 1024 + tail / 1024); tail += count % 1024;
tail = tail % 1024; monitor.worked(count / 1024 + tail / 1024);
if (monitor.isCanceled()) tail = tail % 1024;
break; if (monitor.isCanceled())
} break;
in.close(); }
out.close(); in.close();
ftpClient.completePendingCommand(); out.close();
} ftpClient.completePendingCommand();
} else }
{ } else
throw new Exception(FtpActivator.getPluginResource().getString( {
FtpConstants.FTP_I18N_J_D_E_UP_F_M) throw new Exception(FtpActivator.getPluginResource().getString(
+ localFile.getName()); FtpConstants.FTP_I18N_J_D_E_UP_F_M)
} + localFile.getName());
} }
} }
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment