http发送图片,字节数组的方式
来源:互联网 发布:淘宝能发货到台湾吗 编辑:程序博客网 时间:2024/06/05 20:29
1.根据HttpURLConnection上传
public static JSONObject httpUploadImg(String postUrl,byte[] imgBuffer){URL url = new URL(postUrl);StringBuffer buffer = new StringBuffer();HttpURLConnection conn = (HttpURLConnection) url.openConnection();String boundary = "--------httpUploadImg";Map<String , byte[]> byteparams = new HashMap<String, byte[]>();DataOutputStream ds;StringBuilder sb = new StringBuilder();conn.setDoOutput(true);conn.setUseCaches(false);conn.setConnectTimeout(CONNECTION_TIMEOUT);conn.setRequestMethod("POST");conn.setRequestProperty("Content-Type", sb.append("multipart/form-data; boundary=").append(boundary).toString());byteparams.put("null.jpg", imgBuffer);try { conn.connect(); } catch(SocketTimeoutException e) { throw new RuntimeException(); }ds = new DataOutputStream(conn.getOutputStream());writeByteParams(ds, byteparams, boundary);paramsEnd(ds, boundary);InputStream inputStream = conn.getInputStream();InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String str = null;while ((str = bufferedReader.readLine()) != null) {buffer.append(str);}bufferedReader.close();inputStreamReader.close();// 释放资源inputStream.close();inputStream = null;conn.disconnect();JSONObject jsonObject = (JSONObject) JSONValue.parse(buffer.toString());return jsonObject;}
private static void writeByteParams(DataOutputStream ds, Map<String , byte[]> byteparams, String boundary) throws Exception{Set keySet = byteparams.keySet();for(Iterator it = keySet.iterator(); it.hasNext(); ds.writeBytes("\r\n")){String name = (String)it.next();byte[] value = (byte[])byteparams.get(name);ds.writeBytes((new StringBuilder()).append("--").append(boundary).append("\r\n").toString()); ds.writeBytes((new StringBuilder()).append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(URLEncoder.encode(name, "UTF8")).append("\"\r\n").toString()); //是jpg的格式 ds.writeBytes((new StringBuilder()).append("Content-Type: img/jpg").append("\r\n").toString()); ds.writeBytes("\r\n"); ds.write(value);}}private static void paramsEnd(DataOutputStream ds, String boundary)throws Exception { ds.writeBytes((new StringBuilder()).append("--").append(boundary).append("--").append("\r\n").toString()); ds.writeBytes("\r\n"); }
2.httpClient发送图片
public static JSONObject httpUploadImg(String postUrl,byte[] imgBuffer){HttpPost httpPost = new HttpPost(postUrl);MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create();mpEntity.addBinaryBody("upfile", imgBuffer, ContentType.DEFAULT_BINARY, "null.jpg");httpPost.setEntity(mpEntity.build());// 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); HttpResponse response;StringBuilder string = new StringBuilder();try {response = httpclient.execute(httpPost);HttpEntity resEntity = response.getEntity();BufferedReader in = new BufferedReader(new InputStreamReader(resEntity.getContent())); String inputLine = null; while((inputLine = in.readLine()) != null){string.append(inputLine);}in.close();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {httpclient.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}JSONObject jsonObject = (JSONObject) JSONValue.parse(string.toString());return jsonObject;}
0 0
- http发送图片,字节数组的方式
- 发送HTTP请求的两种方式
- 用HTTP的PUT方式发送请求
- C#发送Post方式的Http请求
- 使用post的方式发送http请求
- 图片转为Byte字节数组 字节数组生成图片
- 通过字节流的方式将图片上传服务器
- 获取网页图片字节数组
- 图片以字节数组的形式存到数据库
- 图片文件和byte字节数组的转换
- java接收图片数据的字节数组并保存
- Flex 图片与字节数组的互转
- 将图片转化为字节数组更好的方法
- java 对象 转换成字节数组发送给服务器,根据服务器返回的字节数组自动组装成java对象。
- 页面发送http请求方式
- Java发送HTTP的POST和GET方式请求
- 以DELETE方式发送带请求体的HTTP请求
- C#模拟发送http get、post请求的方式
- 常见前端面试题及答案(下)
- HTML学习
- HTML5,让 Ajax 改变 URL 且支持后退
- 控制台不断输入数字,输入end的时候输出最大值
- 【蓝桥单片机】CT107D错误笔记
- http发送图片,字节数组的方式
- 【BZOJ 3626】【LNOI 2014】LCA
- linux时区校正命令
- 坑!自己走过的坑...
- Python学习笔记 --- dict中的keys(), values() 取出来的值为什么是无序的?
- 文字垂直居中、垂直方向向下移动的方法
- 解决用户重复提交问题
- Protostuff工具实现ProtoBuf序列化使用详解
- 跟小博老师一起学Servlet ——Servlet之COOKIE