How can I get only one form field from an HttpResponse in Java and write it to a file?

I am calling a client download service that sends MIME data and tries to save the zip file.

The service doesn't just return the file by itself, but also several other MIME fields. So when I open an input stream using entity.getContent (), I end up writing all this data to my zip file, whereas I only want to write one "Payload" part. I have searched and cannot see anyway to only get one distinct piece from the content.

Code below:

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

      

The content of the generated file is shown below. Note that I want to write the actual binary content for the "payload" to a file. Any pointers or suggestions would be greatly appreciated!

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

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

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

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

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

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

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

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

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

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

PK ÁIKšŽÌ * * *> 511257719_511257718_837P5010X12BATCH_99000061.199786.1.999.date "Â0E ... ùNvB ^ lQJT1 ¥ CéÀ§äÛkR)` O¾Ç: -s   ¥ × Ï'm Ë * Pæ! > 511257719_511257718_837P5010X12BATCH_99000061.199786.1.999.datPK l -
---- 20170803161934

+3


source to share


1 answer


The solution is read by byte for you, parse it with a string => compare that string with the pattern you want to start writing, (in your case it's the "payload"). When you reach this pattern, start writing another part of your stream to a file. Here's some 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) {}
}

      

+1


source







All Articles