增加装置端下载文件的功能

hdrplus
Matthew 1 year ago
parent 5107e26edf
commit 59022ea296

@ -5,6 +5,7 @@ import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Environment;
import android.os.PowerManager;
import android.os.SystemClock;
@ -39,6 +40,10 @@ import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@ -64,6 +69,7 @@ public class AppMaster {
public static final String CMD_UPDATE_CONFIG = "upd_cfg";
public static final String CMD_PULL_FILE = "pull_files";
public static final String CMD_PUSH_FILE = "push_file";
public static final String CMD_DOWNLOAD_FILE = "dl_file";
public static final String CMD_DELETE_FILE = "del_file";
public static final String CMD_HOT_SPOT = "yw_cmd_hot_spot";
public static final String CMD_ENABLE_GPS = "yw_cmd_enable_gps";
@ -185,13 +191,14 @@ public class AppMaster {
}).start();
}
private void runImpl() {
private boolean runImpl() {
String masterUrl = mMasterUrl;
if (TextUtils.isEmpty(masterUrl)) {
return;
return false;
}
boolean res = false;
HttpURLConnection httpURLConnection = null;
InputStream inputStream = null;
Date now = new Date();
@ -209,15 +216,6 @@ public class AppMaster {
masterUrl += "?" + MicroPhotoContext.MASTER_URL_CMDID + "=" + URLEncoder.encode(mCmdid, "UTF-8");
}
URL url = new URL(masterUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setReadTimeout(15000);
httpURLConnection.setRequestMethod("POST");
// httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Accept-Cmds", "Multiple");
List<Pair<String, String>> postParams = new ArrayList<>();
postParams.add(new Pair<String, String>("id", mCmdid));
@ -268,22 +266,56 @@ public class AppMaster {
ex.printStackTrace();
}
// postParams(httpURLConnection.getOutputStream(), postParams);
buildParams(httpURLConnection.getOutputStream(), postParams);
httpURLConnection.connect();
inputStream = httpURLConnection.getInputStream();
for (int idx = 0; idx < 3; idx++) {
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setReadTimeout(15000);
try {
httpURLConnection.setRequestMethod("POST");
} catch (Exception ex) {
ex.printStackTrace();
}
// httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Accept-Cmds", "Multiple");
int responseCode = httpURLConnection.getResponseCode();
// postParams(httpURLConnection.getOutputStream(), postParams);
buildParams(httpURLConnection.getOutputStream(), postParams);
httpURLConnection.connect();
inputStream = httpURLConnection.getInputStream();
if (responseCode == HttpURLConnection.HTTP_OK) {
//在子线程中不能操作UI线程通过handler在UI线程中进行操作
// handler.sendEmptyMessage(0x00);
String response = convertStreamToString(inputStream);
process(response);
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//在子线程中不能操作UI线程通过handler在UI线程中进行操作
// handler.sendEmptyMessage(0x00);
String response = convertStreamToString(inputStream);
process(response);
}
res = true;
} catch (Exception ex) {
// ex.printStackTrace();
mService.logger.warning(ex.getMessage());
}
if (res) {
break;
}
try {
Thread.sleep(3000);
} catch (Exception ex) {
}
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
private void buildStats(long startTime, List<Pair<String, String>> stats) {
@ -525,6 +557,44 @@ public class AppMaster {
}
}
}
} else if (TextUtils.equals(cmd, CMD_DOWNLOAD_FILE)) {
String path = jsonObject.optString("path", null);
String url = jsonObject.optString("url", null);
mService.logger.warning("Recv Download File: " + url + " => " + path);
File file = new File(path);
File parentPath = file.getParentFile();
if (!parentPath.exists()) {
parentPath.mkdirs();
}
FileDownloader dl = new FileDownloader();
File tmpPath = new File(MicroPhotoContext.buildAppDir(mService.getApplicationContext()), "tmp");
if (!tmpPath.exists()) {
tmpPath.mkdirs();
}
File tmpFile = new File(tmpPath, file.getName());
if (tmpFile.exists()) {
tmpFile.delete();
}
String apkPath = file.getAbsolutePath();
if (dl.download(url, tmpFile.getAbsolutePath())) {
sendResult(cid, 1, cmd, cmd + ":" + mCmdid);
Context context = mService.getApplicationContext();
mService.logger.info("Downloaded File: " + url);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Files.move(tmpFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
} catch (Exception ex) {
}
} else {
mService.logger.warning("Failed to Download:" + url);
}
} else if (TextUtils.equals(cmd, CMD_HOT_SPOT)) {
int enable = jsonObject.optInt("enable", 1);
String name = jsonObject.optString("name", "XYMP");

Loading…
Cancel
Save