public static HttpStatusCode getUrlRequestStatusCode(string methodUrl, string userCode /*上传人编号*/, string editCode /*采集人编号*/,
                                                             string uploadName /*上传文件名号*/, string uploadPath /*上传路径*/, string createTime /*文件创建时间*/,
                                                             string fileSize /*文件大小*/, string fileTime /*文件时长*/, string appCode, out string responseText)
        {
            //return “0;/1/101/103/” 表示成功 或者 “1;失败信息”
            string tagUrl = methodUrl + "&userCode=" + userCode + "&editCode=" + editCode + "&uploadName=" + uploadName
                            + "&filePath=" + uploadPath + "&createTime=" + createTime + "&fileSize=" + fileSize + "&fileTime=" + fileTime + "&appCode=" + appCode;;
            CookieCollection cookies = new CookieCollection();//如何从response.Headers["Set-Cookie"];中获取并设置CookieCollection的代码略

            HttpWebResponse response = HttpWebResponseUtility.CreateGetHttpResponse(tagUrl, null, null, cookies);

            Stream       resStream = response.GetResponseStream();                           //返回从 Internet 资源返回数据流
            StreamReader sr        = new StreamReader(resStream, System.Text.Encoding.UTF8); //实例华一个流的读写器

            responseText = sr.ReadToEnd();                                                   //这就是百度首页的HTML哦 ,字符串形式的流的其余部分(从当前位置到末尾)。如果当前位置位于流的末尾,则返回空字符串 ("")
            resStream.Close();                                                               //关闭当前流并释放与之关联的所有资源
            sr.Close();


            return(response.StatusCode);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="methodUrl">方法url</param>
        /// <param name="userCode"></param>
        /// <param name="editCode"></param>
        /// <param name="appCode"></param>
        /// <param name="responseText"></param>
        /// <returns></returns>
        public static HttpStatusCode getFtpDirRequestStatusCode(string methodUrl, string userCode /*上传人或采集终端编号*/, string editCode /*采集人编号*/, string appCode /*防伪编号*/, out string responseText)
        {
            //return “0;/1/101/103/” 表示成功 或者 “1;失败信息”

            string           tagUrl  = methodUrl + "&userCode=" + userCode + "&editCode=" + editCode + "&appCode=" + appCode;
            CookieCollection cookies = new CookieCollection();

            HttpWebResponse response = HttpWebResponseUtility.CreateGetHttpResponse(tagUrl, null, null, cookies);

            responseText = response.GetResponseStream().ToString();

            Stream       resStream = response.GetResponseStream();                           //返回从 Internet 资源返回数据流
            StreamReader sr        = new StreamReader(resStream, System.Text.Encoding.UTF8); //实例华一个流的读写器

            responseText = sr.ReadToEnd();                                                   //这就是百度首页的HTML哦 ,字符串形式的流的其余部分(从当前位置到末尾)。如果当前位置位于流的末尾,则返回空字符串 ("")
            resStream.Close();                                                               //关闭当前流并释放与之关联的所有资源
            sr.Close();


            return(response.StatusCode);
        }
Exemplo n.º 3
0
        private void UploadWork()
        {
            //LogManager.showNotifaction("ereqrew");
            //string str1 = LogManager.getTimeString();
            //string str = LogManager.getTimeString() + Path.GetExtension("c:\\123\\345\\222\\12346.jpg");
            //str.Equals("");



            //启动时先查看当前目录下是否有未上传完成的文件
            if (Directory.Exists(System.Environment.CurrentDirectory + "\\Data\\"))
            {
                List <string> listUnUploadFiles = new List <string>();
                calcFileCountAndAdd(System.Environment.CurrentDirectory + "\\Data\\", listUnUploadFiles);
                FtpList.AddRange(listUnUploadFiles);
            }


            FtpClient ftpClient = new FtpClient(m_ftpSever, m_ftpUser, m_ftpPwd, 120, int.Parse(m_ftpPort), m_ftpBuffer);

            //for test
            //ftpClient.Upload(FtpList[0], true);//true,断点续传 使用工作站中的文件上传,防止U盘被拔掉
            //string remoteRanameFile1 = LogManager.getTimeStringFileExtension(FtpList[0]);
            //ftpClient.RenameFile(Path.GetFileName(FtpList[0]), remoteRanameFile1, true);//上传后立刻重命名 格式20120312100624_001

            while (threadRunFlag)
            {
                Thread.Sleep(1);
                for (int i = 0; i < FtpList.Count; i++)
                {
                    if (FtpList.Count <= 0 || !threadRunFlag)
                    {
                        Thread.Sleep(1000);
                        continue;
                    }

                    Thread.Sleep(1);

                    string localFileName = FtpList[i];

                    try
                    {
                        ftpClient.Login();

                        //ftpClient.Download("0218620121203103434.mp4", "123.mp4", true);//断点下载
                        //LogManager.WriteLog("正在上传文件: " + );
                        //ftpClient.Upload(@"E:\company\MD\自动上传软件\执法记录仪资料\mp4\0218620121203103434.mp4",true);//断点续传


                        //从接口获取文件FTP的上传路径
                        string interface_Ftp    = m_interfaceStr + "?method=getFtpPath";
                        string interface_Upload = m_interfaceStr + "?method=uploadFile";
                        string responseText     = "";
                        string removeDir        = ""; //服务器返回的FTP文件存放路径
                        string removeDir1       = "";
                        string removeFileName   = ""; //上传到FTP上的文件绝对路径 1/101/103/11.mp4
                        try
                        {
                            if (HttpWebResponseUtility.getFtpDirRequestStatusCode(interface_Ftp, m_WorkStationID, MDUsb.getPoliceIDFromFile(localFileName), m_AppCode, out removeDir1)
                                != System.Net.HttpStatusCode.OK)
                            {
                                //接口调用失败,停止下面的工作,直接退出
                                writeMsg("错误,路径接口调用失败,请检查系统设备配置、网络及服务器!");
                                continue;
                            }
                        }
                        catch (Exception ex)
                        {
                            writeMsg("错误,接口调用失败,请检查系统设备配置、网络及服务器!");
                            LogManager.WriteErrorLog(ex.Message);
                            continue;
                        }

                        string[] list1 = removeDir1.Substring(removeDir1.LastIndexOf('\n') + 1).Split(';');
                        if (!list1[0].Equals("0"))
                        {
                            LogManager.showErrorMsg("错误," + list1[1]);
                            continue;
                        }

                        removeDir = list1[1];



                        //切换到服务器接口返回的工作目录
                        ftpClient.MakeDirs(removeDir);
                        //ftpClient.ChangeDir(removeDir);

                        //上传文件
                        writeMsg("正在上传文件: " + localFileName);
                        ftpClient.Upload(localFileName, true);                                         //true,断点续传 使用工作站中的文件上传,防止U盘被拔掉
                        string remoteRanameFile = LogManager.getTimeStringFileExtension(localFileName);
                        ftpClient.RenameFile(Path.GetFileName(localFileName), remoteRanameFile, true); //上传后立刻重命名 格式20120312100624_001


                        //获得文件的播放时间
                        string fileDuration = FFMpegUtility.getMediaPlayTime(localFileName);

                        removeFileName = removeDir + remoteRanameFile;// 包含全ftp路径,例:1/2/201202201223210044.jpg


                        FileInfo fileInfo = new FileInfo(localFileName);
                        try
                        {
                            //调用平台上传接口
                            if (HttpWebResponseUtility.getUrlRequestStatusCode(interface_Upload, m_WorkStationID, MDUsb.getPoliceIDFromFile(localFileName),
                                                                               Path.GetFileName(localFileName), removeFileName, MDUsb.getDataTimeFromFile(localFileName), fileInfo.Length.ToString(), fileDuration,
                                                                               m_AppCode, out responseText)
                                != System.Net.HttpStatusCode.OK)
                            {
                                //接口调用失败,停止下面的工作,直接退出
                                writeMsg("错误,上传接口调用失败,请检查系统设备配置、网络及服务器!");
                                continue;
                            }
                        }
                        catch (Exception ex)
                        {
                            writeMsg("错误,接口调用失败,请检查系统设备配置、网络及服务器!");
                            LogManager.WriteErrorLog(ex.Message);
                            continue;
                        }
                        string[] list2 = responseText.Substring(responseText.LastIndexOf('\n') + 1).Split(';');
                        if (!list2[0].Equals("0"))
                        {
                            LogManager.showErrorMsg("错误," + list2[1]);
                            continue;
                        }


                        //上传成功删除队列及本地文件
                        FtpList.Remove(localFileName);
                        writeMsg("文件上传服务器成功" + localFileName);
                        File.Delete(localFileName);
                        writeMsg("删除已上传文件" + localFileName);
                    }
                    catch (Exception ex)
                    {
                        LogManager.WriteLog(ex.Message);
                        Thread.Sleep(1000);
                        continue;
                    }
                }
            }
        }