Android学习笔记_13_网络通信之多个上传文件

一、获取HTTP协议:

  建立一个Web项目,建立一个如下所示的jsp界面,用IE捕获表单提交信息。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="<%=request.getContextPath() %>/FileUpload" method="post" enctype="multipart/form-data">
        <input type="text" name="name" > <br>
        <input type="file" name="file1"> <br>
        <input type="file" name="file2"> <br>
        <input type="submit" value=" submit ">
    </form>
</body>
</html>

  通过IE捕获信息如下:

POST /Simple/FileUpload HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/QVOD, application/QVOD, application/x-shockwave-flash, 
application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, 
application/vnd.ms-xpsdocument, application/xaml+xml, */*
Referer: http://192.168.8.103:8080/Simple/1.jsp
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; YYGameAll_1.2.167057.92; Windows NT 5.1; Trident/4.0; GTB6.4; .NET CLR 3.0.04506.648; 
.NET CLR 3.5.21022; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C)
Content-Type: multipart/form-data; boundary=---------------------------7dd2ce2338056a
Accept-Encoding: gzip, deflate
Host: 192.168.8.103:8080
Content-Length: 3940
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=B497923BC243F5507DEF27912E36A5FF

-----------------------------7dd2ce2338056a
Content-Disposition: form-data; name="name"

123456
-----------------------------7dd2ce2338056a
Content-Disposition: form-data; name="file2"; filename="1.txt"
Content-Type: text/plain

1.内容和Content-Type之间有一个换行,分隔符“
-----------------------------7dd2ce2338056a”

所有内存传输完成之后,会加一个结束行“-----------------------------7dd2ce2338056a--”
-----------------------------7dd2ce2338056a--

  二、文件上传代码实现:

  在进行文件上传时,如果用Socket就需要将上面的HTTP请求头写出去,可以通过socket.getOutputStream()获取输出流对象,进而将数据写出去。如果采用HttpURLConnection相对Socket就简单些,可以通过类似con.setRequestProperty("Connection", "Keep-Alive")就可以讲请求头添加进去。下面采用两种方法实现

文件上传,Socket和HttpURLConnection.

package com.example.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class SocketHttpRequester {
    /**
     * 直接通过HTTP协议提交数据到服务器,实现如下面表单提交功能: <FORM METHOD=POST
     * ACTION="http://192.168.0.200:8080/ssi/fileload/test.do"
     * enctype="multipart/form-data"> <INPUT TYPE="text" NAME="name"> <INPUT
     * TYPE="text" NAME="id"> <input type="file" name="imagefile"/> <input
     * type="file" name="zip"/> </FORM>
     * 
     * @param path
     *            上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,
     *            因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080
     *            这样的路径测试)
     * @param params
     *            请求参数 key为参数名,value为参数值
     * @param file
     *            上传文件
     */
    public static boolean post(String path, Map<String, String> params,
            FormFile[] files) throws Exception {
        final String BOUNDARY = "---------------------------7da2137580612"; // 数据分隔线
        final String endline = "--" + BOUNDARY + "--\r\n";// 数据结束标志//注意回车换行部分

        int fileDataLength = 0;
        for (FormFile uploadFile : files) {// 得到文件类型数据的总长度
            StringBuilder fileExplain = new StringBuilder();
            fileExplain.append("--");
            fileExplain.append(BOUNDARY);
            fileExplain.append("\r\n");
            // 以上部分用来完成分割线: -----------------------------7dd33a32e07fc
            // 注意这里的回车换行
            fileExplain.append("Content-Disposition: form-data;name=\""
                    + uploadFile.getParameterName() + "\";filename=\""
                    + uploadFile.getFilename() + "\"\r\n");
            fileExplain.append("Content-Type: " + uploadFile.getContentType()
                    + "\r\n\r\n");
            // 上面两行用来
            /*
             * Content-Disposition: form-data; name="videofile";
             * filename="2.png" Content-Type: image/jpeg
             */
            fileDataLength += fileExplain.length();
            /*
             * //通过上面就可以计算下面这个部分的总长度了 -----------------------------7dd33a32e07fc
             * Content-Disposition: form-data; name="videofile";
             * filename="2.png" Content-Type: image/jpeg
             */
            if (uploadFile.getInStream() != null) {
                fileDataLength += uploadFile.getFile().length();
            } else {
                fileDataLength += uploadFile.getData().length;
            }
            fileDataLength += "\r\n".length();
        }
        StringBuilder textEntity = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {// 构造文本类型参数的实体数据
            textEntity.append("--");
            textEntity.append(BOUNDARY);
            textEntity.append("\r\n");
            textEntity.append("Content-Disposition: form-data; name=\""
                    + entry.getKey() + "\"\r\n\r\n");
            textEntity.append(entry.getValue());
            textEntity.append("\r\n");
        }
        // 计算传输给服务器的实体数据总长度
        int dataLength = textEntity.toString().getBytes().length
                + fileDataLength + endline.getBytes().length;
        URL url = new URL(path);
        int port = url.getPort() == -1 ? 80 : url.getPort();
        // 这里没有用HttpUrlConnection来做,而是直接用socket来做
        // 因为HttpUrlConnection使用了缓冲技术,通过这个类给web,应用发送文件的时候
        // 这个时候文件首先写入到缓冲里面,很可能导致内存溢出的情况.
        // Socket比HttpUrlConnection底层,没有缓冲区限制
        Socket socket = new Socket(InetAddress.getByName(url.getHost()), port);
        OutputStream outStream = socket.getOutputStream();
        // 下面完成HTTP请求头的发送
        String requestmethod = "POST " + url.getPath() + " HTTP/1.1\r\n";
        outStream.write(requestmethod.getBytes());
        String accept = "Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";
        outStream.write(accept.getBytes());
        String language = "Accept-Language: zh-CN\r\n";
        outStream.write(language.getBytes());
        String contenttype = "Content-Type: multipart/form-data; boundary="
                + BOUNDARY + "\r\n";
        outStream.write(contenttype.getBytes());
        String contentlength = "Content-Length: " + dataLength + "\r\n";
        outStream.write(contentlength.getBytes());
        String alive = "Connection: Keep-Alive\r\n";
        outStream.write(alive.getBytes());
        String host = "Host: " + url.getHost() + ":" + port + "\r\n";
        outStream.write(host.getBytes());
        // 写完HTTP请求头后根据HTTP协议再写一个回车换行
        outStream.write("\r\n".getBytes());
        // 把所有文本类型的实体数据发送出来
        outStream.write(textEntity.toString().getBytes());
        // 把所有文件类型的实体数据发送出来
        for (FormFile uploadFile : files) {
            StringBuilder fileEntity = new StringBuilder();
            fileEntity.append("--");
            fileEntity.append(BOUNDARY);
            fileEntity.append("\r\n");
            fileEntity.append("Content-Disposition: form-data;name=\""
                    + uploadFile.getParameterName() + "\";filename=\""
                    + uploadFile.getFilename() + "\"\r\n");
            fileEntity.append("Content-Type: " + uploadFile.getContentType()
                    + "\r\n\r\n");
            outStream.write(fileEntity.toString().getBytes());
            // 大文件
            if (uploadFile.getInStream() != null) {
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = uploadFile.getInStream().read(buffer, 0, 1024)) != -1) {
                    outStream.write(buffer, 0, len);
                }
                uploadFile.getInStream().close();
            } else {// 小文件
                outStream.write(uploadFile.getData(), 0,
                        uploadFile.getData().length);
            }
            outStream.write("\r\n".getBytes());
        }
        // 下面发送数据结束标志,表示数据已经结束
        outStream.write(endline.getBytes());
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
        if (reader.readLine().indexOf("200") == -1) {// 读取web服务器返回的数据,判断请求码是否为200,如果不是200,代表请求失败
            return false;
        }
        outStream.flush();
        outStream.close();
        reader.close();
        socket.close();
        return true;
    }
    
    //单个文件
    public static boolean post(String path, Map<String, String> params,
            FormFile file) throws Exception {
        return post(path, params, new FormFile[] { file });
    }
    
    //单个文件
    public static boolean uploadFile(String path,Map<String, String> params,File file,String encode) {
        return uploadFile(path, params, new File[]{file}, encode);
    }
    /**
     * 
     * @param path 访问路径
     * @param params 简单参数
     * @param files 上传的文件
     * @param encode 编码
     * @return
     */
    public static boolean uploadFile(String path,Map<String, String> params,File[] files,String encode) {
        BufferedInputStream inputStream = null;
        HttpURLConnection con =null;
        DataOutputStream ds = null;
        try {
            String end = "\r\n";//换行
            String twoHyphens = "--";//内容分割线多了两个“--”
            String boundary = "---------------------------7dd2ce2338056a";// 分割线
            URL url = new URL(path);
            con = (HttpURLConnection) url.openConnection();
            /* 允许Input、Output,不使用Cache */
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            /* 设置传送的method=POST */
            con.setRequestMethod("POST");
            /* setRequestProperty */
            con.setRequestProperty("Connection", "Keep-Alive");
            con.setRequestProperty("Charset", encode);
            con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);
            /* 设置DataOutputStream */
            ds = new DataOutputStream(con.getOutputStream());
            //简单请求参数
            if(params!=null){
                for (String paramName : params.keySet()) {
                    ds.writeBytes(twoHyphens + boundary + end);
                    ds.writeBytes("Content-Disposition: form-data; name=\""+paramName+"\""+end);
                    ds.writeBytes(end);
                    ds.writeBytes(params.get(paramName));
                    ds.writeBytes(end);
                }
            }
            //上传文件
            int count=1;
            if(files!=null && files.length>0){
                for (File file : files) {
                    ds.writeBytes(twoHyphens + boundary + end);
                    ds.writeBytes("Content-Disposition: form-data; "
                            + "name=\"file"+count+"\";filename=\"" + file.getName() + "\"" + end);
                    ds.writeBytes(end);
                    /* 取得文件的FileInputStream */
                    inputStream = new BufferedInputStream(new FileInputStream(file));
                    /* 设置每次写入1024bytes */
                    byte[] buffer = new byte[1024];
                    int length = -1;
                    /* 从文件读取数据至缓冲区 */
                    while ((length = inputStream.read(buffer)) != -1) {
                        ds.write(buffer, 0, length);
                    }
                    ds.writeBytes(end);
                }
                count++;
            }
            ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
            if (con.getResponseCode() == 200) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    inputStream=null;
                }
            }
            if(ds!=null){
                try {
                    ds.close();
                } catch (IOException e) {
                    ds=null;
                }
            }
            if(con!=null){
                con.disconnect();    
            }
        }
        return false;
    }

    /**
     * HttpClient提交数据到服务器
     * 
     * @param path
     *            http://192.168.8.101:8080/Simple/FileUpload
     * @param params
     *            请求参数 key为参数名,value为参数值
     * @param encode
     *            编码
     */
    public static byte[] postFromHttpClient(String path,
            Map<String, String> params, String encode) throws Exception {
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();// 用于存放请求参数
        for (Map.Entry<String, String> entry : params.entrySet()) {
            formparams.add(new BasicNameValuePair(entry.getKey(), entry
                    .getValue()));
        }
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
                encode);
        HttpPost httppost = new HttpPost(path);
        httppost.setEntity(entity);
        HttpClient httpclient = new DefaultHttpClient();// 看作是浏览器
        HttpResponse response = httpclient.execute(httppost);// 发送post请求
        return readStream(response.getEntity().getContent());
    }

    /**
     * HttpURLConnection发送请求,服务器返回流对象,可以用于下载服务器的资源
     * 
     * @param path
     *            请求路径
     * @param params
     *            请求参数 key为参数名称 value为参数值
     * @param encode
     *            请求参数的编码
     */
    public static byte[] post(String path, Map<String, String> params,
            String encode) throws Exception {
        // String params = "method=save&name="+ URLEncoder.encode("老毕",
        // "UTF-8")+ "&age=28&";//需要发送的参数
        StringBuilder parambuilder = new StringBuilder("");
        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                parambuilder.append(entry.getKey()).append("=")
                        .append(URLEncoder.encode(entry.getValue(), encode))
                        .append("&");
            }
            parambuilder.deleteCharAt(parambuilder.length() - 1);
        }
        byte[] data = parambuilder.toString().getBytes();
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);// 允许对外发送请求参数
        conn.setUseCaches(false);// 不进行缓存
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("POST");
        // 下面设置http请求头
        conn.setRequestProperty(
                "Accept",
                "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
        conn.setRequestProperty("Accept-Language", "zh-CN");
        conn.setRequestProperty(
                "User-Agent",
                "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(data.length));
        conn.setRequestProperty("Connection", "Keep-Alive");
        // 发送参数
        DataOutputStream outStream = new DataOutputStream(
                conn.getOutputStream());
        outStream.write(data);// 把参数发送出去
        outStream.flush();
        outStream.close();
        if (conn.getResponseCode() == 200) {
            return readStream(conn.getInputStream());
        }
        return null;
    }

    /**
     * 读取流
     * 
     * @param inStream
     * @return 字节数组
     * @throws Exception
     */
    public static byte[] readStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        return outSteam.toByteArray();
    }
}
View Code

  FormFile类

