gpt4 book ai didi

java - 将 C# 字节数组转换为 Java 字节数组以及 Java 中的最大数组大小

转载 作者:行者123 更新时间:2023-12-01 19:18:25 24 4
gpt4 key购买 nike

我的代码有两个问题。我必须从 KTA (Kofax Total Agility) 调用 REST API。我向入口点发送一个调用:

{
"sessionId" : "EG346A9A4BD94E45C09106DA457596293",
"reportingData": null,
"documentId": "c12ebf4e-b756-4c55-a0e6-ab2200f16990"
}

并收到回复:

{
"d": {
"__type": "DocumentSourceFile:http://www.kofax.com/agility/services/sdk",
"MimeType": "application/pdf",
"SourceFile": [
37, 80, 68, 70, 226,227,207,211...
]
}
}

根据文档,它返回一个字节数组,并且该字节数组应转换为 pdf 文件。到目前为止,一切都很好 !现在我遇到了一些问题:

  • 文档指定我应该接收一个包含文件数据的字节,但它看起来像一个 int 数组或无符号字节(值大于 127)
  • 我尝试检索文档,为此我在基本类中对字节数组进行了硬编码。由于长度原因,我收到一条错误消息:java:代码太大

这是我用来尝试是否可以将其转换为文件的代码:

public static int[] sourceFile = new int[]{37,80,68,70,45,49,46,52,10,37,226,227,207,211,

// test variables
private static String FILEPATH = "";
private static File file = new File(FILEPATH);

// convert to byte array
private static void getSourceFile(int[] sourceFile) {
byte[] returnValue = new byte[sourceFile.length];
for(int i = 0; i < sourceFile.length; i++) {
byte b = (byte) Integer.parseInt(String.valueOf(sourceFile[i]));
returnValue[i] = b;
}
// byte to file
writeByte(returnValue);
}

private static void writeByte(byte[] bytes) {
try {
// Initialize a pointer in file using OutputStream
OutputStream os = new FileOutputStream(file);
// Starts writing the bytes in it
os.write(bytes);
System.out.println("Successfully byte inserted");
// Close the file
os.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}

public static void main(String[] args) {
//getSourceFile(sourceFile);
System.out.println(sourceFile.length);
}

最佳答案

我终于解决了有关错误消息和字节数组的问题。该代码在现实情况下不起作用。我将使用其中的一部分将其放入应用程序中。我没有在类中对数组进行硬编码,而是将数组的内容放在文本文件中。在应用程序中,我将收到一个带有字节数组的 json。

// convert a file to string
private static String fileToString() {
StringBuilder stringBuilder = new StringBuilder();
try(Stream<String> stream = Files.lines(Paths.get("source.txt"), StandardCharsets.UTF_8)) {
stream.forEach(str -> stringBuilder.append(str).append(","));
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println("Strinbuilder : " + stringBuilder.toString());
return stringBuilder.toString();
}

第二步和第三步是将字符串转换为字节数组。

// convert a string to array of string
private static String[] stringToArray(String s) {
String[] returnValue;
String delimiter = ",";

returnValue = s.split(delimiter);

return returnValue;
}

// convert an array of string to an array of byte
private static byte[] stringToByte(String[] str) {
byte[] returnValue = new byte[str.length];
try {
for (int i = 0; i < str.length; i++) {
returnValue[i] = (byte) Integer.parseInt(String.valueOf(str[i]));
System.out.println("returnValue[i] = " + returnValue[i]);
}
return returnValue;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

最后我将字节数组转换为pdf文件。

private static void writeByte(byte[] bytes) {
try {
// Initialize a pointer in file using OutputStream
OutputStream os = new FileOutputStream("out.pdf");
// Starts writing the bytes in it
os.write(bytes);
System.out.println("Successfully byte inserted");
// Close the file
os.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}

主类只是为了检查我是否生成正确的输出并且它工作正常。

public static void main(String[] args) {
String s = fileToString();
String[] arr = stringToArray(s);
byte[] byteArray = stringToByte(arr);

assert byteArray != null;
writeByte(byteArray);
}

感谢 AntiqTech!

关于java - 将 C# 字节数组转换为 Java 字节数组以及 Java 中的最大数组大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com