将C#字节数组转换为Java字节数组和Java中的最大数组大小
关于我的代码,我有两个问题。 我必须从KTA(Kofax Total Agility)调用REST API。我向入口点发送电话:将C#字节数组转换为Java字节数组和Java中的最大数组大小,java,arrays,kofax,Java,Arrays,Kofax,关于我的代码,我有两个问题。 我必须从KTA(Kofax Total Agility)调用REST API。我向入口点发送电话: { "sessionId" : "EG346A9A4BD94E45C09106DA457596293", "reportingData": null, "documentId": "c12ebf4e-b756-4c55-a0e6-ab2200f16990" } 并收到回复: { "d": { "__type": "Do
{
"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文件。到目前为止,一切都很好!现在我遇到一些问题:
- 文档指定我应该接收一个包含文件数据的字节,但它看起来像一个整数数组或无符号字节(值大于127)
- 我试图检索文档,为此我在一个基本类中硬编码了字节数组。由于长度原因,我收到一条错误消息:java:code太大
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);
}
public static int[]sourceFile=new int[]{37,80,68,70,45,49,46,52,10,37226227207211,
//测试变量
私有静态字符串FILEPATH=“”;
私有静态文件=新文件(文件路径);
//转换为字节数组
私有静态void getSourceFile(int[]sourceFile){
字节[]返回值=新字节[sourceFile.length];
对于(int i=0;i
我终于解决了有关错误消息和字节数组的问题。
这段代码在实际情况下不起作用。我将使用它的一部分将其放入应用程序中。
我没有在类中硬编码数组,而是将数组的内容放在一个文本文件中
// 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();
}
主类的目的只是检查我是否生成了正确的输出,并且工作正常
public static void main(String[] args) {
String s = fileToString();
String[] arr = stringToArray(s);
byte[] byteArray = stringToByte(arr);
assert byteArray != null;
writeByte(byteArray);
}
感谢AntiqTech!“sourceFile=new int[]{37,80,68,70,45,49,46,52,10,37226227207211,”您是这样复制粘贴整个文件内容的吗?字节数组有多长?Java中数组的最大长度是2147483647。如果您在运行时收到此数组,它可能不代表整个文件,并且可能是该文件可能被分成多个数组。因此,您只需编写一个数组大小较小的示例代码与原始数组进行比较,并以追加模式将其写入文件。每当您收到另一个数组时,您都会继续追加。事实上,我的数组包含24k个条目。我将通过从文件中读取来构建它。从现在起,我应该能够管理此部分…信息:根据Java虚拟机规范,方法m的代码不能超过65536字节。我今天学会了…谢谢。“我将通过从文件中读取来构建它”。此文件是您为测试代码而创建的,还是您根据REST API创建的文件?事实上,我测试并硬编码了REST请求的一些输出。我的帖子返回了一个糟糕的c#字节数组,它应该可以帮助我创建一个文件。事实上,我只是尝试从该数组重建文件。之后,我将在我的应用程序中实现此代码我只是把东西分开,然后试着看看我的API是如何工作的。因为我不能创建硬编码数组,所以我会从一个专门为测试目的创建的文件中生成它。我的请求会返回一个JSONI。我会尝试回答我的理解,我希望它会有用。在解析json的部分,你可以尝试直接写入字节,一个by一,放入如下文件:OutputStream os=new FileOutputStream(new File(“a.pdf”);JsonArray arr=jsonObject.getAsJsonObject(“d”).getAsJsonArray(“SourceFile”);for(int i=0;i
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);
}