a
xiuxi
2019-08-30 12ce6f9e6123f99676b5c9fd2fa22501edddb7cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.basic.security;
 
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.google.gson.Gson;
 
public class GetApk extends HttpServlet {
 
    // http://192.168.1.116:8080/getApk?apkName=Security_simplify&currentApkVersion=1
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("GetApk.doGet()");
        String apkName = request.getParameter("apkName");
        String currentApkVersionStr = request.getParameter("currentApkVersion");
        
        Gson gson = new Gson();
        Map<String, String> responseMap = new HashMap<String, String>();
        responseMap.put("hasNewVersion", "false");
        try {
            File apkFiles[] = new File(getServletContext().getRealPath("")).listFiles();
            for (File apkFile : apkFiles) {
                String newApkFileName = apkFile.getName();
                if (newApkFileName.endsWith(".apk") && newApkFileName.startsWith(newApkFileName)) {
                    try {
                        int newVersion = Integer.parseInt(newApkFileName.replace(apkName, "").replace(".apk", ""));
                        int currentApkVersion = Integer.parseInt(currentApkVersionStr);
                        if (newVersion > currentApkVersion) {
                            responseMap.put("hasNewVersion", "true");
                            responseMap.put("newApkName", newApkFileName);
                            responseMap.put("newApkSize", apkFile.length()+"");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        out.println(gson.toJson(responseMap));
        out.flush();
        out.close();
    }
 
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
 
    public GetApk() {
        super();
    }
 
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
 
    public void init() throws ServletException {
    }
 
}