android下载图片代码,Android 中图片的上传和下载代码

本文介绍在Android的WEB服务客户端中,利用HttpClient实现图片上传和下载功能。上传时,客户端将图片转为字节流发送请求,服务器接收字节流保存文件并记录路径;下载时,客户端获取网络图片地址发送请求,将返回的字节流转化为图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在实现一个Android的WEB服务客户端,比如微博,论坛客户端时,经常会使用到图片的上传和下载。在这里介绍如何利用HttpClient实现图片的上传和下载功能。

1 图片上传:上传图片时,首先获得图片的路径,创建文件,并将图片转化为字节流写入到request,并发送该请求。 客户端代码:

File file = new File(imageUrl);

String httpUrl = httpDomain+"AddImageServlet"+"?gid="+gid;

HttpPost request = new HttpPost(httpUrl);

HttpClient httpClient = new DefaultHttpClient();

FileEntity entity = new FileEntity(file,"binary/octet-stream");

HttpResponse response;

try {

request.setEntity(entity);

entity.setContentEncoding("binary/octet-stream");

response = httpClient.execute(request);

//如果返回状态为200,获得返回的结果

if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){

……//图片上传成功

}

}

catch(Exception e){

} 复制代码

服务器端所做的工作则是接收该字节流,写入文件中,并在服务器中相应文件夹中保存该文件,并记录该文件的路径,将图片文件路径写入到数据库中保存。

服务器端代码:

//获得新闻id

String gid = request.getParameter("gid");

String filePath = getRealPath(request) + "\\userpic\\";

// 定义上载文件的最大字节

int MAX_SIZE = 102400 * 102400;

// 声明文件读入类

DataInputStream in = null;

FileOutputStream fileOut = null;

// 取得客户端上传的数据类型

String contentType = request.getContentType();

if(contentType.indexOf("binary/octet-stream") >= 0){

// 读入上传的数据

in = new DataInputStream(request.getInputStream());

int formDataLength = request.getContentLength();

// 如果图片过大

if(formDataLength > MAX_SIZE){

String errormsg=("上传的文件字节数不可以超过" + MAX_SIZE);

out.println(errormsg);

return ;

}

// 保存上传文件的数据

byte dataBytes[] = new byte[formDataLength];

int byteRead = 0;

int totalBytesRead = 0;

// 上传的数据保存在byte数组

while(totalBytesRead < formDataLength){

byteRead = in.read(dataBytes,totalBytesRead,formDataLength);

totalBytesRead += byteRead;

}

String fileName = filePath + gid+".png";

// 检查上载文件的目录是否存在

File fileDir = new File(filePath);

if(!fileDir.exists()){

fileDir.mkdirs();

}

// 创建文件的写出类

fileOut = new FileOutputStream(fileName);

// 保存文件的数据

fileOut.write(dataBytes);

fileOut.close();

//保存文件的路径名

…… 复制代码

2 图片下载:首先获得网络图片的图片地址,发送请求后,服务器将会返回该图片的字节流,利用BitmapFactory.decodeStream()方法将字节流转化为图片并返回。具体代码如下:

//获得网络中的图片

public Bitmap getGossipImage(String gid){

String httpUrl = httpDomain+"userpic/"+gid+".png";

Bitmap bitmap = null;

HttpGet httpRequest = new HttpGet(httpUrl);

//取得HttpClient 对象

HttpClient httpclient = new DefaultHttpClient();

try {

//请求httpClient ,取得HttpRestponse

HttpResponse httpResponse = httpclient.execute(httpRequest);

if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){

//取得相关信息 取得HttpEntiy

HttpEntity httpEntity = httpResponse.getEntity();

InputStream is = httpEntity.getContent();

bitmap = BitmapFactory.decodeStream(is);

is.close();

}else{

Toast.makeText(context, "连接失败!", Toast.LENGTH_SHORT).show();

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return bitmap;

}

复制代码

图片输入输出流的上传下载关键代码 取得HttpServletRequest的InputStream输入流 InputStream is request getInputStream ; 以InputStream输入流为基础 建立一个BufferedReader对象 BufferedReader br new BufferedReader new InputStreamReader is ; String buffer null; 循环读取请求内容的每一行内容 while buffer br readLine null { 如果读到的内容以 开始 且以 结束 表明忆到请求内容的结尾 if buffer endsWith " " &&buffer startsWith " " { 跳出循环 break; } 如果读到的内容以 开始 表明开始了一个表单域 if buffer startsWith " " { 如查下一行内容中有filename字符串 表明这是一个文件域 if br readLine indexOf "filename" > 1 { 跳过两行 开始处理上传的文件内容 br readLine ; br readLine ; 以系统时间为文件名 创建一个新文件 File file new File request getRealPath " " + System currentTimeMillis ; 创建文件输出流 PrintStream ps new PrintStream new FileOutputStream file ; 接着开始读取文件内容 String content null; while content br readLine null { 如果读取的内容以 开始 表明开始了下一个表单域内容 if content startsWith " " { 跳出处理 break; } 否则 将读到的内容输出到文件中 ps println content ; }">图片输入输出流的上传下载关键代码 取得HttpServletRequest的InputStream输入流 InputStream is request getInputStream ; 以InputStream输入流为基础 建立一个BufferedReader对象 BufferedReader br new BufferedReader new InputStreamReader is ; String buff [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值