package com.example.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class FormFile {
    //上传文件的数据
    private byte[] data;
    
    private InputStream inStream;
    
    private  File file;
    //文件名称
    private String filename;
    //请求参数名称
    private String parameterName;
    //内容类型
    private String contentType="application/octet-stream";
    /**
     * 针对上传文件比较小
     * @param data 文件内容
     * @param filename 文件名称
     * @param parameterName 请求参数名称
     * @param contentType 内容类型
     */
    public FormFile(byte[] data, String filename, String parameterName,
            String contentType) {
        this.data = data;
        this.filename = filename;
        this.parameterName = parameterName;
        if(contentType!=null)
            this.contentType = contentType;
    }

    /**
     * 针对比较大的文件
     * @param file 传一个文件对象
     * @param parameterName 请求参数名称
     * @param contentType 内容类型
     */
    public FormFile(File file, String parameterName, String contentType) {
        this.file = file;
        this.filename=file.getName();
        this.parameterName = parameterName;
        if(contentType!=null)
            this.contentType = contentType;
        try {
            this.inStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public InputStream getInStream() {
        return inStream;
    }

    public void setInStream(InputStream inStream) {
        this.inStream = inStream;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getParameterName() {
        return parameterName;
    }

    public void setParameterName(String parameterName) {
        this.parameterName = parameterName;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}

  三、单元测试:

     public void testPost2()throws Exception{
        String path="http://192.168.8.101:8080/Simple/FileUpload";    
        Map<String, String> map=new HashMap<String, String>();
        map.put("username", "hello web");
        FormFile[] formFiles=new FormFile[2];
        formFiles[0] = new FormFile(new File(Environment.getExternalStorageDirectory(),"5.jpg"),"userimage","image/jpeg");
        formFiles[1] = new FormFile(new File(Environment.getExternalStorageDirectory(),"5.png"),"userimage2","image/jpeg");
        SocketHttpRequester.post(path, map, formFiles);
    }
    public void testPost3()throws Exception{
        String path="http://192.168.8.101:8080/Simple/FileUpload";    
        Map<String, String> params=new HashMap<String, String>();
        params.put("username", "hello web");
        params.put("age", "12");
        File[] files=new File[2];
        files[0]=new File(Environment.getExternalStorageDirectory(),"5.jpg");
        files[1]=new File(Environment.getExternalStorageDirectory(),"1.txt");
        SocketHttpRequester.uploadFile(path,params,files,"UTF-8");
    }

  四、通过Get或Post方式提交请求参数到Web:

  对于Get请求方式只需要将数据拼接到URL后面就可以,但是Get提交的数据长度有限,对于数据量比较大时,需要使用Post方式提交。下面是Post方式提交数据的HTTP响应头参数名称及值,对于Post请求需要将数据封装到“请求属性(RequestProperty)”上。从下面请求头信息可以看成,Post请求头和请求参数之间有换行(\r\n)。

下面是封装的Get和Post两种请求方式的封装类:

package com.example.service;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

/**
 * 向web服务发送数据
 * @author Administrator
 */
public class HTTPService {

    public static String contentType="application/x-www-form-urlencoded";

    /**
     * 发送post请求
     * @param path 路径
     * @param params 请求参数
     * @param encoding 编码方式
     * @return
     */
    public static boolean sendPost(String path, Map<String, Object> params,
            String encoding) {
        try {
            StringBuffer sb = buildParams(params, encoding,false);
            byte[] data = sb.toString().getBytes();
            HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("POST");
            // 允许对外输出数据
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type",contentType);
            conn.setRequestProperty("Content-Length",String.valueOf(data.length));
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(data);
            if (conn.getResponseCode() == 200) {
                // 可以通过 conn.getInputStream() 接收数据
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 发送get请求
     * @param path
     * @param params
     * @param encoding
     * @return
     */
    public static boolean sendGet(String path, Map<String, Object> params,
            String encoding) {
        try {
            StringBuffer sb = buildParams(params, encoding,true);
            path += sb.toString();
            HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                // 可以通过 conn.getInputStream() 接收数据
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 组装参数
     * 
     * @param params
     * @param encoding
     * @return
     * @throws UnsupportedEncodingException
     */
    private static StringBuffer buildParams(Map<String, Object> params,
            String encoding,boolean isGet) throws UnsupportedEncodingException {
        StringBuffer sb = new StringBuffer();
        if (isGet) {
            sb.append("?");
        }
        if (params != null) {
            for (String key : params.keySet()) {
                sb.append(key).append("=");
                sb.append(URLEncoder.encode(String.valueOf(params.get(key)),
                        encoding));
                sb.append("&");
            }
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb;
    }
}

测试:

public class HttpGetPostTest extends AndroidTestCase {
    
    public void testGet()throws Exception{
        String path="http://192.168.8.103:8080/Simple/PersonList";    
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("name", "android get test...");
        HTTPService.sendGet(path, params , "utf-8");
    }
    public void testPost()throws Exception{
        String path="http://192.168.8.103:8080/Simple/PersonList";    
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("name", "android post test...");
        HTTPService.sendPost(path, params , "utf-8");
    }
    
}
posted @ 2013-11-30 21:25  若 ♂ 只如初见  阅读(843)  评论(0编辑  收藏  举报