android实现从app上传图片到服务器

android图片上传

代码如下:

package neu.dreamera.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import neu.dreamera.R;
import neu.dreamera.utils.FileCacheUtil;
import neu.dreamera.utils.HttpConnectionUtil;

public class TestActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_test);

 final String url = "http://*******"; //此处写上自己的URL
 
 final Map<String,Object> paramMap = new HashMap<String, Object>(); //文本数据全部添加到Map里
 paramMap.put("title","test");
 paramMap.put("datetime", "1890-06-08T08:08:00Z");
 paramMap.put("time_str", "my test");
 paramMap.put("place", "120");

 String path = "********"; //此处写上要上传的文件在系统中的路径
 final File pictureFile = new File(path); //通过路径获取文件

 //开启一个新线程,向服务器上传数据
 new Thread(){
 public void run(){
 try{
 HttpConnectionUtil.doPostPicture(url, paramMap, pictureFile);
 } catch(Exception e) {
 e.printStackTrace();
 }
 }
 }.start();
 }
}
package neu.dreamera.utils;

/**
 * Created by 10405 on 2016/6/6.
 */

import android.app.Activity;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HttpConnectionUtil {

 static String BOUNDARY = java.util.UUID.randomUUID().toString();
 static String PREFIX = "--", LINEND = "\r\n";
 static String MULTIPART_FROM_DATA = "multipart/form-data";
 static String CHARSET = "UTF-8";

 public static void doPostPicture(String urlStr, Map<String,Object> paramMap, File pictureFile )
 throws Exception{

 URL url = new URL(urlStr);
 HttpURLConnection conn = (HttpURLConnection) url.openConnection();

 conn.setDoInput(true);// 允许输入
 conn.setDoOutput(true);// 允许输出
 conn.setUseCaches(false);
 conn.setReadTimeout(10 * 1000); // 缓存的最长时间
 conn.setRequestMethod(POST);

 conn.setRequestProperty("Charset", "UTF-8");
 conn.setRequestProperty("Connection", "Keep-Alive");
 conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);


 DataOutputStream os = new DataOutputStream(conn.getOutputStream());

 StringBuilder sb = new StringBuilder(); //用StringBuilder拼接报文,用于上传图片数据
 sb.append(PREFIX);
 sb.append(BOUNDARY);
 sb.append(LINEND);
 sb.append("Content-Disposition: form-data; name=\"picture\"; filename=\"" + pictureFile.getName() + "\"" + LINEND);
 sb.append("Content-Type: image/jpg; charset=" + CHARSET + LINEND);
 sb.append(LINEND);
 os.write(sb.toString().getBytes());
 InputStream is = new FileInputStream(pictureFile);

 byte[] buffer = new byte[1024];
 int len = 0;
 while ((len = is.read(buffer)) != -1) {
 os.write(buffer, 0, len); //写入图片数据
 }
 is.close();
 os.write(LINEND.getBytes());

 StringBuilder text = new StringBuilder();
 for(Map.Entry<String,Object> entry : paramMap.entrySet()) { //在for循环中拼接报文,上传文本数据
 text.append("--");
 text.append(BOUNDARY);
 text.append("\r\n");
 text.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");
 text.append(entry.getValue());
 text.append("\r\n");
 }
 os.write(text.toString().getBytes("utf-8")); //写入文本数据

 // 请求结束标志
 byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
 os.write(end_data);
 os.flush();
 os.close();

 // 得到响应码
 int res = conn.getResponseCode();
 System.out.println("asdf code "+ res);
 System.out.println("asdf " + conn.getResponseMessage());
 conn.disconnect();
 }

}

未经允许不得转载:安度博客 » android实现从app上传图片到服务器

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址