栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

客户端模拟浏览器上传文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

客户端模拟浏览器上传文件

主要场景:图像识别之类的,都需要上传图片,尤其是winform,console等客户端程序

1.单文件/多文件上传 (header头不一样)

使用方法2.1:main

        static void Main(string[] args)
        {
            string path = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "Img");
            string filePath = Path.Combine(path, "1.jpg");
            string token = "632dcb15425e4b2596728951baf1378c3";
            var file = File.ReadAllBytes(filePath);
            string url = "https://adc-wechat-ca154-ocr.cognitiveservices.azure.cn/vision/v3.2/ocr?language=unk&detectOrientation=true&model-version=latest";
            string result = HttpFileUtil.UploadFile(filePath, token, url);
            Console.WriteLine(result);
            Console.Read();




            //文件流进行上传
            WebClient myWebClient = new WebClient();
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            byte[] postArray = br.ReadBytes(Convert.ToInt32(fs.Length));
            myWebClient.Headers.Add("Content-Type", "application/octet-stream");
            myWebClient.Headers.Add("Ocp-Apim-Subscription-Key", token);
            var result1 = myWebClient.UploadData(url, "POST", postArray);
            string str = Encoding.UTF8.GetString(result1);
            Console.WriteLine(str);
        }

使用方法2.2:工具类

//工具类  

  public class HttpFileUtil
    {
        /// 
        /// 单文件上传
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string UploadFile(string localFilePath, string token, string url)
        {
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = CredentialCache.DefaultCredentials;

            try
            {
                myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                myWebClient.Headers.Add("Ocp-Apim-Subscription-Key", token);
                var by = myWebClient.UploadFile(url, "POST", localFilePath);
                string str = Encoding.UTF8.GetString(by);
                return str;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// 
        /// 多文件上传
        /// 
        /// 
        /// 
        /// 
        /// 
        public static bool UploadFile2(string localFilePath, string token, string url)
        {
            string uriString;
            // 得到文件名,文件扩展名字,服务器路径
            // 创建WebClient实例
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = CredentialCache.DefaultCredentials;

            try
            {
                //使用UploadFile方法可以用下面的格式
                myWebClient.Headers.Add("Content-Type", "multipart/form-data");//注意头部必须是form-data
                myWebClient.Headers.Add("Authorization", token);
                var by = myWebClient.UploadFile(url, "POST", localFilePath);
                string s = Encoding.UTF8.GetString(by);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1014246.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号