public int WriteFile( String filename, Byte[] buffer, ref uint writtenBytes, long offset, DokanFileInfo info) { Debug("WriteFile {0} bufferLen {1} Offset {2}", filename, buffer.Length, offset); string path = GetPath(filename); if (path.Contains(":SSHFSProperty.Permission")) { if (offset == 0) { string[] tmp = path.Split(new char[] { ':' }); path = tmp[0]; int permission = 0; permission = Convert.ToInt32(System.Text.Encoding.ASCII.GetString(buffer), 8); WritePermission(path, permission); writtenBytes = (uint)buffer.Length; return(0); } else { writtenBytes = 0; return(0); } } try { ChannelSftp channel = GetChannel(); //GetMonitor monitor = new GetMonitor(buffer.Length); Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path, null, 3 /*HACK: 存在しないモード */, offset); stream.Write(buffer, 0, buffer.Length); stream.Close(); writtenBytes = (uint)buffer.Length; return(0); } catch (IOException) { return(0); } catch (Exception e) { Debug(e.ToString()); return(-1); } }
/// <summary> /// 上傳文件 /// </summary> /// <param name="remotePath">路徑文件夾名稱,不包含sftp://ip:port,只包含路徑文件夾</param> /// <param name="localFileName">本地文件完整路徑</param> public void Upload(string remotePath, string localFileName) { Connect(); FileInfo fileInf = new FileInfo(localFileName); //如果ActionWhileFileExist是Rename string uri = ""; string filename = fileInf.Name.Replace("\\", "/"); if (filename.StartsWith("/")) { filename = filename.Substring(1, filename.Length - 1); } if (remotePath.EndsWith("/")) { remotePath = remotePath.Substring(0, remotePath.Length - 1); } if (remotePath == "/" || remotePath == "") { uri = filename; } else { uri = remotePath + "/" + filename; } if (uri.StartsWith("/")) { uri = uri.Substring(1, uri.Length - 1); } uri = uri.Replace(@"\", "/"); //MessageBox.Show(uri); int buffLength = 2048;// 缓冲大小设置为kb byte[] buff = new byte[buffLength]; int contentLen; FileStream fs = fileInf.OpenRead(); //Tamir.SharpSsh.java.io.InputStream iptStream = new Tamir.SharpSsh.java.io.FileInputStream(Filename);/ Tamir.SharpSsh.java.io.OutputStream optStream = m_sftp.put(uri); try { contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { optStream.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } //如果是上传过程用临时文件名的,到这里已经上传完毕了,把文件名改回去 optStream.close(); filename = null; fileInf = null; } catch (Exception ex) { throw new Exception("上傳失敗:\r\n" + ex.Message); //MessageBox.Show(ex.Message, "Upload Error"); } finally { if (optStream != null) { optStream = null; } if (fs != null) { fs.Flush(); fs.Close(); fs = null; } } }
public void Upload(object o, int iRow, string FtpPathWithoutIP, string Filename, string initialpath, int offset) { FileInfo fileInf = new FileInfo(Filename); //如果ActionWhileFileExist是Rename string uri = ""; string urifordeleteexistfile = ""; string filename = fileInf.FullName.Substring(initialpath.Length + 1, fileInf.FullName.Length - initialpath.Length - 1).Replace("\\", "/"); if (filename.StartsWith("/")) { filename = filename.Substring(1, filename.Length - 1); } if (FtpPathWithoutIP == "/" || FtpPathWithoutIP == "") { uri = filename; urifordeleteexistfile = filename; } else { uri = FtpPathWithoutIP + filename; urifordeleteexistfile = FtpPathWithoutIP + filename; } if (uri.StartsWith("/")) { uri = uri.Substring(1, uri.Length - 1); } //上传已有文件是覆盖还是重命名待上传文件,如果设置是Rename,查询一下文件是否存在,存在则给待上传文件加上时间戳 string trueFileName = ""; if (ActionWhileFileExist == "Rename") { if (CheckFileExist(uri, fileInf.Name.ToString()) == true) { //文件uri重新设置 string extname = fileInf.Extension.ToString(); filename = filename.Substring(0, filename.Length - extname.Length) + "_" + DateTime.Now.ToString("yyyyMMddHHssmm") + extname; if (FtpPathWithoutIP == "/" || FtpPathWithoutIP == "") { uri = filename; } else { uri = FtpPathWithoutIP + filename; } } } //上传文件是否要使用临时文件名,如果要,加上后缀.tmp trueFileName = uri; if (Renamefile == "Y") { uri = uri + ".tmp"; } uri = uri.Replace(@"\", "/"); int buffLength = 2048;// 缓冲大小设置为kb byte[] buff = new byte[buffLength]; int contentLen = 0; int uploadedlen = 0; int irefreshflag = 0; FileStream fs = fileInf.OpenRead(); //Tamir.SharpSsh.java.io.InputStream iptStream = new Tamir.SharpSsh.java.io.FileInputStream(Filename);/ Tamir.SharpSsh.java.io.OutputStream optStream = null; if (offset > 0) { optStream = m_sftp.put(uri, 2); fs.Seek(offset, System.IO.SeekOrigin.Current); uploadedlen = offset; } else { optStream = m_sftp.put(uri); } try { contentLen = fs.Read(buff, 0, buffLength); uploadedlen = uploadedlen + contentLen; MethodInfo mi = getFormMethod.getMethod(o.GetType().FullName); decimal d_persent = 0; // 流内容没有结束 while (contentLen != 0) { if (Uploaddownloadpauseflag == "Y") { break; } optStream.Write(buff, 0, contentLen); d_persent = Convert.ToDecimal(Convert.ToDecimal(uploadedlen) / Convert.ToDecimal(fs.Length)); mi.Invoke(o, new object[] { iRow, String.Format("{0}" + "%", Convert.ToInt32(d_persent * 100)) + "--->Uploading " + fileInf.Name, uploadedlen }); irefreshflag++; if (irefreshflag == 15) { irefreshflag = 0;//循环15次刷新一下界面,防止因为DoEvents而减慢上传速度 Application.DoEvents(); } contentLen = fs.Read(buff, 0, buffLength); uploadedlen = uploadedlen + contentLen; } optStream.close(); if (Uploaddownloadpauseflag == "Y") { return; } d_persent = Convert.ToDecimal(1); mi.Invoke(o, new object[] { iRow, String.Format("{0}" + "%", Convert.ToInt32(d_persent * 100)) + "--->Uploaded " + fileInf.Name, -1 }); //如果是上传过程用临时文件名的,到这里已经上传完毕了,把文件名改回去 if (Renamefile == "Y") { try { Delete(urifordeleteexistfile); } catch { } Rename(uri, trueFileName); } //filenamewithotpath = null; trueFileName = null; filename = null; fileInf = null; } catch (Exception ex) { throw ex; //MessageBox.Show(ex.Message, "Upload Error"); } finally { if (optStream != null) { optStream = null; } if (fs != null) { fs.Flush(); fs.Close(); fs = null; } } }