基于http或 TCP实现文件上传-科卓培训

来源:互联网 发布:win7 不让安装软件 编辑:程序博客网 时间:2024/05/17 07:32
 

Android上传文件有两种方式,第一种是基于Http协议的HttpURLConnection,第二种是基于TCP协议的Socket。 这两种方式的区别是使用HttpURLConnection上传时内部有缓存机制,如果上传较大文件会导致内存溢出。如果用TCP协议Socket方式上传就会解决这种弊端。

HTTP协议HttpURLConnection

1. 通过URL封装路径打开一个HttpURLConnection


2.设置请求方式以及头字段:Content-Type、Content-Length、Host

3.拼接数据发送

示例:

private static final String BOUNDARY = "---------------------------7db1c523809b2";//数据分割线  
  
public boolean uploadHttpURLConnection(String username, String password, String path) throws Exception {  
    //找到sdcard上的文件  
    File file = new File(Environment.getExternalStorageDirectory(), path);  
                 //仿Http协议发送数据方式进行拼接  
    StringBuilder sb = new StringBuilder();  
    sb.append("--" + BOUNDARY + "\r\n");  
    sb.append("Content-Disposition: form-data; name=\"username\"" + "\r\n");  
    sb.append("\r\n");  
    sb.append(username + "\r\n");  
  
    sb.append("--" + BOUNDARY + "\r\n");  
    sb.append("Content-Disposition: form-data; name=\"password\"" + "\r\n");  
    sb.append("\r\n");  
    sb.append(password + "\r\n");  
  
    sb.append("--" + BOUNDARY + "\r\n");  
    sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + path + "\"" + "\r\n");  
    sb.append("Content-Type: image/pjpeg" + "\r\n");  
    sb.append("\r\n");  
  
    byte[] before = sb.toString().getBytes("UTF-8");  
    byte[] after = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");  
  
    URL url = new URL("http://192.168.1.16:8080/14_Web/servlet/LoginServlet");  
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
    conn.setRequestMethod("POST");  
    conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);  
    conn.setRequestProperty("Content-Length", String.valueOf(before.length + file.length() + after.length));  
    conn.setRequestProperty("HOST", "192.168.1.16:8080");  
    conn.setDoOutput(true);   
  
    OutputStream out = conn.getOutputStream();  
    InputStream in = new FileInputStream(file);  
      
    out.write(before);  
  
    byte[] buf = new byte[1024];  
    int len;  
    while ((len = in.read(buf)) != -1)  
        out.write(buf, 0, len);  
  
    out.write(after);  
  
    in.close();  
    out.close();  
    return conn.getResponseCode() == 200;  
}
TCP协议Socket

1.我们可以使用Socket发送TCP请求,将上传数据分段发送

示例:

public boolean uploadBySocket(String username, String password, String path) throws Exception {  
    // 根据path找到SDCard中的文件  
    File file = new File(Environment.getExternalStorageDirectory(), path);  
    // 组装表单字段和文件之前的数据  
    StringBuilder sb = new StringBuilder();  
  
    sb.append("--" + BOUNDARY + "\r\n");  
    sb.append("Content-Disposition: form-data; name=\"username\"" + "\r\n");  
    sb.append("\r\n");  
    sb.append(username + "\r\n");  
  
    sb.append("--" + BOUNDARY + "\r\n");  
    sb.append("Content-Disposition: form-data; name=\"password\"" + "\r\n");  
    sb.append("\r\n");  
    sb.append(password + "\r\n");  
  
    sb.append("--" + BOUNDARY + "\r\n");  
    sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + path + "\"" + "\r\n");  
    sb.append("Content-Type: image/pjpeg" + "\r\n");  
    sb.append("\r\n");  
  
    // 文件之前的数据  
    byte[] before = sb.toString().getBytes("UTF-8");  
    // 文件之后的数据  
    byte[] after = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");  
  
    URL url = new URL("http://192.168.1.199:8080/14_Web/servlet/LoginServlet");  
  
    // 由于HttpURLConnection中会缓存数据, 上传较大文件时会导致内存溢出, 所以我们使用Socket传输  
    Socket socket = new Socket(url.getHost(), url.getPort());  
    OutputStream out = socket.getOutputStream();  
    PrintStream ps = new PrintStream(out, true, "UTF-8");  
  
    // 写出请求头  
    ps.println("POST /14_Web/servlet/LoginServlet HTTP/1.1");  
    ps.println("Content-Type: multipart/form-data; boundary=" + BOUNDARY);  
    ps.println("Content-Length: " + String.valueOf(before.length + file.length() + after.length));  
    ps.println("Host: 192.168.1.199:8080");  
      
    InputStream in = new FileInputStream(file);  
  
    // 写出数据  
    out.write(before);  
  
    byte[] buf = new byte[1024];  
    int len;  
    while ((len = in.read(buf)) != -1)  
        out.write(buf, 0, len);  
  
    out.write(after);  
  
    in.close();  
    out.close();  
  
    return true;  
}

原创粉丝点击