コード例 #1
0
        public void Download(object o, int iRow, string localPath, string FileNameWithoutPath, string remotePath, int offset)
        {
            //Tamir.SharpSsh.java.io.InputStream ftpStream = null;
            //Tamir.SharpSsh.java.io.OutputStream ftpStream = null;
            //FileStream outputStream = null;
            try
            {
                string trueFileName = localPath + "\\" + FileNameWithoutPath;

                if (remotePath.StartsWith("."))
                {
                    remotePath = remotePath.Substring(1, remotePath.Length - 1);
                }
                if (remotePath.StartsWith("/"))
                {
                    remotePath = remotePath.Substring(1, remotePath.Length - 1);
                }
                string newFileName = "";
                localPath = localPath + "\\" + remotePath.Substring(remotePath.LastIndexOf("/") + 1, remotePath.Length - remotePath.LastIndexOf("/") - 1);
                if (Renamefile == "Y")
                {
                    newFileName = trueFileName + ".tmp";
                }
                else
                {
                    newFileName = trueFileName;
                }
                if (File.Exists(newFileName) == false)//文件没有了,重新下载
                {
                    offset = 0;
                }
                //SftpATTRS attr =;
                //long cl = m_sftp.stat(remotePath).getSize();//;GetFileSize(remotePath);
                long cl = GetFileSize(remotePath);
                if (cl == 0)
                {
                }

                MethodInfo mi = getFormMethod.getMethod(o.GetType().FullName);
                mi.Invoke(o, new object[] { iRow, String.Format("{0}" + "%", "??") + "--->Downloading " + FileNameWithoutPath, 0 });



                tMonitor = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(RefreshDownloadProgress));

                clsParas cp = new clsParas();
                cp.callType           = "Breakpoint";
                cp.o                  = o;
                cp.iRow               = iRow;
                cp.mi                 = mi;
                cp.totalSize          = cl;
                cp.localFileName      = newFileName;
                tMonitor.IsBackground = true;
                tMonitor.Start(cp);

                try
                {
                    if (offset > 0)
                    {
                        m_sftp.get(remotePath, newFileName, null, 1);
                    }
                    else
                    {
                        m_sftp.get(remotePath, newFileName);
                    }

                    if (m_sftp.Stop)
                    {
                        m_sftp.exit();

                        m_sftp = null;
                        Disconnect();
                    }
                    //    //下面这个方法太慢
                    #region
                    //ftpStream = m_sftp.get(remotePath);
                    ////m_sftp.get(remotePath, ftpStream, null, 1, offset);

                    //int bufferSize = 2048;
                    //int readCount;
                    //int downloadcount = 0;
                    //int irefreshflag = 0;
                    ////if (offset != 0)
                    ////{
                    ////    m_sftp.get(
                    ////    ftpStream.Seek(offset, System.IO.SeekOrigin.Current);
                    ////}
                    //downloadcount = offset;

                    //byte[] buffer = new byte[bufferSize];
                    ////ftpStream.Seek(offset, System.IO.SeekOrigin.Begin);
                    //readCount = ftpStream.Read(buffer, 0, bufferSize);
                    //downloadcount = downloadcount + readCount;


                    //decimal d_persent = 0;

                    //if (offset == 0)
                    //{
                    //    outputStream = new FileStream(newFileName, FileMode.Create);
                    //}
                    //else
                    //{
                    //    outputStream = new FileStream(newFileName, FileMode.Append);
                    //}

                    //while (readCount > 0)
                    //{
                    //    if (Uploaddownloadpauseflag == "Y")
                    //    {
                    //        break;
                    //    }
                    //    outputStream.Write(buffer, 0, readCount);

                    //    if (cl > 0)
                    //    {
                    //        d_persent = Convert.ToDecimal(Convert.ToDecimal(downloadcount) / Convert.ToDecimal(cl));
                    //        mi.Invoke(o, new object[] { iRow, String.Format("{0}" + "%", Convert.ToInt32(d_persent * 100)) + "--->Downloading " + FileNameWithoutPath, downloadcount });
                    //        irefreshflag++;
                    //        if (irefreshflag == 15)
                    //        {
                    //            //循环15次刷新一下界面,防止因为DoEvents而减慢上传速度
                    //            irefreshflag = 0;
                    //            Application.DoEvents();
                    //        }
                    //    }
                    //    readCount = ftpStream.Read(buffer, 0, bufferSize);

                    //    downloadcount = downloadcount + readCount;
                    //}
                    //ftpStream.Close();
                    //outputStream.Close();
                    #endregion


                    //if (Uploaddownloadpauseflag == "Y")
                    //{
                    //    return;
                    //}
                }
                catch (Exception ex)
                {
                    //throw ex;
                    return;
                }
                finally
                {
                    tMonitor.Abort();
                }
                if (m_sftp == null)
                {
                    return;
                }

                mi.Invoke(o, new object[] { iRow, String.Format("{0}" + "%", Convert.ToInt32(1 * 100)) + "--->Downloaded " + FileNameWithoutPath, -1 });

                if (Renamefile == "Y")
                {
                    if (File.Exists(trueFileName))
                    {
                        File.SetAttributes(trueFileName, FileAttributes.Normal);
                        File.Delete(trueFileName);
                    }
                    File.Move(newFileName, trueFileName);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //if (ftpStream != null)
                //{
                //    ftpStream.Close();
                //    ftpStream = null;
                //}
                //if (outputStream != null)
                //{
                //    outputStream.Close();
                //    outputStream.Dispose();
                //}
            }
        }
