Home > Mobile >  It saves empty when saving apk file to device
It saves empty when saving apk file to device

Time:12-08

I set up HttpServer in Java. When the apk file is sent by the client, I want to save it to the device. However, when using the code below, it saves to the device in a empty way. (0 bytes when I say empty) Where am I doing wrong?

Note: In the log in the code, I tested that the apk file was sent properly by the client. Values ​​such as size, byte, apk name are coming in properly.

class postApkFile implements HttpHandler {

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override

    public void handle(HttpExchange he) throws IOException {

        he.getResponseHeaders().add("Access-Control-Allow-Origin", "*");

        if (he.getRequestMethod().equalsIgnoreCase("OPTIONS")) {
            he.getResponseHeaders().add("Access-Control-Allow-Methods", "GET, OPTIONS");
            he.getResponseHeaders().add("Access-Control-Allow-Headers", "Content-Type,Authorization");
            he.sendResponseHeaders(204, -1);
            return;
        }

        DiskFileItemFactory d = new DiskFileItemFactory();

        try {
            ServletFileUpload up = new ServletFileUpload(d);
            List<FileItem> result = up.parseRequest(new RequestContext() {

                @Override
                public String getCharacterEncoding() {
                    return "UTF-8";
                }

                @Override
                public int getContentLength() {
                    return 0; //tested to work with 0 as return
                }

                @Override
                public String getContentType() {
                    return he.getRequestHeaders().getFirst("Content-type");
                }

                @Override
                public InputStream getInputStream() throws IOException {
                    return he.getRequestBody();
                }

            });

            InputStream inputStream = he.getRequestBody();


     
            String configXmlFilePath = Environment.getExternalStorageDirectory()   "test/configXml/test.apk";
            File apkFile = new File(configXmlFilePath);

            FileOutputStream outputStream = new FileOutputStream(apkFile);

            he.getResponseHeaders().add("Content-type", "text/plain");
            he.sendResponseHeaders(200, 0);
            OutputStream os = he.getResponseBody();

            byte[] buffer = new byte[1024];
            Log.i(HttpServerManager.getHttpServerManager().TAG, "buffer1");
            int bytesRead;
            Log.i(HttpServerManager.getHttpServerManager().TAG, "bufferread");
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            inputStream.close();
            outputStream.close();
            for (FileItem fi : result) {
                os.write(fi.getName().getBytes());
                os.write("\r\n".getBytes());

                Log.i(HttpServerManager.getHttpServerManager().TAG, "File-Item: "   fi.getFieldName()   " = "   fi.getName()   " get size "   fi.getSize() );

            }
            os.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

CodePudding user response:

Writing the while block after the for loop solved my problem.

class postApkFile implements HttpHandler {

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override

    public void handle(HttpExchange he) throws IOException {

        he.getResponseHeaders().add("Access-Control-Allow-Origin", "*");

        if (he.getRequestMethod().equalsIgnoreCase("OPTIONS")) {
            he.getResponseHeaders().add("Access-Control-Allow-Methods", "GET, OPTIONS");
            he.getResponseHeaders().add("Access-Control-Allow-Headers", "Content-Type,Authorization");
            he.sendResponseHeaders(204, -1);
            return;
        }

        DiskFileItemFactory d = new DiskFileItemFactory();

        try {
            ServletFileUpload up = new ServletFileUpload(d);
            List<FileItem> result = up.parseRequest(new RequestContext() {

                @Override
                public String getCharacterEncoding() {
                    return "UTF-8";
                }

                @Override
                public int getContentLength() {
                    return 0; //tested to work with 0 as return
                }

                @Override
                public String getContentType() {
                    return he.getRequestHeaders().getFirst("Content-type");
                }

                @Override
                public InputStream getInputStream() throws IOException {
                    return he.getRequestBody();
                }

            });



            String configXmlFilePath = Environment.getExternalStorageDirectory()   "test/configXml/test.apk";
            File apkFile = new File(configXmlFilePath);



            he.getResponseHeaders().add("Content-type", "text/plain");
            he.sendResponseHeaders(200, 0);
            OutputStream os = he.getResponseBody();
            InputStream inputStream = null;

            for (FileItem fi : result) {
                os.write(fi.getName().getBytes());
                os.write("\r\n".getBytes());
                inputStream = fi.getInputStream();

                Log.i(HttpServerManager.getHttpServerManager().TAG, "File-Item: "   fi.getFieldName()   " = "   fi.getName());

            }


            FileOutputStream outputStream = new FileOutputStream(apkFile);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            Log.i(HttpServerManager.getHttpServerManager().TAG, "os.Close ve outputStream.close");

            os.close();
            inputStream.close();
            outputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
  • Related