Skip to content

Instantly share code, notes, and snippets.

@tophyr
Created October 25, 2012 16:30
Show Gist options
  • Save tophyr/3953852 to your computer and use it in GitHub Desktop.
Save tophyr/3953852 to your computer and use it in GitHub Desktop.
@Override
protected void writeMessage(OutputStream os) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(os);
String json = getGson().toJson(m_Envelope);
osw.write("\n\n--"); osw.write(MULTIPART_BOUNDARY); osw.write("\n");
osw.write("Content-Type: application/json\n");
osw.write("Content-Disposition: form-data\n\n");
osw.write(json);
osw.flush();
double totalSize = 1; // prevent div-by-zero exceptions
for (Document doc : m_Envelope.getDocuments())
totalSize += doc.getDataSize();
byte[] buf = new byte[1024];
long written = 0;
for (Document doc : m_Envelope.getDocuments()) {
osw.write("\n--"); osw.write(MULTIPART_BOUNDARY); osw.write("\n");
osw.write("Content-Type: "); osw.write(doc.getMimeType()); osw.write("\n");
osw.write(String.format("Content-Disposition: file; filename=\"%s\"; documentId=\"%d\"\n", doc.getName(), doc.getID()));
osw.flush();
InputStream is = doc.getDataStream();
try {
int i = 0;
while ((i = is.read(buf)) > 0)
{
os.write(buf, 0, i);
written += i;
if (m_ProgressListener != null)
m_ProgressListener.madeProgress(written / totalSize);
}
} finally {
is.close();
}
}
}
~/Projects $less EnvelopeUploaderImpl_1350968626183.out
{"emailSubject":"DSI-1684778549","emailBlurb":"","documents":[{"name":"DSI-1684778549","documentId":1}]}
--+5asw822nfrdTdakgjr6i6zqCgJ0cW06
Content-Type: application/json
Content-Disposition: form-data
{"emailSubject":"DSI-1684778549","emailBlurb":"","documents":[{"name":"DSI-1684778549","documentId":1}]}
--+5asw822nfrdTdakgjr6i6zqCgJ0cW06
Content-Type: image/jpeg
Content-Disposition: file; filename="DSI-1684778549"; documentId="1"
--+5asw822nfrdTdakgjr6i6zqCgJ0cW06
Content-Type: image/jpeg
Content-Disposition: file; filename="DSI-1684778549"; documentId="1"
<FF><D8><FF><E0>^@^PJFIF^@^A^A^@^@^A^@^A^@^@<FF><DB>^@C^@^H^F^F^G^F^E^H^G^G^G
^L^T^M^L^K^K^L^Y^R^S^O^T^]^Z^_^^^]^Z^\^\ $.' ",#^\^\(7),01444^_'9=82<.342<FF><DB>^@C^A ^L^K^L^X^M^M^X2!^\!22222222222222222222222222222222222222222222222222<FF><C0>^@^Q^H^A<E0>^B<80>^C^A"^@^B^Q^A^C^Q^A<FF><C4>^@^_^@^@^A^E^A^A^A^A^A^A^@^@^@^@^@^@^@^@^A^B^C^D^E^F^G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment