android 当保存apk文件到设备时,它保存为空

qzwqbdag  于 2022-12-09  发布在  Android
关注(0)|答案(1)|浏览(189)

我在Java中设置了HttpServer,当客户端发送apk文件时,我想将其保存到设备中,但是在使用下面的代码时,它以空的方式保存到设备中(我说空时为0字节)我哪里做错了?
注意:在代码的日志中,我测试了客户端是否正确发送了apk文件。Values 例如大小、字节、APK名称。

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();
        }
    }

}
bvjveswy

bvjveswy1#

在for循环之后编写while块解决了我的问题。

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();
        }
    }

}

相关问题