Esempio n. 1
0
 // 文件类型
 private static string getUrl(UnFileInfo fi)
 {
     string url = "";
     switch (fi.extens)
     {
         case "jpg,jpeg,png,gif,bmp":
             url = Img1_Domain + fi.fullName.Replace(Img1_Path, "");
             break;
         case "txt,doc,dot,xls,ppt,pdf":
             url = Img1_Domain + "?m=" + fi.md5;
             break;
         case "swf,mp3,mp4,wav,wmv,midi,avi,rmvb,3gp,mov,mpeg":
             url = Img1_Domain + "?m=" + fi.md5;
             break;
     }
     return url;
 }
Esempio n. 2
0
        /// <summary>
        /// 文件属性
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static UnFileInfo attributes(this UnFileEvent t)
        {
            string home = UnFile.getHomeDirectory();
            // 缓存
            string caches = home + "/Caches";
            // 重要文件
            string important = home + "/Important";
            // 永久文件
            string preference = home + "/Preference";
            // 临时文件
            string tmp = home + "/Tmp";
            // 错误日志
            string log = home + "/Log";

            UnFileInfo ufa = new UnFileInfo();
            switch (t)
            {
                case UnFileEvent.caches:
                    ufa.parentFloder = "";
                    ufa.directoryName = caches + "/";
                    break;
                case UnFileEvent.important:
                    ufa.parentFloder = DateTime.Now.ToString("yyyy-MM-dd");
                    ufa.directoryName = important + "/" + ufa.parentFloder;
                    break;
                case UnFileEvent.preference:
                    ufa.parentFloder = DateTime.Now.ToString("yyyy-MM-dd");
                    ufa.directoryName = preference + "/" + ufa.parentFloder;
                    break;
                case UnFileEvent.tmp:
                    ufa.parentFloder = "";
                    ufa.directoryName = tmp + "/";
                    break;
                case UnFileEvent.log:
                    ufa.parentFloder = DateTime.Now.ToString("yyyy-MM-dd");
                    ufa.directoryName = log + "/" + ufa.parentFloder;
                    break;
                default:
                    ufa.parentFloder = "";
                    ufa.directoryName = tmp + "/";
                    break;
            }
            return ufa;
        }
Esempio n. 3
0
        /// <summary>
        /// 上传
        /// </summary>
        private void up()
        {
            HttpWebRequest request = UnHttpHelp.createPost(_url, _timeOut, null, _eve.text());
            UnFileInfo _f = new UnFileInfo(_fileName);
            request.Headers["md5"] = _f.md5;
            request.Headers["extens"] = _f.extens;

            using (FileStream fs = new FileStream(_fileName, FileMode.Open))
            {
                request.ContentLength = fs.Length;
                BinaryReader r = new BinaryReader(fs);
                // 每次读取大小
                int bufferLength = 1024;
                byte[] buffer = new byte[bufferLength];

                // 开始发送
                long fSize = fs.Length;
                long dSize = 0;
                int size = r.Read(buffer, 0, bufferLength);
                using (Stream post = request.GetRequestStream())
                {
                    while (size > 0)
                    {
                        dSize += size;
                        post.Write(buffer, 0, size);
                        size = r.Read(buffer, 0, bufferLength);

                        UnAttrPgs pgs = new UnAttrPgs();
                        pgs.pid = _pid;
                        pgs.totalLength = fSize;
                        pgs.length = dSize;
                        this.tryProgress(pgs);
                    }
                }
            }
            // 开始传输
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    byte[] back = UnHttpHelp.getResponseData(response);
                    UnAttrRst rst = new UnAttrRst();
                    rst.pid = _pid;
                    rst.code = 1;
                    rst.msg = "通讯成功";
                    rst.back = en.GetString(back);
                    rst.data = rst.back;
                    bool _bool = this.trySuccess(rst);
                }
            }
            catch(Exception e)
            {
                UnAttrRst rst = new UnAttrRst();
                rst.pid = _pid;
                rst.code = -1;
                rst.msg = e.ToString();
                this.tryError(rst);
            }
        }
