1 | private byte[] createExpectedFilepartOutput(↵ | | 1 | private byte[] createExpectedFilepartOutput(↵
|
2 | String boundaryString,↵ | | 2 | String boundaryString,↵
|
3 | String fileField,↵ | | 3 | String fileField,↵
|
4 | File file,↵ | | 4 | File file,↵
|
5 | String mimeType,↵ | | 5 | String mimeType,↵
|
6 | byte[] fileContent,↵ | | 6 | byte[] fileContent,↵
|
7 | boolean firstMultipart,↵ | | 7 | boolean firstMultipart,↵
|
8 | boolean lastMultipart) throws IOException {↵ | | 8 | boolean lastMultipart) throws IOException {↵
|
9 | // The encoding used for http headers and control information↵ | | |
|
10 | final String httpEncoding = "ISO-8859-1";↵ | | |
|
11 | final byte[] DASH_DASH = "--".getBytes(httpEncoding);↵ | | 9 | final byte[] DASH_DASH = new String("--").getBytes(ISO_8859_1);↵
|
12 | ↵ | | 10 | ↵
|
13 | final ByteArrayOutputStream output = new ByteArrayOutputStream();↵ | | 11 | final ByteArrayOutputStream output = new ByteArrayOutputStream();↵
|
14 | if(firstMultipart) {↵ | | 12 | if(firstMultipart) {↵
|
15 | output.write(DASH_DASH);↵ | | 13 | output.write(DASH_DASH);↵
|
16 | output.write(boundaryString.getBytes(httpEncoding));↵ | | 14 | output.write(boundaryString.getBytes(ISO_8859_1));↵
|
17 | output.write(CRLF);↵ | | 15 | output.write(CRLF);↵
|
18 | }↵ | | 16 | }↵
|
19 | // replace all backslash with double backslash↵ | | 17 | // replace all backslash with double backslash↵
|
20 | String filename = file.getName();↵ | | 18 | String filename = file.getName();↵
|
21 | output.write("Content-Disposition: form-data; name=\"".getBytes(httpEncoding));↵ | | 19 | output.write("Content-Disposition: form-data; name=\"".getBytes(ISO_8859_1));↵
|
22 | output.write(fileField.getBytes(httpEncoding));↵ | | 20 | output.write(fileField.getBytes(ISO_8859_1));↵
|
23 | output.write(("\"; filename=\"" + filename + "\"").getBytes(httpEncoding));↵ | | 21 | output.write(("\"; filename=\"" + filename + "\"").getBytes(ISO_8859_1));↵
|
24 | output.write(CRLF);↵ | | 22 | output.write(CRLF);↵
|
25 | output.write("Content-Type: ".getBytes(httpEncoding));↵ | | 23 | output.write("Content-Type: ".getBytes(ISO_8859_1));↵
|
26 | output.write(mimeType.getBytes(httpEncoding));↵ | | 24 | output.write(mimeType.getBytes(ISO_8859_1));↵
|
27 | output.write(CRLF);↵ | | 25 | output.write(CRLF);↵
|
28 | output.write("Content-Transfer-Encoding: binary".getBytes(httpEncoding));↵ | | 26 | output.write("Content-Transfer-Encoding: binary".getBytes(ISO_8859_1));↵
|
29 | output.write(CRLF);↵ | | 27 | output.write(CRLF);↵
|
30 | output.write(CRLF);↵ | | 28 | output.write(CRLF);↵
|
31 | output.write(fileContent);↵ | | 29 | output.write(fileContent);↵
|
32 | output.write(CRLF);↵ | | 30 | output.write(CRLF);↵
|
33 | output.write(DASH_DASH);↵ | | 31 | output.write(DASH_DASH);↵
|
34 | output.write(boundaryString.getBytes(httpEncoding));↵ | | 32 | output.write(boundaryString.getBytes(ISO_8859_1));↵
|
35 | if(lastMultipart) {↵ | | 33 | if(lastMultipart) {↵
|
36 | output.write(DASH_DASH);↵ | | 34 | output.write(DASH_DASH);↵
|
37 | }↵ | | 35 | }↵
|
38 | output.write(CRLF);↵ | | 36 | output.write(CRLF);↵
|
39 | ↵ | | 37 | ↵
|
40 | output.flush();↵ | | 38 | output.flush();↵
|
41 | output.close();↵ | | 39 | output.close();↵
|
|
42 | return output.toByteArray();↵ | | 40 | return output.toByteArray();↵
|
43 | | | 41 |
|