 所有回答列表(3)
所有回答列表(3)
				下面是我从网上搜到的解决方法,自己使用了是可以的,看看是否是你需要的,你也可以直接搜索关键词“post 图片”,能得到很多解决方法。
public static String formUpload(String urlStr, Map<String, String> textMap, 
            Map<String, String> fileMap) { 
        String res = ""; 
        HttpURLConnection conn = null; 
        String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符 
        try { 
            URL url = new URL(urlStr); 
            conn = (HttpURLConnection) url.openConnection(); 
            conn.setConnectTimeout(5000); 
            conn.setReadTimeout(30000); 
            conn.setDoOutput(true); 
            conn.setDoInput(true); 
            conn.setUseCaches(false); 
            conn.setRequestMethod("POST"); 
            conn.setRequestProperty("Connection", "Keep-Alive"); 
            conn 
                    .setRequestProperty("User-Agent", 
                            "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)"); 
            conn.setRequestProperty("Content-Type", 
                    "multipart/form-data; boundary=" + BOUNDARY); 
   
            OutputStream out = new DataOutputStream(conn.getOutputStream()); 
            // text 
            if (textMap != null) { 
                StringBuffer strBuf = new StringBuffer(); 
                Iterator iter = textMap.entrySet().iterator(); 
                while (iter.hasNext()) { 
                    Map.Entry entry = (Map.Entry) iter.next(); 
                    String inputName = (String) entry.getKey(); 
                    String inputValue = (String) entry.getValue(); 
                    if (inputValue == null) { 
                        continue; 
                    } 
                    strBuf.append("\r\n").append("--").append(BOUNDARY).append( 
                            "\r\n"); 
                    strBuf.append("Content-Disposition: form-data; name=\"" 
                            + inputName + "\"\r\n\r\n"); 
                    strBuf.append(inputValue); 
                } 
                out.write(strBuf.toString().getBytes()); 
            } 
   
            // file 
            if (fileMap != null) { 
                Iterator iter = fileMap.entrySet().iterator(); 
                while (iter.hasNext()) { 
                    Map.Entry entry = (Map.Entry) iter.next(); 
                    String inputName = (String) entry.getKey(); 
                    String inputValue = (String) entry.getValue(); 
                    if (inputValue == null) { 
                        continue; 
                    } 
                    File file = new File(inputValue); 
                    String filename = file.getName(); 
                    String contentType = new MimetypesFileTypeMap() 
                            .getContentType(file); 
                    if (filename.endsWith(".png")) { 
                        contentType = "image/png"; 
                    } 
                    if (contentType == null || contentType.equals("")) { 
                        contentType = "application/octet-stream"; 
                    } 
   
                    StringBuffer strBuf = new StringBuffer(); 
                    strBuf.append("\r\n").append("--").append(BOUNDARY).append( 
                            "\r\n"); 
                    strBuf.append("Content-Disposition: form-data; name=\"" 
                            + inputName + "\"; filename=\"" + filename 
                            + "\"\r\n"); 
                    strBuf.append("Content-Type:" + contentType + "\r\n\r\n"); 
   
                    out.write(strBuf.toString().getBytes()); 
   
                    DataInputStream in = new DataInputStream( 
                            new FileInputStream(file)); 
                    int bytes = 0; 
                    byte[] bufferOut = new byte[1024]; 
                    while ((bytes = in.read(bufferOut)) != -1) { 
                        out.write(bufferOut, 0, bytes); 
                    } 
                    in.close(); 
                } 
            } 
   
            byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes(); 
            out.write(endData); 
            out.flush(); 
            out.close(); 
   
            // 读取返回数据 
            StringBuffer strBuf = new StringBuffer(); 
            BufferedReader reader = new BufferedReader(new InputStreamReader( 
                    conn.getInputStream())); 
            String line = null; 
            while ((line = reader.readLine()) != null) { 
                strBuf.append(line).append("\n"); 
            } 
            res = strBuf.toString(); 
            reader.close(); 
            reader = null; 
        } catch (Exception e) { 
            System.out.println("发送POST请求出错。" + urlStr); 
            e.printStackTrace(); 
        } finally { 
            if (conn != null) { 
                conn.disconnect(); 
                conn = null; 
            } 
        } 
        return res; 
    } 
    
    public static String postMedia(String url,String type,String filePath){
        
        Map<String, String> textMap = new HashMap<String, String>(); 
           
        textMap.put("type", type); 
   
        Map<String, String> fileMap = new HashMap<String, String>(); 
           
        fileMap.put("media", filePath); 
           
        String ret = formUpload(url, textMap, fileMap);
        
        return ret;
    }
    
    
    
    public static void main(String args[]) throws IOException{
        //System.out.println(URLPostContent("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxf7eada91d02985a3&secret=c73e56b6f16c69f0f2107fbb144197e5", null));
        
        String filepath="d:\\500x600.jpg"; 
        
        String urlStr = "http://api.weixin.qq.com/cgi-bin/material/add_material?access_token=lhIHK3OkOvY7GUmKNPtJh8budsOYhKWFGqRqcqg0zPv_UA-e7Nr3pYbUN3T8o4Q_t7ev-7S2u_Xpxci3ZxBSgOoLKJ-UbBBk1U-B-bstGXQ"; 
           
        String ret =  postMedia(urlStr,"image",filepath);
           
        System.out.println(ret); 
        
        
    }
- 等 最代码怎么获取牛币啊?
- 完 谁来告诉我最代码上线的时间,答对者给5牛币,先来先得
- 等 牛友们,大家好,你们做程序员多久了?现在还好吗?
- 完 在微信打开的页面里进行app下载
- 等 最代码2014年欢乐聚声会
- 完 mysql如何查询表数据并且对3个字段降序的SQL?
- 完 最代码牛币机制改革
- 完 成功的在bae上使用了自定义运行环境 jetty+nginx的组合,大家对jetty+nginx优化有哪些心得?
- 完 进来分享一下各位牛牛是如何加入最代码大家庭的?
- 等 为什么java BufferedImage类处理大图直接抛出内存溢出的异常?
- 等 最代码是否开发手机app客户端?
- 完 java程序员学习哪些java的技术?java有哪些框架?都能做哪方面的开发?
- 等 php格式网页文件怎么运行?
- 等 Java volatile值获取的问题
- 等 前端vue,拦截了登录后台后,返回的token,requests拦截token,但是发送请求的时候,就出现跨越异常
- 等 大专本科计算机科班怎么找到Java工作?
- 等 eclipse怎么把三个java swing游戏项目合成一个项目?
- 完 伙伴们,大家都有什么好的解压方式么,分享一下~
- 完 三四线城市,6、7k,运维工作,索然无味,想去辞职上培训,各位牛牛有什么建议嘛
- 等 jsp页面输入中文变成问号
- 等 JPA在线上运行一段时间后报错Caused by: java.lang.IncompatibleClassChangeError: null
- 等 PHP 这个规则用preg_match_all怎么写
- 等 大佬们,有没有知道Alfresco如何配置LDAP登录呢?
- 等 php的install目录是框架带的吗?
 相关问答
相关问答
				 最近浏览
最近浏览
				





 
                 
    