Java从JsonElement获取字节数组

时间:2018-08-05 10:37:24

标签: java arrays json gson byte

当我将文件上传到数据库时,上传的字节数组的长度为 32 。但是,在检索到它之后,我从JsonElement中提取了字节数组,现在的长度为 46

这是我返回从REST服务器收到的JsonObject的方式:

public static JsonObject getSplitFiles(String username, String fileName) {
    BufferedReader br = null;
    try {
        URL url = new URL(SERVERURL + username + "/" + fileName);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/json");

        InputStream inputStream = connection.getInputStream();
        InputStreamReader input = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        br = new BufferedReader(input); // Getting the response from the webservice

        String output = br.readLine();
        br.close();
        input.close();
        inputStream.close();
        connection.disconnect();
        if (output != null) {
            br.close();
            connection.disconnect();
            JsonElement jelement = new JsonParser().parse(output);
            return jelement.getAsJsonObject();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

我如何从JsonObject返回对象:

public static FileStorage getSplitFiles(String username, String fileName) throws UnsupportedEncodingException {
    JsonObject jsonObject = FileStorageDAO.getSplitFiles(username, fileName);
    FileStorage fs = new FileStorage();
    fs.setUsername(jsonObject.get("username").getAsString());
    fs.setFileName(jsonObject.get("fileName").getAsString());
    fs.setFileType(jsonObject.get("fileType").getAsString());
    fs.setDateCreated(jsonObject.get("dateCreated").getAsString());
    fs.setSplitFile1(jsonObject.get("splitFile1").toString().getBytes("UTF-8"));
    fs.setSplitFile2(jsonObject.get("splitFile2").toString().getBytes("UTF-8"));
    fs.setSplitFile3(jsonObject.get("splitFile3").toString().getBytes("UTF-8"));
    fs.setSplitFile4(jsonObject.get("splitFile4").toString().getBytes("UTF-8"));
    fs.setKeyBlock(jsonObject.get("keyBlock").toString().getBytes("UTF-8"));
    fs.setParBlock(jsonObject.get("parBlock").toString().getBytes("UTF-8"));
    fs.setNoOfFiles(jsonObject.get("noOfFiles").getAsInt());

    System.out.println(fs.getSplitFile1().length); -> Prints 46
    return fs;
}

这是我上传字节的代码:

public static void upload() throws Exception {
    byte[] b = Files.readAllBytes(new File("D:\\Hello.txt").toPath());
    byte[][] c = splitBytes(b, b.length / 3);

    String encKey = FileSecure.generateEncKey();

    byte[] b1 = null, b2 = null, b3 = null, b4 = null;
    byte[] alpha = null, beta = null, charlie = null, delta = null;

    for (byte[] bloc : c) {
        byte[] block = FileSecure.encrypt(bloc, encKey);

        if (b1 == null) {
            b1 = FileSecure.hash(block);
        }
        else if (b2 == null) {
            b2 = FileSecure.hash(block);
        }
        else if (b3 == null) {
            b3 = FileSecure.hash(block);
        }
        else if (b4 == null) {
            b4 = FileSecure.hash(block);
        }

        if (alpha == null) {
            alpha = block;
        }
        else if (beta == null) {
            beta = block;
        }
        else if (charlie == null) {
            charlie = block;
        }
        else if (delta == null) {
            delta = block;
        }
    }

    System.out.println(alpha.length); -> Prints 32
}

那么如何从JsonElement中提取原始字节数组?

0 个答案:

没有答案