String filepath = "/storage/sdcard0/ShareSDK/cn.sharesdk.demo/cache/pic_glance_back.jpg"; /** * 上传图片 * * @param urlStr 上传服务器地址 * @param textMap * @param fileMap * @return */ public 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); String contentType = getContentType(inputValue); 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; } /** * 获取文件类型 * @param path * @return */ private String getContentType(String path) { File file = new File(path); String type = null; try { type = file.toURL().openConnection().getContentType(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return type; }
使用:
Map<String, String> textMap = new HashMap<String, String>(); textMap.put("name", "testname"); Map<String, String> fileMap = new HashMap<String, String>(); fileMap.put("userfile", filepath); String ret = formUpload(ContactCons.URL_GET_UPLOAD_PHOTO_URL, textMap, fileMap); Log.d("", ret);
相关推荐
http post 上传 图片 doc 音频 视频 等 已封装 直接调用方法即可 (原生网络请求)
纯C实现的HTTP请求(POST/GET),我这边需要通过HTTP请求Get或Post方式请求Json数据,然后解析JSON格式,解析json我使用的第三方库jsoncpp
1.稍微修改可以实现自动上传文件 2.上传文件不受大小限制 ...文件上传过程中可以显示当前上传进度,当前平均上传速度、上传文件已花费时长 上传文件大小和总的文件大小百分比 4。保证文件的稳定传输
C版本的HTTP,可以上传多个文件,可以向服务器发送json字串.自己工程中正在使用.因为也是是从网上找到的,做了一定修改,所以共享出来.
QT post getQT通过HTTP实现图片实时上传和下载并显示,
VC编写自己构造http协议数据的post上传图片类 文档说明
android 拍照上传 SD卡图片上传 Httppost上传
unity通过http上传图片到服务器,上传截屏图片到tomcat服务器
Qt之 Post方法上传图片到服务器两种方式Base64流和File
主要介绍了iOS通过http post上传图片的相关资料,需要的朋友可以参考下
libcurl 提交post表单数据 上传图片 demo 根据网页请求协议,模拟post表单的方式上传数据,并最返回数据进行处理
C语言编写,http请求,上传文件到指定服务器,下载服务器文件,指网络图片地址,下载网络图片实例,里面含有软件和操作说明,不明白可以私信我,看到我会回复的。
HTTP POST上传文件jar,使用jar可以有效帮助你完成图片或文件上传工作
【最新】C++ http post form_data传文件源码,【最新】C++ http post form_data传文件源码,【最新】C++ http post form_data传文件源码
Unity 中通过UnityWebRequest 以POST形式传JSON格式(键值对格式)的参数请求数据。
C# post模拟提交多个图片接收图片并存到存到服务器上。
程序结合易语言模块小西_HTTP应用模块.ec,实现post上传图片。
程序结合易语言模块小西_HTTP应用模块.ec,实现post上传图片。