如题,是上传的时候

解决方案 »

  1.   


            #region 文件上传
           
           /// <summary>
           /// 上传文件(自动分割)
           /// </summary>
           /// <param name="filePath">待上传的文件全路径名称(@"E:\FTP\ftproot\20070228DQCK.zip")</param>
           /// <param name="hostURL">服务器的地址</param>
           /// <param name="byteCount">分割的字节大小</param>        
           /// <param name="userID">主机用户ID</param>
           /// <param name="cruuent">当前字节指针</param>
           /// <returns>成功返回"";失败则返回错误信息</returns>
            public string UpLoadFile(string filePath, string hostURL, int byteCount,string userID,long cruuent)
            {
                string tmpURL = hostURL;
                byteCount = byteCount * 1024;
                //http://localhost:8080/fism/app?service=fileupload&beanId=com.cfcc.fism.service.upload.CollFileSaveServiceImpl&action=upload&filename=AI1215900000020051130411.zip&userid=test&npos=333
                //action=length            System.Net.WebClient WebClientObj = new System.Net.WebClient();
                FileStream fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader bReader = new BinaryReader(fStream);
                long length = fStream.Length;           
                string sMsg = "版式上传成功";
                string fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1);           
                try
                {                #region 续传处理
                    byte[] data;
                    if (cruuent > 0)
                    {
                        fStream.Seek(cruuent, SeekOrigin.Current);
                    }
                    #endregion                  #region 分割文件上传
                    for (; cruuent <= length; cruuent = cruuent + byteCount)
                    { 
                        if (cruuent + byteCount > length)
                        {
                            data = new byte[Convert.ToInt64((length - cruuent))];
                            bReader.Read(data, 0, Convert.ToInt32((length - cruuent)));
                        }
                        else
                        {
                            data = new byte[byteCount];
                            bReader.Read(data, 0, byteCount);
                        }                    try
                        { 
                            LOG.Debug(data);                         //***
                            hostURL = tmpURL + "&action=upload" + "&filename=" + fileName + "&userid=" + userID + "&npos=" + cruuent.ToString();
                            byte[] byRemoteInfo = WebClientObj.UploadData(hostURL, "POST", data);
                            string sRemoteInfo = System.Text.Encoding.Default.GetString(byRemoteInfo);                      //  获取返回信息
                            if (sRemoteInfo.Trim() != "")
                            {
                                sMsg =  sRemoteInfo;
                                break;                        }
                        }
                        catch (Exception ex)
                        {
                            sMsg =  ex.ToString();
                            break;
                        }
                    #endregion                }
                }
                catch (Exception ex)
                {
                    sMsg = sMsg + ex.ToString(); 
                }
                try
                { 
                    bReader.Close();
                    fStream.Close();
                }
                catch (Exception exMsg)
                {
                    sMsg =  exMsg.ToString();
                }            GC.Collect();
                return sMsg;
            } 
            #endregion
       #region 获取文件大小
           /// <summary>
           /// 获取远程服务器文件字节大小
           /// </summary>
           /// <param name="filePath">待上传的文件全路径名称</param>
           /// <param name="hostURL">服务器的地址</param>
           /// <param name="userID">主机用户ID</param>
           /// <returns>远程文件大小</returns>
            public long GetRemoteFileLength(string filePath, string hostURL, string userID)
           {
               long length = 0; 
               System.Net.WebClient WebClientObj = new System.Net.WebClient();  
               
               string fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1);
               
               hostURL = hostURL +"&action=length" + "&filename=" + fileName + "&userid=" + userID + "&npos=0" ;
               
               byte[] data = new byte[0];
               byte[] byRemoteInfo = WebClientObj.UploadData(hostURL , "POST", data);
               string sRemoteInfo = System.Text.Encoding.Default.GetString(byRemoteInfo);//主系统没有作异常处理
               try
               {
                   length = Convert.ToInt64(sRemoteInfo);
               }
               catch (Exception exx)
               {
                   LOG.Error("FileLib类GetRemoteFileLength()中length = Convert.ToInt64(sRemoteInfo)语句异常:" + exx.Message);//我们强制处理异常
                   length = 0;
               }
               GC.Collect();           return length;       }
      

  2.   

    你在进行续传时,先从服务器获取 文件的大小,然后 把这个大小作为 一个 偏移量,从这个位置继续读取本地文件进行上传,可以达到续传的目的,上面的代码是 我用 .Net 和 Servlet 进行续传时的代码,可以作为参考服务器端需要写的方法有两个:(1)获取服务器上的文件大小,Response,(2)续写文件流
      

  3.   

    我写的是公用类啊,并没有限定是 CS 还是 Web 方式的上传,只要调用就可以了啊Web上传的话,用 FileInput 或者其他的脚本来选择 本地文件,然后执行上传代码就可以了