本文介绍了如何仅从Java中的HttpResponse获取单个表单字段并将其写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am calling a client's download service which sends back MIME data and attempting to save a zip file.

该服务不仅会自动返回文件,还会返回其他一些MIME字段.因此,当我使用entity.getContent()打开输入流时,最终将所有这些数据写入到我的zip文件中,而我只希望编写一部分有效载荷".我已经搜索过,但仍然看不到仅从内容中获得一个单独的部分.

The service does not simply return the file by itself but several other MIME fields as well. So when I open the inputstream using entity.getContent() I end up writing all of this data to my zip file whereas I only wish to write one part "Payload". I have searched and do not see anyway to obtain just the one individual part from the content.

代码如下:

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

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

Contents of the file that is produced are shown below. Note that I only wish to write the actual binary content for "Payload" to the file. Any pointers or suggestions would be greatly appreciated!

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

----20170803161934Content-Disposition: form-data;name="PayloadType"

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

X12_999_Response_005010X231A1----20170803161934Content-Disposition: form-data;name="ProcessingMode"

批次---- 20170803161934内容处置:form-data; name ="PayloadID"

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

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

12345-abcde----20170803161934Content-Disposition: form-data;name="TimeStamp"

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

2017-08-08T16:46:34Z----20170803161934Content-Disposition: form-data;name="CORERuleVersion"

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

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

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

99000061----20170803161934Content-Disposition: form-data;name="SenderID"

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

KYMEDICAID----20170803161934Content-Disposition: form-data;name="ErrorCode"

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

Success----20170803161934Content-Disposition: form-data;name="ErrorMessage"

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

----20170803161934Content-Disposition: form-data; name="Payload"

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

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

推荐答案

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

Solution for you is read you byte, parse it to string => compare this string with a pattern which you want to start writing the content (in your case is 'payload'). When you reach this pattern then start to write other part of you stream to file. Here is sample code for you:

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中的HttpResponse获取单个表单字段并将其写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 02:45