HttpURLConnection form-data的post方式,提交图片信息

来源:互联网 发布:淘宝微店是什么 编辑:程序博客网 时间:2024/05/22 10:33


package wuxi.mantoo.intelligentbag.util;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.UUID;import org.json.JSONException;import org.json.JSONObject;import wuxi.mantoo.intelligentbag.config.ManUrl;import android.util.Log;public class UpLoadPickture {private final String TAG = "UpLoadPickture";/* sumber server */public void uploadPickture(final String piktrueRoute) {/* * visit server */// next linefinal String newLine = "\r\n";final String boundaryPrefix = "--";// data divisionfinal String BOUNDARY = UUID.randomUUID().toString();new Thread() {public void run() {String target = ManUrl.UPLOAD; // 要提交的目标地址URL url;try {url = new URL(target);HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); // 创建一个HTTP连接urlConn.setRequestMethod("POST"); // 指定使用POST请求方式urlConn.setDoInput(true); // 向连接中写入数据urlConn.setDoOutput(true); // 从连接中读取数据urlConn.setUseCaches(false); // 禁止缓存urlConn.setInstanceFollowRedirects(true); // 自动执行HTTP重定向urlConn.setRequestProperty("connection", "Keep-Alive");urlConn.setRequestProperty("Charset", "UTF-8");urlConn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY); // 设置内容类型DataOutputStream out = new DataOutputStream(urlConn.getOutputStream()); // 获取输出流// 上传文件File file = new File(piktrueRoute);StringBuilder sb = new StringBuilder();sb.append(boundaryPrefix);sb.append(BOUNDARY);sb.append(newLine);/** *文件参数,photo参数名可以随意修改 *photo 为服务器的key *如果服务器设置了这个key就要改成响应的参数*/sb.append("Content-Disposition: form-data;name=\"photo\";filename=\""+ file.getName() + "\"" + newLine);sb.append("Content-Type:application/octet-stream");// 参数头设置完以后需要两个换行,然后才是参数内容sb.append(newLine);sb.append(newLine);// 将参数头的数据写入到输出流中out.write(sb.toString().getBytes());// 数据输入流,用于读取文件数据DataInputStream in = new DataInputStream(new FileInputStream(file));byte[] bufferOut = new byte[1024];int bytes = 0;// 每次读1KB数据,并且将文件数据写入到输出流中while ((bytes = in.read(bufferOut)) != -1) {out.write(bufferOut, 0, bytes);}// 最后添加换行out.write(newLine.getBytes());in.close();// 定义最后数据分隔线,即--加上BOUNDARY再加上--。byte[] end_data = (newLine + boundaryPrefix + BOUNDARY+ boundaryPrefix + newLine).getBytes();// 写上结尾标识out.write(end_data);out.flush(); // 输出缓存out.close(); // 关闭数据输出流Log.i(TAG, "getResponseCode:" + urlConn.getResponseCode());if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) { // 判断是否响应成功InputStreamReader in1 = new InputStreamReader(urlConn.getInputStream(), "utf-8"); // 获得读取的内容,utf-8获取内容的编码BufferedReader buffer = new BufferedReader(in1); // 获取输入流对象String inputLine = null;Log.d(TAG, "inputLine:" + buffer.readLine());while ((inputLine = buffer.readLine()) != null) {Log.d(TAG, inputLine + "\n");try {JSONObject reader = new JSONObject(inputLine);// 使用JSONObject解析JSONObject reObjectData = reader.getJSONObject("data");} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();Log.i(TAG, e.getMessage());}}in1.close(); // 关闭字符输入流}urlConn.disconnect(); // 断开连接} catch (MalformedURLException e) {e.printStackTrace();Log.i(TAG, e.getMessage());} catch (IOException e) {e.printStackTrace();Log.i(TAG, e.getMessage());}};}.start();}}


0 0
原创粉丝点击