gpt4 book ai didi

java - 如何从 Java 中的 HttpResponse 获取单个表单字段并将其写入文件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:49:46 24 4
gpt4 key购买 nike

我正在调用客户端的下载服务,该服务会发回 MIME 数据并尝试保存一个 zip 文件。

该服务不仅返回文件本身,还返回其他几个 MIME 字段。因此,当我使用 entity.getContent() 打开输入流时,我最终将所有这些数据写入了我的 zip 文件,而我只想写入一部分“有效负载”。我已经搜索过,但无论如何都看不到从内容中仅获取一个单独的部分。

代码如下:

HttpResponse response = services.getFileByPayloadID("12345-abcde");

BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(response.getEntity().getContent());
} catch (UnsupportedOperationException | IOException e) {
e.printStackTrace();
}
String filePath = "c:\\sample.zip";
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));

int inByte;

while((inByte = bis.read()) != -1) {
bos.write(inByte);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {bis.close();} catch (Exception e) {}
try {bos.close();} catch (Exception e) {}
}

生成的文件内容如下所示。请注意,我只想将“有效负载”的实际二进制内容写入文件。如有任何指点或建议,我们将不胜感激!

----20170803161934Content-Disposition: form-data;name="有效负载类型"

X12_999_Response_005010X231A1----20170803161934Content-Disposition: 表单数据;name="ProcessingMode"

批量----20170803161934Content-Disposition:form-data;name="PayloadID"

12345-abcde----20170803161934内容处置:表单数据;名称=“时间戳”

2017-08-08T16:46:34Z----20170803161934内容配置:表单数据;名称="CORERuleVersion"

2.2.0----20170803161934Content-Disposition:form-data;name="ReceiverID"

99000061----20170803161934内容配置:表单数据;名称=“发件人ID”

KYMEDICAID----20170803161934内容处置:表单数据;名称=“错误代码”

成功----20170803161934内容处置:表单数据;名称=“错误消息”

----20170803161934Content-Disposition:表单数据;名称=“有效载荷”

PK ÁIKšŽÌ*• * > 511257719_511257718_837P5010X12BATCH_99000061.199786.1.999.date»Â0E…ùNvB^lQJT1¥CéÀ§äÛkR)`O¾Ç:–s‰Ï¥±ÏÄÚkR)`O¾Ç:–s‰Ï±ÏÄÏýŽ! * > 511257719_511257718_837P5010X12BATCH_99000061.199786.1.999.datPK lñ
----20170803161934

最佳答案

您的解决方案是读取您的字节,将其解析为字符串 => 将此字符串与您要开始写入内容的模式进行比较(在您的情况下为“有效负载”)。当您达到此模式时,然后开始将流的其他部分写入文件。这是您的示例代码:

HttpResponse response = services.getFileByPayloadID("12345-abcde");
ByteArrayOutputStream buf = new ByteArrayOutputStream();
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(response.getEntity().getContent());
} catch (UnsupportedOperationException | IOException e) {
e.printStackTrace();
}
String filePath = "c:\\sample.zip";
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
String output = "";
int inByte;
String charset = "your-charset"; //using charset to ensure that you can parse string correct to compare it.
boolean start = false;
while((inByte = bis.read()) != -1) {
if (!start){
buf.write((byte) inByte);
output = buf.toString(charset);
if (output.endsWith("name=\"Payload\"")){ //compare with your pattern to determine when will start write to file
start = true;
}
}
if(start){
bos.write(inByte);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {bis.close();} catch (Exception e) {}
try {bos.close();} catch (Exception e) {}
}

关于java - 如何从 Java 中的 HttpResponse 获取单个表单字段并将其写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45578097/

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