From 99e09d8c6992893c22230d4ac78148637c2ecac9 Mon Sep 17 00:00:00 2001 From: BlueMatthew Date: Sun, 24 Dec 2023 00:01:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=90=E7=BB=B4APP=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xypower/common/FileUploader.java | 142 ++++++++++++++++++ .../com/xypower/common/MicroPhotoContext.java | 5 +- .../java/com/xypower/common/ZipUtils.java | 58 +++++++ .../java/com/xypower/mpmaster/AppMaster.java | 85 +++++------ .../com/xypower/mpmaster/MainActivity.java | 6 +- 5 files changed, 244 insertions(+), 52 deletions(-) create mode 100644 common/src/main/java/com/xypower/common/FileUploader.java create mode 100644 common/src/main/java/com/xypower/common/ZipUtils.java diff --git a/common/src/main/java/com/xypower/common/FileUploader.java b/common/src/main/java/com/xypower/common/FileUploader.java new file mode 100644 index 00000000..1e5343ae --- /dev/null +++ b/common/src/main/java/com/xypower/common/FileUploader.java @@ -0,0 +1,142 @@ +package com.xypower.common; + +import android.text.TextUtils; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.file.Files; +import java.util.UUID; + +public class FileUploader { + + private HttpURLConnection httpConn; + private DataOutputStream request; + private final static String BOUNDARY = UUID.randomUUID().toString();; + private final static String CRLF = "\r\n"; + private final static String twoHyphens = "--"; + + /** + * This constructor initializes a new HTTP POST request with content type + * is set to multipart/form-data + * + * @param requestURL + * @throws IOException + */ + public FileUploader(String requestURL) + throws IOException { + + // creates a unique boundary based on time stamp + URL url = new URL(requestURL); + httpConn = (HttpURLConnection) url.openConnection(); + httpConn.setUseCaches(false); + httpConn.setDoOutput(true); // indicates POST method + httpConn.setDoInput(true); + + httpConn.setRequestMethod("POST"); + httpConn.setRequestProperty("Connection", "Keep-Alive"); + httpConn.setRequestProperty("Cache-Control", "no-cache"); + httpConn.setRequestProperty( + "Content-Type", "multipart/form-data;boundary=" + this.BOUNDARY); + + request = new DataOutputStream(httpConn.getOutputStream()); + } + + @Override + protected void finalize() { + if (httpConn != null) { + try { + httpConn.disconnect(); + } catch (Exception ex) { + + } + } + } + + /** + * Adds a form field to the request + * + * @param name field name + * @param value field value + */ + public void addFormField(String name, String value) throws IOException { + request.writeBytes(this.twoHyphens + this.BOUNDARY + this.CRLF); + request.writeBytes("Content-Disposition: form-data; name=\"" + name + "\""+ this.CRLF); + request.writeBytes("Content-Type: text/plain; charset=UTF-8" + this.CRLF); + request.writeBytes(this.CRLF); + request.writeBytes(value+ this.CRLF); + request.flush(); + } + + /** + * Adds a upload file section to the request + * + * @param fieldName name attribute in + * @param uploadFile a File to be uploaded + * @throws IOException + */ + public void addFilePart(String fieldName, File uploadFile, String fileName, String mimeType) + throws IOException { + request.writeBytes(this.twoHyphens + this.BOUNDARY + this.CRLF); + request.writeBytes("Content-Disposition: form-data; name=\"" + + fieldName + "\";filename=\"" + + (TextUtils.isEmpty(fileName) ? uploadFile.getName() : fileName) + "\"" + this.CRLF); + if (!TextUtils.isEmpty(mimeType)) { + request.writeBytes("Content-Type: " + mimeType + "; charset=" + "UTF-8" + this.CRLF); + } + request.writeBytes(this.CRLF); + + byte[] bytes = Files.readAllBytes(uploadFile.toPath()); + request.write(bytes); + } + + /** + * Completes the request and receives response from the server. + * + * @return a list of Strings as response in case the server returned + * status OK, otherwise an exception is thrown. + * @throws IOException + */ + public String finish() throws IOException { + String response =""; + + request.writeBytes(this.CRLF); + request.writeBytes(this.twoHyphens + this.BOUNDARY + + this.twoHyphens + this.CRLF); + + request.flush(); + request.close(); + + // checks server's status code first + int status = httpConn.getResponseCode(); + if (status == HttpURLConnection.HTTP_OK) { + InputStream responseStream = new + BufferedInputStream(httpConn.getInputStream()); + + BufferedReader responseStreamReader = + new BufferedReader(new InputStreamReader(responseStream)); + + String line = ""; + StringBuilder stringBuilder = new StringBuilder(); + + while ((line = responseStreamReader.readLine()) != null) { + stringBuilder.append(line).append("\n"); + } + responseStreamReader.close(); + + response = stringBuilder.toString(); + httpConn.disconnect(); + httpConn = null; + } else { + throw new IOException("Server returned non-OK status: " + status); + } + + return response; + } +} diff --git a/common/src/main/java/com/xypower/common/MicroPhotoContext.java b/common/src/main/java/com/xypower/common/MicroPhotoContext.java index ecd7aefb..77c3905f 100644 --- a/common/src/main/java/com/xypower/common/MicroPhotoContext.java +++ b/common/src/main/java/com/xypower/common/MicroPhotoContext.java @@ -21,7 +21,8 @@ public class MicroPhotoContext { public static final String PACKAGE_NAME_MPAPP = "com.xypower.mpapp"; public static final String PACKAGE_NAME_MPMASTER = "com.xypower.mpmaster"; - public final static String DEFAULT_MASTER_URL = "http://180.166.218.222:40101/?cmdid="; + public final static String DEFAULT_MASTER_URL = "http://180.166.218.222:40101/"; + public final static String MASTER_URL_CMDID = "cmdid"; public final static int DEFAULT_PROTOCOL = 0xFF00; @@ -47,7 +48,7 @@ public class MicroPhotoContext { path += File.separator; } - path += contxt.getPackageName() + File.separator; + path += PACKAGE_NAME_MPAPP + File.separator; File pathFile = new File(path); if (!pathFile.exists() && !pathFile.mkdirs()) { return null; diff --git a/common/src/main/java/com/xypower/common/ZipUtils.java b/common/src/main/java/com/xypower/common/ZipUtils.java new file mode 100644 index 00000000..6f1df193 --- /dev/null +++ b/common/src/main/java/com/xypower/common/ZipUtils.java @@ -0,0 +1,58 @@ +package com.xypower.common; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class ZipUtils { + public static void ZipFolder(File srcFile, File zipFile) { + try { + ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile)); + ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip); + outZip.finish(); + outZip.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static void ZipFiles(String srcFileParentName, String srcFileName, ZipOutputStream zipOutputSteam) throws Exception { + if (zipOutputSteam == null) + return; + File file = new File(srcFileParentName + srcFileName); + + if (file.isFile()) { + ZipEntry zipEntry = new ZipEntry(srcFileName); + FileInputStream inputStream = new FileInputStream(file); + zipOutputSteam.putNextEntry(zipEntry); + int len; + byte[] buffer = new byte[1024 * 256]; + while ((len = inputStream.read(buffer)) != -1) { + zipOutputSteam.write(buffer, 0, len); + } + zipOutputSteam.closeEntry(); + } else { + // folder + String fileList[] = file.list(); + // NO sub file + if (fileList.length <= 0) { + ZipEntry zipEntry = new ZipEntry(srcFileName + File.separator); + zipOutputSteam.putNextEntry(zipEntry); + zipOutputSteam.closeEntry(); + } + // + for (int i = 0; i < fileList.length; i++) { + ZipFiles(srcFileParentName + srcFileName + "/", fileList[i], zipOutputSteam); + } + } + } + +} diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index 53d5a095..33e4cbe4 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -2,15 +2,18 @@ package com.xypower.mpmaster; import android.content.Context; import android.os.Environment; +import android.os.Handler; import android.os.PowerManager; import android.text.TextUtils; import android.util.Pair; import com.dev.devapi.api.SysApi; import com.xypower.common.FileDownloader; +import com.xypower.common.FileUploader; import com.xypower.common.InetAddressUtils; import com.xypower.common.JSONUtils; import com.xypower.common.MicroPhotoContext; +import com.xypower.common.ZipUtils; import org.json.JSONObject; @@ -28,7 +31,9 @@ import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -84,12 +89,11 @@ public class AppMaster { path += "data/Master.json"; JSONObject jsonObject = JSONUtils.loadJson(path); - - if (jsonObject.has("url")) { + if (jsonObject != null && jsonObject.has("url")) { masterUrl = jsonObject.optString("url"); } - MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getAppConfig(mService.getApplicationContext()); + MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(mService.getApplicationContext()); mCmdid = appConfig.cmdid; if (TextUtils.isEmpty(masterUrl)) { @@ -101,9 +105,9 @@ public class AppMaster { try { if (masterUrl.indexOf('?') != -1) { - masterUrl += "&cmdid=" + URLEncoder.encode(mCmdid, "UTF-8"); + masterUrl += "&" + MicroPhotoContext.MASTER_URL_CMDID + "=" + URLEncoder.encode(mCmdid, "UTF-8"); } else { - masterUrl += "?cmdid=" + URLEncoder.encode(mCmdid, "UTF-8"); + masterUrl += "?" + MicroPhotoContext.MASTER_URL_CMDID + "=" + URLEncoder.encode(mCmdid, "UTF-8"); } URL url = new URL(masterUrl); httpURLConnection = (HttpURLConnection) url.openConnection(); @@ -278,57 +282,48 @@ public class AppMaster { } private void uploadLogs(String url) { + String appDir = mService.buildAppDir(); + try { - } + long ts = System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis(); + SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); + Date dt = new Date(ts); - public static void ZipFolder(File srcFile, File zipFile) { - try { - ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile)); - ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip); - outZip.finish(); - outZip.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - } + final String fileName = mCmdid + "_" + format.format(dt) + ".zip"; + final File file = File.createTempFile(fileName, null, new File(appDir)); + if (file == null) { + return; + } - private static void ZipFiles(String srcFileParentName, String srcFileName, ZipOutputStream zipOutputSteam) throws Exception { - if (zipOutputSteam == null) - return; - File file = new File(srcFileParentName + srcFileName); - - if (file.isFile()) { - ZipEntry zipEntry = new ZipEntry(srcFileName); - FileInputStream inputStream = new FileInputStream(file); - zipOutputSteam.putNextEntry(zipEntry); - int len; - byte[] buffer = new byte[1024 * 256]; - while ((len = inputStream.read(buffer)) != -1) { - zipOutputSteam.write(buffer, 0, len); + final String mpAppDir = MicroPhotoContext.buildMpAppDir(mService.getApplicationContext()); + final File logDir = new File(mpAppDir + "logs" + File.separator); + if (!logDir.exists()) { + return; } - zipOutputSteam.closeEntry(); - } else { - // folder - String fileList[] = file.list(); - // NO sub file - if (fileList.length <= 0) { - ZipEntry zipEntry = new ZipEntry(srcFileName + File.separator); - zipOutputSteam.putNextEntry(zipEntry); - zipOutputSteam.closeEntry(); + + ZipUtils.ZipFolder(logDir, file); + + if (!file.exists()) { + return; } - // - for (int i = 0; i < fileList.length; i++) { - ZipFiles(srcFileParentName + srcFileName + "/", fileList[i], zipOutputSteam); + + FileUploader fileUploader = new FileUploader(url); + fileUploader.addFilePart("file", file, fileName, "application/x-zip-compressed"); + + String response = fileUploader.finish(); + if (response != null) { + } + + } catch (Exception ex) { + } + } + private void buildParams(OutputStream output) { BufferedWriter bufferedWriter = null; try { diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java b/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java index a7a980d3..7ca82386 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java @@ -38,11 +38,7 @@ public class MainActivity extends AppCompatActivity { public static void startMicroPhotoService(Context context) { - MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getAppConfig(context.getApplicationContext()); - - if (TextUtils.isEmpty(appConfig.cmdid) || TextUtils.isEmpty(appConfig.server) || appConfig.port == 0) { - return; - } + MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(context); Intent intent = new Intent(context, MpMasterService.class); intent.setAction(MpMasterService.ACTION_START);