Android编程使用HTTP协议与TCP协议实现上传文件的方法

这篇文章主要介绍了Android编程使用HTTP协议与TCP协议实现上传文件的方法,结合实例形式较为详细的分析了Android使用HTTP协议与TCP协议的具体步骤与实现文件传输的相关技巧,需要的朋友可以参考下


http://www.jb51.net/article/77410.htm


本文实例讲述了Android编程使用HTTP协议与TCP协议实现上传文件的方法。分享给大家供大家参考,具体如下:

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

HTTP协议HttpURLConnection

1. 通过URL封装路径打开一个HttpURLConnection
2.设置请求方式以及头字段:Content-Type、Content-Length、Host
3.拼接数据发送

示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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" );
   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请求,将上传数据分段发送

示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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" );
   // 由于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 ;
}

搭建服务器,完成上传功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package cn.test.web.servlet;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class LoginServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
   @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doPost(request, response);
   }
   @Override
   public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     boolean isMultipart = ServletFileUpload.isMultipartContent(request);
     if (isMultipart)
       try {
         FileItemFactory factory = new DiskFileItemFactory();
         ServletFileUpload upload = new ServletFileUpload(factory);
         List<FileItem> items = upload.parseRequest(request);
         File dir = new File(request.getSession().getServletContext().getRealPath( "/WEB-INF/upload" ));
         //创建目录
         dir.mkdir();
         for (FileItem item : items)
           if (item.isFormField())
             System.out.println(item.getFieldName() + ": " + item.getString());
           else {
             item.write( new File(dir,item.getName().substring(item.getName().lastIndexOf( "\\" )+ 1 )));
           }
       } catch (Exception e) {
         e.printStackTrace();
       }
     else {
       System.out.println(request.getMethod());
       System.out.println(request.getParameter( "username" ));
       System.out.println(request.getParameter( "password" ));
     }
   }
}

希望本文所述对大家Android程序设计有所帮助。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值