android http put请求方式,Android_以Http的PUT方法上传图片

/**

* 使用HTTP请求的PUT方式上传图片。

*/

public static void putUploadImage(Bitmap bm, String urlStr, Handler completionHandler) {

new Thread(new Runnable() { //注:网络请求一定要放在子线程里面 否则会遇到请求失败甚至异常。

@Override

public void run() {

String respStr = null;

boolean respStatus = false;

//把Bitmap转Byte数组。

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 75, baos);

byte[] imgBytes = baos.toByteArray();

try {

URL url = new URL(urlStr);

//创建连接。

HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

//设置http连接属性。

urlConn.setDoInput(true); //设置是否从httpUrlConnection读入,默认情况下是true.

urlConn.setDoOutput(true); //http正文内,因此需要设为true,默认情况下是false.

urlConn.setRequestMethod("PUT"); //可以根据需要 设置GET、POST、DELETE、PUT等http提交方式。

//urlConn.setChunkedStreamingMode(0);

//urlConn.setUseCaches(false); //设置缓存(注意设置请求方法为post不能使用缓存) 。

urlConn.setInstanceFollowRedirects(true);

//urlConn.setRequestProperty("Host", "******"); //设置请求的服务器网址/域名,例如***.**.***.***

//urlConn.setRequestProperty("Content-Type", "application/json"); //设定请求格式json,也可以设定xml格式的。

urlConn.setRequestProperty("Content-Type", "image/jpeg");

//urlConn.setRequestProperty("Content-Length", imgBytes.length + ""); //设置文件请求的长度。

//urlConn.setRequestProperty("Accept-Charset", "utf-8"); //设置编码语言。

//urlConn.setRequestProperty("Token", "token"); //设置请求的token.

//urlConn.setRequestProperty("Connection", "keep-alive"); //设置连接的状态。

urlConn.setConnectTimeout(10000); //设置连接超时时间。

urlConn.setReadTimeout(10000); //设置读取超时时间。

//DataOutputStream dataOutputStream = new DataOutputStream(urlConn.getOutputStream()); //ok.

//dataOutputStream.write(imgBytes);

//dataOutputStream.flush();

//dataOutputStream.close();

OutputStream output = urlConn.getOutputStream(); //ok.

//向对象输出流写出数据,这些数据将存到内存缓冲区中。

output.write(imgBytes);

//刷新对象输出流,将任何字节都写入潜在的流中。

output.flush();

//关闭流对象,此后,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中。

output.close();

//读取响应。

if (urlConn.getResponseCode() == 200) {

//从服务器获得一个输入流。

//调用HttpURLConnection连接对象的getInputStream()函数,将内存缓冲区中封装好的完整HTTP请求电文发送到服务端。

InputStreamReader inputStream = new InputStreamReader(urlConn.getInputStream());

BufferedReader reader = new BufferedReader(inputStream);

String lines;

StringBuffer sBuffer = new StringBuffer("");

while ((lines = reader.readLine()) != null) {

lines = new String(lines.getBytes(), "utf-8");

sBuffer.append(lines);

}

reader.close();

respStr = sBuffer.toString();

respStatus = true;

LogUtils.defaultLog("NetworkUtil_上传请求成功_" + respStr + ".");

} else {

LogUtils.defaultLog("NetworkUtil_上传请求失败_" + urlConn.getResponseCode());

}

//断开连接。

urlConn.disconnect();

//回调主线程。

Message msg = completionHandler.obtainMessage();

msg.what = MSG_OK_ID; //消息标识。

msg.obj = respStr; //用来提供额外对象参数。

completionHandler.sendMessage(msg); //发送消息。

} catch (Exception e) {

//e.printStackTrace();

LogUtils.defaultLog("NetworkUtil_上传请求失败_" + e.getMessage());

//回调主线程。

Message msg = completionHandler.obtainMessage();

msg.what = MSG_CUSTOM_ERROR_ID; //消息标识。

msg.obj = e.getMessage(); //用来提供额外对象参数。

completionHandler.sendMessage(msg); //发送消息。

}

}

}).start();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值