使用HttpClient下载图片常用代码,以及下载失败原因

最近一直在完善云相册项目,寻找各种bug,昨天突然发现,图片上传经常出现错误,错误有可能与网速有关,但是这种错误却是致命的,我们不能保证所有人的网速都很稳定,于是我就仔细看了下代码,发现异常都是在此处response = httpClient.execute(post);执行上传动作的时候出的错。先对代码做一点说明吧,UploaderParam是一个实体类对象,里面存放的是下载信息(包括本地图片地址),别的部分只要懂httpClient应该都能明白的。(修改:唉,好像还是和网速有点关联啊,大家仅参考下代码吧)

[代码]java代码:

public String uploadFile(UploaderParam uploaderParam) {
		Log.d(TAG, "uploadFile ... ");
		HttpResponse response = null;
		BufferedReader reader = null;
		StringBuilder builder = new StringBuilder();
		final String fileNameIn = uploaderParam.getFileName();
		Log.i(TAG, "upload fileName: "+ fileNameIn);
		try {
			String epgUri = assembleEPGUrl(uploaderParam);
			HttpPost post = new HttpPost(epgUri);
			Log.i(TAG, "httpPost uri: " + epgUri);
			//HttpPost post = new HttpPost("http://10.0.64.132:8001/upload?sid=2&skey=23837391433022");
			MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
			//读取本地图片,以流的形式
			FileInputStream inputStream = new FileInputStream(new File(uploaderParam.getFileName()));
			//final byte[] data = IOUtils.toByteArray(inputStream);
			final int length = inputStream.available();
			Log.i(TAG, "Length is: " + Integer.toString(length));
			InputStreamBody isb = new InputStreamBody(inputStream, "uploaderFile") {
				@Override
				public long getContentLength() {
					return length;
				}
				@Override
				public String getFilename() {
					return fileNameIn;
				}
			};
			multipartEntity.addPart("uploaderFile", isb);
			post.setEntity(multipartEntity);
			System.out.println(" 悲了个剧啊"+post.getURI());
			Log.d("uploadFile", "uploaderParam.getFileName() is : " + uploaderParam.getFileName());
			Thread.sleep(1000);
			try{
				response = httpClient.execute(post);
			}catch(Exception e){
				System.out.println("try again!!");
				Thread.sleep(1000);
				try{
					response = httpClient.execute(post);
				}catch(Exception e1){
					System.out.println("again 失败");
					return null;
				}
			}
			
			Log.d(TAG, "uploadFile .......................................................... ");
			Log.d(TAG, "1" + response.getStatusLine().getStatusCode());
			Log.d(TAG, "2" + response.getStatusLine().getReasonPhrase());
			if(response == null){
				System.out.println("response is null");
			}
			if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
				System.out.println("response.getStatusLine().getStatusCode() != HttpStatus.SC_OK");
			}
			//by LLH
			if(response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				Log.d(TAG, "uploadFile success .......................................................... ");
				HttpEntity entity = response.getEntity();
				reader = new BufferedReader(new InputStreamReader(entity.getContent()));
				String line = null;
				while((line = reader.readLine()) != null) {
					builder.append(line);
				}
				reader.close();
			}
			else
			{
				Log.e(TAG, "execute() return null");
			}
			//EntityUtils.toString(response.getEntity());
			
			Log.d(TAG, "uploadFile result is : " + builder.toString());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return builder.toString();
	}
代码已经贴出来了,不过仅供参考,真正解决下载失败的其实是包问题,我们都知道,httpclient是封装好的协议,远比使用httpconnection方便很多,但是同时也会有很多问题,我们项目一开始引用的就是httpmime-4.0.1.jar,这个包真的拥有不小的问题,下载时很容易出错。于是我重下了个4.2版本,错误就不再产生了。希望我的这篇文章能帮助你们。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
以上文章是12年写的,现在基本都是4.0系统,不提倡使用HttpClient,google官方也已经不维护了吧,不管从哪个角度来讲HttpConnection才是现在最好的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值