Esempio n. 4
0
        /// <summary>
        ///  发送
        /// </summary>
        /// <returns>返回结果</returns>
        private UnAttrRst send()
        {
            UnAttrRst rst = new UnAttrRst();
            UnAttrPgs pgs = new UnAttrPgs();
            try
            {
                byte[] send = en.GetBytes(_msg);
                // md5加密文件名
                string md5 = (_msg + _url).md5Hash();
                string path = UnFileEvent.caches.fullPath() + md5 + ".txt";
                // 缓存文件
                UnFileInfo f = new UnFileInfo(path, null, null, _cacheTimeOut);
                if (_cacheTimeOut > 0 && f.exists && !f.isLate)
                {
                    // 进度
                    pgs.pid = _pid;
                    pgs.totalLength = f.length;
                    pgs.length = f.length;
                    this.tryProgress(pgs);

                    // 完成
                    rst.pid = _pid;
                    rst.code = 2;
                    rst.msg = "返回缓存";
                    using (FileStream inf_fs = new FileStream(f.fullName, FileMode.Open))
                    {
                        byte[] data = new byte[inf_fs.Length];
                        inf_fs.Read(data, 0, data.Length);
                        rst.back = en.GetString(data);
                        rst.data = rst.back;
                    }
                    this.trySuccess(rst);
                    return rst;
                }

                // 设置参数
                HttpWebRequest request = UnHttpHelp.createPost(_url, _timeOut, "text/xml", _eve.text(), _cerPath, _cerPass);
                request.ContentLength = send.Length;
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(send, 0, send.Length);
                }

                // 下载数据
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    byte[] back = UnHttpHelp.getResponseData(response);

                    pgs.pid = _pid;
                    pgs.totalLength = back.Length;
                    pgs.length = back.Length;
                    this.tryProgress(pgs);

                    rst.pid = _pid;
                    rst.code = 1;
                    rst.msg = "提交成功";
                    rst.back = en.GetString(back);
                    rst.data = rst.back;
                    bool b = this.trySuccess(rst);
                    if (_cacheTimeOut > 0 && b)
                    {
                        UnFile.createDirectory(UnFileEvent.caches);
                        using (FileStream inf_fs = new FileStream(f.fullName, FileMode.Create))
                        {
                            inf_fs.Seek(0, SeekOrigin.Begin);
                            inf_fs.Write(back, 0, (int)back.Length);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                rst.pid = _pid;
                rst.code = -1;
                rst.msg = e.ToString();
                this.tryError(rst);
            }
            return rst;
        }
Esempio n. 5
0
        /// <summary>
        /// 下载
        /// </summary>
        private void down()
        {
            UnAttrPgs pgs = new UnAttrPgs();
            UnAttrRst rst = new UnAttrRst();
            try
            {
                string kzm = _url.Substring(_url.LastIndexOf("."));
                if (kzm.Length < 2)
                {
                    kzm = ".no";
                }
                // md5加密文件名
                string md5 = _url.md5Hash();
                string path = UnFileEvent.caches.fullPath() + md5 + kzm;
                // 缓存文件
                UnFileInfo f = new UnFileInfo(path, null, null, _cacheTimeOut);
                if (_cacheTimeOut > 0 && f.exists && !f.isLate)
                {
                    pgs.pid = _pid;
                    pgs.totalLength = f.length;
                    pgs.length = f.length;
                    this.tryProgress(pgs);

                    rst.pid = _pid;
                    rst.code = 2;
                    rst.msg = "返回缓存";
                    rst.back = path;
                    rst.data = f;
                    this.trySuccess(rst);
                    return;
                }

                // 下载数据
                using (HttpWebResponse response = UnHttpHelp.creageGet(_url, _timeOut).GetResponse() as HttpWebResponse)
                {
                    using (Stream rsps = response.GetResponseStream())
                    {
                        UnFile.createDirectory(UnFileEvent.caches);
                        UnFile.createDirectory(UnFileEvent.tmp);
                        //创建本地文件写入流
                        using (Stream stream = new FileStream(f.fullNameTmp, FileMode.Create))
                        {
                            long fSize = response.ContentLength;
                            long dSize = 0;
                            byte[] buff = new byte[1024];
                            int size = rsps.Read(buff, 0, buff.Length);
                            while (size > 0)
                            {
                                dSize += size;
                                stream.Write(buff, 0, size);
                                size = rsps.Read(buff, 0, buff.Length);

                                pgs.pid = _pid;
                                pgs.totalLength = fSize;
                                pgs.length = dSize;
                                this.tryProgress(pgs);
                            }
                        }
                    }
                }

                // 转正式文件
                if (f.exists)
                {
                    f.baseInfo.Delete();
                }
                File.Move(f.fullNameTmp, f.fullName);
                f = new UnFileInfo(f.fullName);
                rst.pid = _pid;
                rst.code = 1;
                rst.msg = "下载完成";
                rst.data = f;
                bool b = this.trySuccess(rst);
                // 不缓存
                if (!b)
                {
                    f.baseInfo.Delete();
                }
            }
            catch (Exception e)
            {
                rst.pid = _pid;
                rst.code = -1;
                rst.msg = e.ToString();
                this.tryError(rst);
            }
        }
Esempio n. 6
0
        // 获取文件
        public void getFile(HttpContext context)
        {
            UnAttrRst rst = new UnAttrRst();
            string type = context.Request["type"];
            long allowSize = 0;
            string allowType = "";
            switch (type)
            {
                case "Image":
                    allowSize = 1024 * 1024;
                    allowType = ".jpg,.jpeg,.png,.gif,.bmp";
                    break;
                case "File":
                    allowSize = 1024 * 1024;
                    allowType = ".txt,.rar,.zip";
                    break;
                default:
                    break;
            }
            HttpPostedFile hpf = context.Request.Files[0];
            if (hpf.ContentLength > 0)
            {
                if (hpf.ContentLength <= allowSize)
                {
                    string ext = System.IO.Path.GetExtension(hpf.FileName).ToLower();
                    var imp = UnFileEvent.important;
                    var name = UnStrRan.getRandom() + ext;
                    string UpPath = imp.fullPath() + "/" + name;
                    DirectoryInfo di = new DirectoryInfo(UnFileEvent.important.fullPath());
                    if (di.Exists == false)
                    {
                        di.Create();
                    }
                    hpf.SaveAs(UpPath);

                    UnFileInfo fi = new UnFileInfo(UpPath, null, allowType, null);
                    if (!fi.isWrongExtens && !fi.isWrongType)
                    {
                        rst.pid = 0;
                        rst.code = 1;
                        rst.msg = "上传成功";

                        BackInfo bi = new BackInfo();
                        switch (type)
                        {
                            case "Image":
                                bi.ReturnCode = 1;
                                bi.ReturnMsg = "传输完成";
                                bi.Url = ImgPath + imp.floder() + "/" + name;
                                break;
                            case "File":
                                bi.ReturnCode = 2;
                                bi.ReturnMsg = "传输完成";
                                bi.UNCode = "UNCODE//:" + UnEncDES.encrypt(UpPath);
                                break;
                            default:
                                break;
                        }
                        rst.back = UnXMMPXml.tToXml(typeof(BackInfo), bi);
                    }
                    else
                    {
                        File.Delete(fi.fullName);
                        rst.pid = 0;
                        rst.code = -3;
                        rst.msg = "允许类型:" + allowType;
                    }
                }
                else
                {
                    rst.pid = 0;
                    rst.code = -2;
                    rst.msg = "不能超过 " + allowSize / 1024 + "KB";
                }
            }
            else
            {
                rst.pid = 0;
                rst.code = -1;
                rst.msg = "文件不存在";
                rst.back = "";
            }

            context.Response.ContentType = "text/xml";
            var xml = UnXMMPXml.tToXml(typeof(UnAttrRst), rst);
            context.Response.Write(xml);
        }
Esempio n. 7
0
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="s"></param>
        /// <param name="exps"></param>
        /// <param name="max"></param>
        /// <param name="md5"></param>
        /// <returns></returns>
        public static UnFileInfo saveFile(Stream s, string exps, long max, string md5)
        {
            UnFileInfo fa = new UnFileInfo();
            // 校验文件大小
            if (s.Length > max)
            {
                fa.isTooLarge = true;
                return fa;
            }
            // 校验文件类型
            string kzm = "no";
            List<string> exts = UnFile.expandedFormFile(s);
            foreach (string ext in exts)
            {
                if (("," + exps + ",").IndexOf("," + ext + ",") > -1)
                {
                    kzm = ext;
                    break;
                }
            }

            if (exps != null && kzm == "no")
            {
                fa.isWrongExtens = true;
                return fa;
            }

            byte[] buffer = new byte[s.Length];
            s.Read(buffer, 0, buffer.Length);
            FileInfo fi = UnFile.createFile(UnFileEvent.important, md5 + "." + kzm, true);
            using (FileStream fs = new FileStream(fi.FullName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                fs.Write(buffer, 0, buffer.Length);
                fs.Flush();
                fs.Close();
            }
            s.Close();
            fa = new UnFileInfo(fi.FullName);
            return fa;
        }
Esempio n. 8
0
 /// <summary>
 /// 移动文件
 /// </summary>
 /// <param name="oldPath"></param>
 /// <param name="newPath"></param>
 /// <param name="isCorver"></param>
 /// <returns></returns>
 public static bool move(string oldPath, string newPath, bool isCorver)
 {
     FileInfo oldFile = new FileInfo(oldPath);
     if (!oldFile.Exists)
     {
         return false;
     }
     UnFileInfo newInfo = new UnFileInfo(newPath);
     DirectoryInfo di = new DirectoryInfo(newInfo.directoryName);
     if (!di.Exists)
     {
         di.Create();
     }
     // 存在则删除
     FileInfo newFile = new FileInfo(newPath);
     if (newFile.Exists)
     {
         // 覆盖
         if (isCorver)
         {
             newFile.Delete();
         }
         else
         {
             return false;
         }
     }
     File.Move(oldPath, newPath);
     return true;
 }