如何从HttpResponse编码二进制PDF?

时间:2019-04-03 10:35:05

标签: c# pdf encoding httpresponse

我从HttpResponse获取二进制PDF,但是无法以正确的格式获取它

GoogleCredentials googleCredentails = 
            ServiceAccountCredentials.fromStream(new FileInputStream("D:\\Naresh\\Projects\\GoogleCloudStorage\\delta-trees-236311-ae7c1cfcda68.json"));

Storage googleStorageService = StorageOptions.newBuilder().setCredentials(googleCredentails)
            .setProjectId("delta-trees-236311")
            .build().getService();

String googleBucketName = "ewf-ocr-poc";
File f = new File("D:\\Naresh\\Projects\\GoogleCloudStorage\\EWFDocumentThroughJava.txt");
byte[] readFileToByteArray = readFileToByteArray(f);
String fileName = f.getName();
InputStream fis = new FileInputStream(f);        
googleStorageService.create(BlobInfo.newBuilder(googleBucketName, fileName).build(),
            readFileToByteArray);

2 个答案:

答案 0 :(得分:3)

PDF是字节而不是文本-看起来您是从http请求中获取PDF,在这种情况下,您不想使用StreamReaderstring或{{1} }-您只需要原始字节。您可能只需使用Encoding,其中httpResponse.GetResponseStream().CopyTo(someOtherStream)是该位置的someOtherStream,即

FileStream

答案 1 :(得分:2)

请勿使用"@sentry/types": "^5.0.0" StreamReader用于将字节流转换为字符流。但是,PDF是二进制文件,您不应将它们转换为文本。

只需将要下载的字节直接复制到文件中,而无需将其转换为字符:

StreamReader