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

How to obtain just a single form field from an HttpResponse in Java and write it to a file?

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

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

代码如下:

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) {}
}

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

----20170803161934 内容配置:表单数据;名称="PayloadType"

X12_999_Response_005010X231A1 ----20170803161934 内容配置:表单数据;名称="ProcessingMode"

批量 ----20170803161934 内容配置:表单数据;名称="PayloadID"

12345-abcde ----20170803161934 内容配置:表单数据;名称="TimeStamp"

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

2.2.0 ----20170803161934 内容配置:表单数据;名称="ReceiverID"

99000061 ----20170803161934 内容配置:表单数据;名称="SenderID"

KYMEDICAID ----20170803161934 内容配置:表单数据;名称="ErrorCode"

成功 ----20170803161934 内容配置:表单数据;名称="ErrorMessage"

----20170803161934 Content-Disposition:表单数据;名字="Payload"

PK ÁIKšŽÌ*• * > 511257719_511257718_837P5010X12BATCH_99000061.199786.1.999.date»Â0E…ùNvB^lQJT1¥CéÀ§äÛkR)`O¾Ç:–s‰¥×Ï´m∼_Ï4æ! æ±G!P+ÈGÄŽ• * > 511257719_511257718_837P5010X12BATCH_99000061.199786.1.999.datPK l ñ
----20170803161934

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

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) {}
}