zoukankan      html  css  js  c++  java
  • 上传多媒体文件到微信公众平台

    /**
         * 上传多媒体文件到微信公众平台
         *
         * @author w_a
         *
         *         2014-11-27
         *
         * @param fileType
         *            文件类型
         * @param access_token
         *            //在微信平台获取到的凭证
         * @param filename
         *            文件名称
         * @param file
         *            文件流
         * @param content_type
         *            文件类型
         * @param filePath
         *            文件路径
         * @return
         */
        public static JSON UploadMedia(String httpUrl, String fileType, String access_token, String filename, File file, String content_type, String filePath) {

            String result = "";
            String end = " ";
            String twoHyphens = "--"; // 用于拼接
            String boundary = "*****"; // 用于拼接 可自定义
            URL submit = null;
            JSONObject json = null;

            String requestUrl = httpUrl + "?access_token=" + access_token + "&type=" + fileType;
            try {
                submit = new URL(requestUrl);
                HttpURLConnection conn = (HttpURLConnection) submit.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);

                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                // 获取输出流对象,准备上传文件
                DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
                dos.writeBytes(twoHyphens + boundary + end);
                dos.writeBytes("Content-Disposition: form-data; name="" + file + "";filename="" + filename + ";filelength="" + filePath + ";Content-Type="" + content_type + end);
                dos.writeBytes(end);
                // 对文件进行传输
                FileInputStream fis = new FileInputStream(file);
                byte[] buffer = new byte[8192]; // 8k
                int count = 0;
                while ((count = fis.read(buffer)) != -1) {
                    dos.write(buffer, 0, count);
                }
                // 关闭文件流
                fis.close();

                dos.writeBytes(end);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
                dos.flush();

                InputStream is = conn.getInputStream();
                InputStreamReader isr = new InputStreamReader(is, "utf-8");
                BufferedReader br = new BufferedReader(isr);
                result = br.readLine();
                dos.close();
                is.close();

            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("与服务器连接发生异常错误:" + e.toString());
                System.out.println("连接地址是:" + requestUrl);
            }
            // 获取到返回Json请自行根据返回码获取相应的结果
            json = JSONObject.parseObject(result);
            return json;
        }

  • 相关阅读:
    解决关于ArcGIS10.2服务手动启动的问题
    简单利用XSS获取Cookie信息实例演示
    Sublime text 3 如何格式化HTML/css/js代码
    和你一起做一百件事
    两个人在一起,无论做什么都觉得很浪漫。 盘点了情侣之间一定要做的100件事,做完这100件事就结婚吧!
    摩根士丹利
    2016中国app年度排行榜:十大行业、25个领域、Top 500 和2017趋势预测
    国外最受欢迎的十大社交APP网站
    中国境内PEVC投资公司名单
    2015中国10大最佳创业投资人(含50强完整名单)
  • 原文地址:https://www.cnblogs.com/xunfang123/p/4196152.html
Copyright © 2011-2022 走看看