用户登录并上传文件;
思路:通过Watch工具,查看browser发送什么字符串,如何组织字符串;浏览器怎么做,android就怎么做;
1.如何标识数据的开始和结束?多个数据用什么分隔?
开始: ---------------------------7db1c523809b2
分隔:-----------------------------7db1c523809b2
结束:-----------------------------7db1c523809b2--
2.如何声明数据类型?是二进制文件还是字符文件?
内容部署:Content-Disposition
内容类型:Content-Type: image/bmp
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.net.URLEncoder;
import android.os.Environment;
public class LoginService {
public boolean get(String path, String username, String password) throws Exception {
URL url = new URL(path + "?username=" + URLEncoder.encode(username) + "&password=" + URLEncoder.encode(password));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setRequestMethod("GET");
return conn.getResponseCode() == 200;
}
//参数:文件路径,用户名和密码
public boolean post(String path, String username, String password) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setRequestMethod("POST");
String body = "username=" + URLEncoder.encode(username) + "&password=" + URLEncoder.encode(password);
//请求属性:内容类型和内容长度;
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // 必须有这个请求头
conn.setRequestProperty("Content-Length", body.getBytes().length + ""); // 这个也必须有
conn.setDoOutput(true);
conn.getOutputStream().write(body.getBytes());
return conn.getResponseCode() == 200;
}
//boundary 边界,用于标识
private static final String BOUNDARY = "---------------------------7db1c523809b2";
/**
* 上传文件
* @param path url地址
* @param username 用户名
* @param password 密码
* @param filename 文件名
* @return
* @throws Exception
*/
public boolean upload(String path, String username, String password, String filename) throws Exception {
File file = new File(Environment.getExternalStorageDirectory(), filename);
//
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=\"" + filename + "\"" + "\r\n");
sb.append("Content-Type: image/bmp" + "\r\n");
//sb.append("Content-Type: "+typeMap.get(fileName.substring(fileName.lastIndexOf(".")+1))+ "\r\n");
//String fileName = "aj.png";
//String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
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(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
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.254: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();
//请求响应码,并返回与200对比的结果;
return conn.getResponseCode() == 200;
}
}