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

该服务不仅会自动返回文件,还会返回其他几个MIME字段。因此,当我使用entity.getContent()打开inputstream时,最终将所有这些数据写入到我的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) {}
}

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

---- 20170803161934
内容处置:form-data; name =“PayloadType”

X12_999_Response_005010X231A1
---- 20170803161934
内容处置:form-data; name =“ProcessingMode”

批量
---- 20170803161934
内容处置:form-data; name =“PayloadID”

12345-abcde
---- 20170803161934
内容处置:form-data; name =“TimeStamp”

2017-08-08T16:46:34Z
---- 20170803161934
内容处置:form-data; name =“CORERuleVersion”

2.2.0
---- 20170803161934
内容处置:form-data; name =“ReceiverID”

99000061
---- 20170803161934
内容处置:form-data; name =“SenderID”

KYMEDICAID
---- 20170803161934
内容处置:form-data; name =“ErrorCode”

成功
---- 20170803161934
内容处置:form-data; name =“ErrorMessage”

---- 20170803161934
内容处置:表单数据; name =“有效载荷”

PKÁIKšŽÌ*•*> 511257719_511257718_837P5010X12BATCH_99000061.199786.1.999.date»0E…ùNvB^ lQJT1¥CéÀ§äÛkR)`O¾Ç:–s‰Â¥×Ïm〜_Ï4æ! .199786.1.999.datPKlñ
---- 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/

10-11 00:24