毕业两年了,也工作两年了,但是由于具体的工作内容原因,真正去按照老大的要求去写一个程序,还是第一次。
这次就遇到了一个问题,从来没有接触过的Android网络编程,需求是按照指定的三种方法,以POST方法,分别用HTTP和Socket上传一个文件到服务器。
指定的三种方法为:
第一种:形如 "http://host:port/xxx/xxx/xxx/param?param1=xxx¶m2=xxx"的形式
Params:
param1
param2
Data:
file:要上传的文件
第二种:形如“http://host:port/xxx/xxx/xxx”的形式
Part1:
param1
Part2:
param2
Part3:
file:要上传的文件
第三种:形如“http://host:port/xxx/xxx/xxx”的形式
Part1:
param1
Part2:
param2
Part3:
file:要上传的文件
利用Socket方法来发送
三种方法,首先搞得我头晕脑胀,纠结了整整一天,后来终于理顺了一些,现在把这些方法陈列出来,大家来批评指正,有不对的地方及时通知我来进行修改,大家共勉。
第一种和第二种都是模拟HTTP协议来发送的,使用HttpURLConnection类来发送,第三种使用Socket方法来发送。
首先我定义了一个上传工具类:UploadUtil.java,里面只是定义了一个Static的服务器地址,INTERNAL_HOST。
代码如下
- public class UploadUtil {
-
- static String INTERNAL_HOST = "http://host:port/xxx/xxx/xxx/upload/";
-
- }
这个主机是在第一种方法的地址中,位于“param?”前面的部分。
完成第一种方法:
第一种方法的“http://host:port/xxx/xxx/xxx/param?param1=xxx¶m2=xxx”,其实已经给出了POST的参数,param1和param2,我为了弄着方便,就直接写死了参数键值。其实也可以在方法内部,通过传入的参数,进行循环拼装,最后只要能组出类似“http://host:port/xxx/xxx/xxx/param?param1=xxx¶m2=xxx”的字符串就行,最后通过URL来生成一个url对象即可。
- public static void queryParam(String fileName)
- {
- String BOUNDARY = "---------------------------7db1c523809b2";
- File file = new File(fileName);
- String host = INTERNAL_HOST + "param?param1=xxx¶m2=xxx";
-
-
- try
- {
- byte[] after = ("--" + BOUNDARY + "--\r\n").getBytes("UTF-8");
-
-
- URL url = new URL(host);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
-
-
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
- conn.setRequestProperty("Content-Length", String.valueOf(file.length()));
- conn.setRequestProperty("HOST", url.getHost());
- conn.setDoOutput(true);
-
-
- OutputStream out = conn.getOutputStream();
- InputStream in = new FileInputStream(file);
-
-
-
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) != -1)
- {
- out.write(buf, 0, len);
- }
-
-
-
-
- in.close();
- out.close();
-
- Log.d("carter", "queryParam 返回码为: " + conn.getResponseCode());
- Log.d("carter", "queryParam 返回信息为: " + conn.getResponseMessage());
-
- }
- catch (MalformedURLException e) {
-
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
大概的步骤:
1.先构造好URL服务器地址
2.初始化一个URL对象,得到里面的HttpURLConnection对象
3.设置connection对象的头部信息,包括POST方法、HOST、Content-Type、Content-Length等属性
4.写入要上传的文件内容
完成第二种方法:
第二种的方法,不想第一种方法那样简单,但基本语法结构都相似,因为都是模仿HTTP协议的形式。
第二种方法,在服务器地址上,只提供了上传的HOST和PATH,具体的参数没有提供,所以要通过在HTTP报文中添加来实现。
- public static void multiPart(String fileName)
- {
- String BOUNDARY = "---------------------------7db1c523809b2";
- File file = new File(fileName);
-
-
- StringBuilder sb = new StringBuilder();
- sb.append("--" + BOUNDARY + "\r\n");
- sb.append("Content-Disposition: form-data; name=\"param1\"" + "\r\n");
- sb.append("\r\n");
- sb.append("xxx" + "\r\n");
-
- sb.append("--" + BOUNDARY + "\r\n");
- sb.append("Content-Disposition: form-data; name=\"param2\"" + "\r\n");
- sb.append("\r\n");
- sb.append("xxx" + "\r\n");
-
-
- sb.append("--" + BOUNDARY + "\r\n");
- sb.append("Content-Disposition: form-data; name=\"data\"; filename=\"" + fileName + "\"" + "\r\n");
- sb.append("Content-Type: text/plain" + "\r\n");
- sb.append("\r\n");
-
- try
- {
- byte[] before = sb.toString().getBytes("UTF-8");
- byte[] after = ("--" + BOUNDARY + "--\r\n").getBytes("UTF-8");
- URL url = new URL(INTERNAL_HOST);
-
-
- 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", url.getHost());
- 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);
-
-
- out.flush();
-
-
- in.close();
- out.close();
-
- Log.d("carter", "multipart 返回码为: " + conn.getResponseCode());
- Log.d("carter", "multipart 返回信息为: " + conn.getResponseMessage());
- }
- catch (MalformedURLException e) {
-
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
-
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
具体步骤为:
1.分别把param1等参数通过格式组织起来
2.得到HttpURLConnection对象,设置一些基本头属性,其中的Content-Length是所有param和文件加上尾标志的总长度。
3.发送outputStream。
完成第三种方法:
第三种方法和第二种方法很相似,只是使用了Socket对象,而不是HttpURLConnection方法。
- public static void socket(String fileName)
- {
- String BOUNDARY = "---------------------------7db1c523809b2";
- File file = new File(fileName);
-
-
- StringBuilder textParam = new StringBuilder();
- textParam.append("--" + BOUNDARY + "\r\n");
- textParam.append("Content-Disposition: form-data; name=\"param1\"" + "\r\n");
- textParam.append("\r\n");
- textParam.append("xxx" + "\r\n");
-
- textParam.append("--" + BOUNDARY + "\r\n");
- textParam.append("Content-Disposition: form-data; name=\"param2\"" + "\r\n");
- textParam.append("\r\n");
- textParam.append("xxx" + "\r\n");
-
-
- int fileDataLen = 0;
- StringBuilder fileParam = new StringBuilder();
- fileParam.append("--" + BOUNDARY + "\r\n");
- fileParam.append("Content-Disposition: form-data;name=\"data\";" + "filename=\"" + fileName + "\"" + "\r\n");
- fileParam.append("Content-Type: text/plain" + "\r\n\r\n");
- fileParam.append("\r\n");
-
-
- fileDataLen += fileParam.length();
-
- fileDataLen += file.length();
-
-
- int totalDataLen = 0;
-
- try
- {
- byte[] textEntity = textParam.toString().getBytes();
- byte[] after = ("--" + BOUNDARY + "--\r\n").getBytes();
-
- totalDataLen = textEntity.length + fileDataLen + after.length;
-
-
- URL url = new URL(INTERNAL_HOST);
- Socket socket = new Socket(url.getHost(), 80);
- socket.setSoTimeout(60000);
-
-
- OutputStream out = socket.getOutputStream();
-
-
- String requestMethod = "POST " + url.getPath() + " HTTP/1.1\r\n";
- out.write(requestMethod.getBytes());
-
-
- String accept = "Accept: image/gif, image/jpeg, 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";
- out.write(accept.getBytes());
-
-
- String language = "Accept-Language: zh-CN\r\n";
- out.write(language.getBytes());
-
-
- String contentType = "Content-Type: multipart/form-data; boundary=" + BOUNDARY + "\r\n";
- out.write(contentType.getBytes());
-
-
- String contentLength = "Content-Length: " + totalDataLen + "\r\n";
- out.write(contentLength.getBytes());
-
-
- String alive = "Connection: Keep-Alive\r\n";
- out.write(alive.getBytes());
-
-
- String host = "Host: " + url.getHost() + ":80\r\n";
- out.write(host.getBytes());
-
-
- out.write("\r\n".getBytes());
-
-
-
- out.write(textEntity);
-
-
-
- StringBuilder fileEntity = new StringBuilder();
- fileEntity.append("--" + BOUNDARY + "\r\n");
- fileEntity.append("Content-Disposition: form-data;name=\"data\";" + "filename=\"" + fileName + "\"" + "\r\n");
- fileEntity.append("Content-Type: text/plain" + "\r\n\r\n");
- out.write(fileEntity.toString().getBytes());
-
-
- FileInputStream fis = new FileInputStream(file);
- byte[] buffer = new byte[1024];
- int len = 0;
- while( (len=fis.read(buffer))!=-1 )
- {
- out.write(buffer, 0, len);
- }
- fis.close();
- out.write("\r\n".getBytes());
-
-
- out.write(after);
-
-
-
- BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String line = "";
- while( (line=reader.readLine())!=null)
- {
- Log.d("carter", line);
- }
-
-
- out.flush();
-
-
- out.close();
- reader.close();
- socket.close();
- }
- catch (MalformedURLException e) {
-
- e.printStackTrace();
- }
- catch (UnsupportedEncodingException e) {
-
- e.printStackTrace();
- }
- catch (IOException e) {
-
- e.printStackTrace();
- }
- }
具体步骤:
1.构造参数信息
2.构造文件信息
3.构造尾信息
4.通过URL和端口得到一个Socket对象
5.设置Socket的属性
6.得到一个输出流,将所有信息输出
7.输出传送结果
至此,三种方法就都实现了,纠结的内容,但是需要积极的去实现。
以上方法,只要替换其中的某些参数信息,就可以完全直接复用了。