コード例 #2
0
        public void Download(object o, int iRow, string localPath, string remotePath)
        {
            //Stream ftpStream = null;
            //FileStream outputStream = null;
            try
            {
                if (remotePath.StartsWith("/"))
                {
                    remotePath = remotePath.Substring(1, remotePath.Length - 1);
                }
                string newFileName = "";
                localPath = localPath + "\\" + remotePath.Substring(remotePath.LastIndexOf("/") + 1, remotePath.Length - remotePath.LastIndexOf("/") - 1);
                if (Renamefile == "Y")
                {
                    newFileName = localPath + ".tmp";
                }
                else
                {
                    newFileName = localPath;
                }
                if (File.Exists(newFileName))
                {
                    File.SetAttributes(newFileName, FileAttributes.Normal);
                    File.Delete(newFileName);

                    //throw new Exception(string.Format("The file {0} is already exist, can't download", newFileName));
                }

                //SftpATTRS attr =;
                long cl = m_sftp.stat(remotePath).getSize();//;GetFileSize(remotePath);

                //ftpStream = m_sftp.get(remotePath);
                MethodInfo mi = getFormMethod.getMethod(o.GetType().FullName);
                mi.Invoke(o, new object[] { iRow, String.Format("{0}" + "%", "??") + "--->Downloading " + newFileName, Convert.ToDecimal(0) });



                System.Threading.Thread tMonitor = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(RefreshDownloadProgress));

                clsParas cp = new clsParas();
                cp.callType           = "Normal";
                cp.o                  = o;
                cp.iRow               = iRow;
                cp.mi                 = mi;
                cp.totalSize          = cl;
                cp.localFileName      = newFileName;
                tMonitor.IsBackground = true;
                tMonitor.Start(cp);

                try
                {
                    m_sftp.get(remotePath, newFileName);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    tMonitor.Abort();
                }
                #region 速度慢
                //int bufferSize = 2048;
                //int readCount;
                //int downloadcount;
                //int irefreshflag = 0;

                //byte[] buffer = new byte[bufferSize];
                //readCount = ftpStream.Read(buffer, 0, bufferSize);
                //downloadcount = readCount;

                //MethodInfo mi = getFormMethod.getMethod(o.GetType().FullName);
                //decimal d_persent = 0;

                //outputStream = new FileStream(newFileName, FileMode.Create);
                //while (readCount > 0)
                //{
                //    outputStream.Write(buffer, 0, readCount);

                //    if (cl > 0)
                //    {
                //        d_persent = Convert.ToDecimal(Convert.ToDecimal(downloadcount) / Convert.ToDecimal(cl));
                //        mi.Invoke(o, new object[] { iRow, String.Format("{0}" + "%", Convert.ToInt32(d_persent * 100)) + "--->Downloading " + remotePath, d_persent });
                //        irefreshflag++;
                //        if (irefreshflag == 15)
                //        {
                //            //循环15次刷新一下界面,防止因为DoEvents而减慢上传速度
                //            irefreshflag = 0;
                //            Application.DoEvents();
                //        }
                //    }
                //    readCount = ftpStream.Read(buffer, 0, bufferSize);

                //    downloadcount = downloadcount + readCount;
                //}
                //ftpStream.Close();
                //outputStream.Close();

                //d_persent = Convert.ToDecimal(1);
                #endregion

                mi.Invoke(o, new object[] { iRow, String.Format("{0}" + "%", Convert.ToInt32(1 * 100)) + "--->Downloaded " + remotePath, Convert.ToDecimal(1) });

                if (Renamefile == "Y")
                {
                    if (File.Exists(localPath))
                    {
                        File.SetAttributes(localPath, FileAttributes.Normal);
                        File.Delete(localPath);
                    }
                    File.Move(newFileName, localPath);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //if (ftpStream != null)
                //{
                //    ftpStream.Close();
                //    ftpStream = null;
                //}
                //if (outputStream != null)
                //{
                //    outputStream.Close();
                //    outputStream.Dispose();
                //}
            }
        }