将 C# 字节数组转换为 Java 字节数组和 Java 中的最大数组大小
Convert C# Byte array to Java byte array and max array size in Java
我的代码有两个问题。
我必须从 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...
]
}
}
根据文档,它 returns 一个字节数组,这个字节数组应该被转换成一个 pdf 文件。到目前为止,一切都很好 !现在我遇到了一些问题:
- 文档指定我应该接收一个包含文件数据的字节,但它看起来像一个 int 数组或无符号字节(值大于 127)
- 我试图检索文档,为此我在基本 class 中对字节数组进行了硬编码。由于长度原因,我收到一条错误消息: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);
}
我终于解决了关于错误消息和字节数组的问题。
此代码在现实世界中不起作用。我将使用其中的一部分将其放入应用程序中。
我没有在 class 中对数组进行硬编码,而是将数组的内容放在一个文本文件中。在应用程序中,我将收到一个带有字节数组的 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);
}
}
主要的 class 只是为了检查我是否生成了正确的输出并且它工作正常。
public static void main(String[] args) {
String s = fileToString();
String[] arr = stringToArray(s);
byte[] byteArray = stringToByte(arr);
assert byteArray != null;
writeByte(byteArray);
}
感谢 AntiqTech!
我的代码有两个问题。 我必须从 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...
]
}
}
根据文档,它 returns 一个字节数组,这个字节数组应该被转换成一个 pdf 文件。到目前为止,一切都很好 !现在我遇到了一些问题:
- 文档指定我应该接收一个包含文件数据的字节,但它看起来像一个 int 数组或无符号字节(值大于 127)
- 我试图检索文档,为此我在基本 class 中对字节数组进行了硬编码。由于长度原因,我收到一条错误消息: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);
}
我终于解决了关于错误消息和字节数组的问题。 此代码在现实世界中不起作用。我将使用其中的一部分将其放入应用程序中。 我没有在 class 中对数组进行硬编码,而是将数组的内容放在一个文本文件中。在应用程序中,我将收到一个带有字节数组的 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);
}
}
主要的 class 只是为了检查我是否生成了正确的输出并且它工作正常。
public static void main(String[] args) {
String s = fileToString();
String[] arr = stringToArray(s);
byte[] byteArray = stringToByte(arr);
assert byteArray != null;
writeByte(byteArray);
}
感谢 AntiqTech!