爲了方便客戶抓取Log,現通過TCP協議連接指定服務器,傳輸指定內容,定義指定目錄,IP,PORT字段接收參數。直接上代碼
public static void uploadLog(final Context context, final String dirPath, final String IP, final int port) {
final JSONArray allFiles = new JSONArray();
getAllFiles(allFiles, dirPath);
if (allFiles == null || allFiles.length() == 0) return;
new Thread() {
@Override
public void run() {
super.run();
try {
Socket socket = new Socket(IP, port);
if (!socket.isConnected()) return; //判斷是否建立連接
OutputStream os = socket.getOutputStream();
int index = dirPath.lastIndexOf("/") + 1;
os.write(dirPath.substring(index).getBytes());//TAG
os.write("\r\n".getBytes());
for (int i = 0; i < allFiles.length(); i++) {
try {
JSONObject o = (JSONObject) allFiles.get(i);
String path = o.getString("path");
String name = o.getString("name");
os.write(name.getBytes());
os.write("\r\n".getBytes());//向服務器端發送文件
if(path.equals("TAG")){
continue;
}
FileInputStream fis = new FileInputStream(path);
if (fis != null) {
InputStreamReader inputreader = new InputStreamReader(fis);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
while ((line = buffreader.readLine()) != null) { //按行讀取文件內容
os.write(line.getBytes());
os.write("\r\n".getBytes());//向服務器端發送文件
// fileOutputStream.write(line.getBytes());
// fileOutputStream.write("\r\n".getBytes());//向服務器端發送文件
}
buffreader.close();
inputreader.close();
}
fis.close();
} catch (JSONException e) {
e.printStackTrace();
}
}
//關閉客戶端輸出流,中斷上傳
socket.shutdownOutput();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
/**
* 獲取指定目錄內所有文件路徑
*
* @param dirPath 需要查詢的文件目錄
*/
public static JSONArray getAllFiles(JSONArray fileList, String dirPath) {
File f = new File(dirPath);
if (!f.exists()) {//判斷路徑是否存在
return null;
}
if (!f.isDirectory()) {
String filePath = f.getAbsolutePath();//獲取文件路徑
int end = f.getName().lastIndexOf('.');
String fileName = f.getName().substring(0, end);//獲取文件名
try {
JSONObject _fInfo = new JSONObject();
_fInfo.put("name", fileName);
_fInfo.put("path", filePath);
fileList.put(_fInfo);
} catch (Exception e) {
}
return fileList;
}
File[] files = f.listFiles();
if (files == null) {//判斷權限
return null;
}
for (File _file : files) {//遍歷目錄
if (_file.isFile()) {
String filePath = _file.getAbsolutePath();//獲取文件路徑
int index = filePath.lastIndexOf("/") + 1;
String fileName = filePath.substring(index);//獲取文件名
// Log.d("LOGCAT","fileName:"+fileName);
// Log.d("LOGCAT","filePath:"+filePath);
try {
JSONObject _fInfo = new JSONObject();
_fInfo.put("name", fileName);
_fInfo.put("path", filePath);
fileList.put(_fInfo);
} catch (Exception e) {
}
} else if (_file.isDirectory()) {//查詢子目錄
try {
String filePath = _file.getAbsolutePath();//獲取文件路徑
int index = filePath.lastIndexOf("/") + 1;
String fileName = filePath.substring(index);//獲取文件名
try {
JSONObject _fInfo = new JSONObject();
_fInfo.put("name", fileName);
_fInfo.put("path", "TAG");//文件夾
fileList.put(_fInfo);
} catch (Exception e) {
}
} catch (Exception e) {
e.printStackTrace();
}
getAllFiles(fileList, _file.getAbsolutePath());
} else {
}
}
return fileList;
}
必須聲明一下權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
設計思路:
1首先通過第三方應用傳過來的Log路徑,通過遍歷該路徑,得到該目錄下的所有文件,保存到集合中,
2然後通過socker建立通信,通信建立成功後開始傳輸日誌,
3讀取指定目錄下的日誌文件,解析內容傳輸到服務端,
4日誌按行讀取,
5內容頭部增加TAG以區分不同應用的日誌
服務端是因爲有現成的軟件,所以這裏就不做解析了。
LogUtil.uploadLog("storage/emulated/0/C28Log/CarRecorderLog","10.0.16.252",8088);