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

Fix:SFtp上传功能修复

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