Android上传图片到服务器

简介:

上传图片到服务器首先要在SD卡上生成一个图片。

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. String path = Environment.getDataDirectory().getPath() + "/data/" +       
  4.       getPackageName() + "/" + "upload.jpg";        
  5. File file = new File(path);        
  6. ByteArrayOutputStream baos = new ByteArrayOutputStream();        
  7. bitMap.compress(Bitmap.CompressFormat.PNG, 85, baos);  //bitMap是要上传的图片Bitmap      
  8. byte[] photoBytes = baos.toByteArray();        
  9. if (!file.exists())        
  10.     file.createNewFile();        
  11. FileOutputStream fos = new FileOutputStream(file);        
  12. fos.write(photoBytes);        
  13. fos.flush();        
  14. fos.close();  

然后再调用下面的方法:

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. /**       
  4. * 上传文件和文字       
  5. * @param aFile       
  6. * @param status       
  7. * @param urlPath       
  8. * @return       
  9. */        
  10. public boolean uploadStatus(File aFile, String status, String urlPath) {        
  11. boolean result = false;        
  12. try {        
  13. URL url = new URL(urlPath);        
  14. HttpURLConnection request = (HttpURLConnection) url        
  15. .openConnection();        
  16. request.setDoOutput(true);        
  17. request.setRequestMethod("POST");        
  18. String boundary = "---------------------------37531613912423";        
  19. String content = "--"        
  20. + boundary        
  21. "\r\nContent-Disposition: form-data; name=\"status\"\r\n\r\n";        
  22. String pic = "\r\n--"        
  23. + boundary        
  24. "\r\nContent-Disposition: form-data; name=\"pic\"; " +        
  25. "filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";        
  26. byte[] end_data = ("\r\n--" + boundary + "--\r\n").getBytes();        
  27. FileInputStream stream = new FileInputStream(aFile);        
  28. byte[] file = new byte[(int) aFile.length()];        
  29. stream.read(file);        
  30. request.setRequestProperty("Content-Type",        
  31. "multipart/form-data; boundary=" + boundary); // 设置表单类型和分隔符        
  32. request.setRequestProperty(        
  33. "Content-Length",        
  34. String.valueOf(content.getBytes().length        
  35. + status.getBytes().length + pic.getBytes().length        
  36. + aFile.length() + end_data.length)); // 设置内容长度        
  37. OutputStream ot = request.getOutputStream();        
  38. ot.write(content.getBytes());        
  39. ot.write(status.getBytes());        
  40. ot.write(pic.getBytes());        
  41. ot.write(file);        
  42. ot.write(end_data);        
  43. ot.flush();        
  44. ot.close();        
  45. request.connect();        
  46. if (200 == request.getResponseCode()) {        
  47. result = true;        
  48. else {        
  49. result = false;        
  50. }        
  51. catch (FileNotFoundException e1) {        
  52. e1.printStackTrace();        
  53. catch (IOException e) {        
  54. e.printStackTrace();        
  55. }        
  56. return result;        
  57. }    

 




     本文转自06peng 51CTO博客,原文链接:http://blog.51cto.com/06peng/962490,如需转载请自行联系原作者




相关文章
|
1月前
|
Ubuntu 网络协议 Java
【Android平板编程】远程Ubuntu服务器code-server编程写代码
【Android平板编程】远程Ubuntu服务器code-server编程写代码
|
23天前
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
21 1
|
1月前
|
Ubuntu 网络协议 Java
在Android平板上使用code-server公网远程Ubuntu服务器编程
在Android平板上使用code-server公网远程Ubuntu服务器编程
|
2月前
|
Ubuntu 网络协议 Linux
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
57 0
|
3月前
|
数据采集 编解码 图形学
Android平台Unity下如何通过WebCamTexture采集摄像头数据并推送至RTMP服务器或轻量级RTSP服务
Android平台Unity下如何通过WebCamTexture采集摄像头数据并推送至RTMP服务器或轻量级RTSP服务
103 0
|
3月前
|
安全 网络协议 Linux
【公网远程手机Android服务器】安卓Termux搭建Web服务器
【公网远程手机Android服务器】安卓Termux搭建Web服务器
65 0
|
4月前
|
API Android开发
[Android]图片加载库Glide
[Android]图片加载库Glide
55 0
|
4月前
|
Android开发
[Android]制作9-Patch图片
[Android]制作9-Patch图片
42 0
|
4月前
|
XML JSON Apache
【Android】如何获得Apache服务器的JSON文件数据
【Android】如何获得Apache服务器的JSON文件数据
61 0
|
前端开发 Java Android开发
Android端通过HttpURLConnection上传文件到服务器
Android端通过HttpURLConnection上传文件到服务器 一:实现原理 最近在做Android客户端的应用开发,涉及到要把图片上传到后台服务器中,自己选择了做Spring3 MVC HTTP API作为后台上传接口,android客户端我选择用HttpURLConnection来通...
1090 0