栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android 通过Base64上传图片到服务器实现实例

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 通过Base64上传图片到服务器实现实例

Android 通过base64上传图片到服务器

之前做上传图片是采用HttpServlet上传,不过用了一下base64上传图片后,感觉比HttpServlet方便很多,大家也可以跟着尝试一下。

前台图片处理:(传Bitmap对象即可)

 
public String Bitmap2StrBybase64(Bitmap bit){ 
  ByteArrayOutputStream bos=new ByteArrayOutputStream(); 
  bit.compress(CompressFormat.JPEG, 40, bos);//参数100表示不压缩 
  byte[] bytes=bos.toByteArray(); 
  return base64.encodeToString(bytes, base64.DEFAULT); 
} 

 前台发送数据:(調用setImgByStr()方法,第一個參數imgStr 为Bitmap转成base64的字符串,第二个参数imgName为图片的名字,包含后缀名.jpg)

public static String host = "http://192.168.1.166:8080/ImageServer/"; 
 
public static String getContent(String url) throws Exception { 
 
  StringBuilder sb = new StringBuilder(); 
 
  HttpClient client = new DefaultHttpClient(); 
  HttpParams httpParams = client.getParams(); 
  // 设置网络超时参数 
  HttpConnectionParams.setConnectionTimeout(httpParams, 3000); 
 
  HttpConnectionParams.setSoTimeout(httpParams, 5000); 
  HttpResponse response = client.execute(new HttpGet(url)); 
  HttpEntity entity = response.getEntity(); 
  if (entity != null) { 
    BufferedReader reader = new BufferedReader(new InputStreamReader( 
 entity.getContent(), "UTF-8"), 8192); 
 
    String line = null; 
    while ((line = reader.readLine()) != null) { 
      sb.append(line + "n"); 
    } 
    reader.close(); 
 
  } 
 
  return sb.toString(); 
} 
public static HttpResponse post(Map params, String url) { 
 
  HttpClient client = new DefaultHttpClient(); 
  HttpPost httpPost = new HttpPost(url); 
  httpPost.addHeader("charset", HTTP.UTF_8); 
  httpPost.setHeader("Content-Type", 
      "application/x-www-form-urlencoded; charset=utf-8"); 
  HttpResponse response = null; 
  if (params != null && params.size() > 0) { 
    List namevaluepairs = new ArrayList(); 
    for (String key : params.keySet()) { 
      namevaluepairs.add(new BasicNamevaluePair(key, (String) params 
   .get(key))); 
    } 
    try { 
      httpPost.setEntity(new UrlEncodedFormEntity(namevaluepairs, 
   HTTP.UTF_8)); 
      response = client.execute(httpPost); 
    } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } catch (RuntimeException e) { 
      e.printStackTrace(); 
    } 
  } else { 
    try { 
      response = client.execute(httpPost); 
    } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
  } 
 
  return response; 
 
} 
public static Object getValues(Map params, String url) { 
  String token = ""; 
  HttpResponse response = post(params, url); 
  if (response != null) { 
    try { 
      token = EntityUtils.toString(response.getEntity()); 
      response.removeHeaders("operator"); 
    } catch (ParseException e) { 
      e.printStackTrace(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
  } 
  return token; 
} 
public static Object setImgByStr(String imgStr,String imgName){ 
  String url = host+"channel-uploadImage.action"; 
  Map params = new HashMap(); 
  params.put("imgStr", imgStr); 
  params.put("imgName", imgName); 
  return getValues(params, url); 
} 

 后台接收数据:

public void uploadPhoto() { 
  //获取存储路径 
  HttpServletRequest request = ServletActionContext.getRequest(); 
  String path = ServletActionContext.getServletContext().getRealPath("/")+"upload"; 
  File file = new File(path); 
  if(!file.exists()){ 
    file.mkdir(); 
  } 
  String imgPath = path + request.getParameter("imgName"); 
  String imgStr = request.getParameter("imgStr"); 
  boolean flag = string2Image(imgStr, imgPath); 
  JacksonUtil.responseJSON(response, flag); 
} 

 后台图片处理:


 
public boolean string2Image(String imgStr, String imgFilePath) { 
  // 对字节数组字符串进行base64解码并生成图片 
  if (imgStr == null) 
    return false; 
  try { 
    // base64解码 
    byte[] b = new base64Decoder().decodeBuffer(imgStr); 
    for (int i = 0; i < b.length; ++i) { 
      if (b[i] < 0) { 
 // 调整异常数据 
 b[i] += 256; 
      } 
    } 
    // 生成Jpeg图片 
    OutputStream out = new FileOutputStream(imgFilePath); 
    out.write(b); 
    out.flush(); 
    out.close(); 
    return true; 
  } catch (Exception e) { 
    return false; 
  } 
}   

OK ! 如果成功上传前端会接收到true ,反之失败false。希望对大家有所帮助!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/158739.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号