在 Post 请求之前将字节数组转换为 C# 字节数组
Converting a byte array onto a C# byte array before Post request
我必须构建一个像这样的 JSON 对象:
{
"sessionId": "DA2F5102377742BE8063ADBC8968A294",
"documentId": "c2f84034-ea1c-4406-8a8b-ab2c00a1b537",
"sourceFile": {
"MimeType": "application/pdf",
"SourceFile": [
25,
68,
56,...
]
}
为此我创建了 2 个 POJO:
@Component
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class UpdateSourceFileRequestModel {
private String sessionId;
private ReportingDataRequestModel reportingData;
private String documentId;
private DocumentSourceFileRequestModel sourceFile;
}
并且:
@Component
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class DocumentSourceFileRequestModel {
// for GettingSourceFile
@SerializedName("MimeType")
private String mimeType;
@SerializedName("SourceFile")
private int[] sourceFile;
}
我必须使用 SerializedName,因为 JSON 区分大小写。到目前为止一切顺利,直到那里一切都很好。
现在,我正在尝试将 pdf 文件转换为字节数组。通过阅读文件可以很容易地完成:
byte[] array = Files.readAllBytes(Paths.get(fileToReturn));
我的问题是我必须 return 一个 C# 字节数组(从 0 到 255 的无符号数)。
为了解决这个问题,我尝试了这个:
int[] result = new int[array.length];
for(int i = 0; i < array.length; i++) {
int posInt = array[i] & 0xFF;
byte bValue = (byte) posInt;
result[i] = bValue;
}
为了满足API的要求,我复制了一个有符号字节数组(-128+127)并将其转换为一个int数组(0到255)。这段代码是在 Whosebug 上找到的。我的问题是转换后的文件无法用 Acrobat 读取。
这里是整个方法代码:
private void updateSourceFile(String fileToReturn, LogOnWithPassword2RestResponseModel login) throws IOException {
System.out.println("\nSending HTTP POST request - returning file process");
// retrieve the file
Path path = Paths.get(fileToReturn);
File f = path.toFile();
// convert file to byte array
byte[] array = Files.readAllBytes(Paths.get(fileToReturn));
int[] result = new int[array.length];
for(int i = 0; i < array.length; i++) {
int posInt = array[i] & 0xFF;
byte anotherB = (byte) posInt;
result[i] =anotherB;
}
UpdateSourceFileRequestModel updateSourceFileRequestModel = new UpdateSourceFileRequestModel();
DocumentSourceFileRequestModel documentSourceFileRequestModel = new DocumentSourceFileRequestModel();
documentSourceFileRequestModel.setMimeType("application/pdf");
documentSourceFileRequestModel.setSourceFile(result);
updateSourceFileRequestModel.setSessionId(login.getD().getSessionId());
updateSourceFileRequestModel.setDocumentId(docId);
updateSourceFileRequestModel.setSourceFile(documentSourceFileRequestModel);
updateSourceFileRequestModel.setReportingData(null);
String url = "http://localhost/TotalAgility/Services/SDK/CaptureDocumentService.svc/json/UpdateSourceFile";
Gson gson = new GsonBuilder().setPrettyPrinting().create();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String jsonInputString = gson.toJson(updateSourceFileRequestModel);
StringEntity postingString = new StringEntity(jsonInputString);
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
post.setHeader("accept", "application/json");
HttpResponse response = httpClient.execute(post);
}
这些行:
byte bValue = (byte) posInt;
result[i] = bValue;
打败了转换为整数的目的,因为您从字面上获取整数结果(通常 >127)并将其转换回字节(这将再次导致超过 127 的负值)。由于问题是正确转换为 json 且没有负值,因此绝对不要在将其转换为整数后再次将其转换为字节。
这很好:
int[] result = new int[array.length];
for(int i = 0; i < array.length; i++) {
result[i] = array[i] & 0xFF;
}
我也测试过这个:
byte[] array = Files.readAllBytes(Paths.get(fileToReturn));
int[] result = new int[array.length];
for(int i = 0; i < array.length; i++) {
if(array[i] < 0) {
result[i] = Byte.MAX_VALUE - array[i];
} else {
result[i] = array[i];
}
}
效果不错
我认为您使用函数从 C# 转换为 Java,而您需要相反的方法:从 Java 字节到 C# byte.If 您想要转换为 0 -255 值并将其存储在有符号整数中,尝试将 256 添加到每个字节:
int[] result = new int[array.length];
for(int i = 0; i < array.length; i++) {
int uByte = array[i] + 256;
result[i] = uByte;
}
我必须构建一个像这样的 JSON 对象:
{
"sessionId": "DA2F5102377742BE8063ADBC8968A294",
"documentId": "c2f84034-ea1c-4406-8a8b-ab2c00a1b537",
"sourceFile": {
"MimeType": "application/pdf",
"SourceFile": [
25,
68,
56,...
]
}
为此我创建了 2 个 POJO:
@Component
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class UpdateSourceFileRequestModel {
private String sessionId;
private ReportingDataRequestModel reportingData;
private String documentId;
private DocumentSourceFileRequestModel sourceFile;
}
并且:
@Component
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class DocumentSourceFileRequestModel {
// for GettingSourceFile
@SerializedName("MimeType")
private String mimeType;
@SerializedName("SourceFile")
private int[] sourceFile;
}
我必须使用 SerializedName,因为 JSON 区分大小写。到目前为止一切顺利,直到那里一切都很好。
现在,我正在尝试将 pdf 文件转换为字节数组。通过阅读文件可以很容易地完成:
byte[] array = Files.readAllBytes(Paths.get(fileToReturn));
我的问题是我必须 return 一个 C# 字节数组(从 0 到 255 的无符号数)。 为了解决这个问题,我尝试了这个:
int[] result = new int[array.length];
for(int i = 0; i < array.length; i++) {
int posInt = array[i] & 0xFF;
byte bValue = (byte) posInt;
result[i] = bValue;
}
为了满足API的要求,我复制了一个有符号字节数组(-128+127)并将其转换为一个int数组(0到255)。这段代码是在 Whosebug 上找到的。我的问题是转换后的文件无法用 Acrobat 读取。
这里是整个方法代码:
private void updateSourceFile(String fileToReturn, LogOnWithPassword2RestResponseModel login) throws IOException {
System.out.println("\nSending HTTP POST request - returning file process");
// retrieve the file
Path path = Paths.get(fileToReturn);
File f = path.toFile();
// convert file to byte array
byte[] array = Files.readAllBytes(Paths.get(fileToReturn));
int[] result = new int[array.length];
for(int i = 0; i < array.length; i++) {
int posInt = array[i] & 0xFF;
byte anotherB = (byte) posInt;
result[i] =anotherB;
}
UpdateSourceFileRequestModel updateSourceFileRequestModel = new UpdateSourceFileRequestModel();
DocumentSourceFileRequestModel documentSourceFileRequestModel = new DocumentSourceFileRequestModel();
documentSourceFileRequestModel.setMimeType("application/pdf");
documentSourceFileRequestModel.setSourceFile(result);
updateSourceFileRequestModel.setSessionId(login.getD().getSessionId());
updateSourceFileRequestModel.setDocumentId(docId);
updateSourceFileRequestModel.setSourceFile(documentSourceFileRequestModel);
updateSourceFileRequestModel.setReportingData(null);
String url = "http://localhost/TotalAgility/Services/SDK/CaptureDocumentService.svc/json/UpdateSourceFile";
Gson gson = new GsonBuilder().setPrettyPrinting().create();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String jsonInputString = gson.toJson(updateSourceFileRequestModel);
StringEntity postingString = new StringEntity(jsonInputString);
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
post.setHeader("accept", "application/json");
HttpResponse response = httpClient.execute(post);
}
这些行:
byte bValue = (byte) posInt;
result[i] = bValue;
打败了转换为整数的目的,因为您从字面上获取整数结果(通常 >127)并将其转换回字节(这将再次导致超过 127 的负值)。由于问题是正确转换为 json 且没有负值,因此绝对不要在将其转换为整数后再次将其转换为字节。
这很好:
int[] result = new int[array.length];
for(int i = 0; i < array.length; i++) {
result[i] = array[i] & 0xFF;
}
我也测试过这个:
byte[] array = Files.readAllBytes(Paths.get(fileToReturn));
int[] result = new int[array.length];
for(int i = 0; i < array.length; i++) {
if(array[i] < 0) {
result[i] = Byte.MAX_VALUE - array[i];
} else {
result[i] = array[i];
}
}
效果不错
我认为您使用函数从 C# 转换为 Java,而您需要相反的方法:从 Java 字节到 C# byte.If 您想要转换为 0 -255 值并将其存储在有符号整数中,尝试将 256 添加到每个字节:
int[] result = new int[array.length];
for(int i = 0; i < array.length; i++) {
int uByte = array[i] + 256;
result[i] = uByte;
}