@@ -11,7 +11,6 @@ public class Multipart {
11
11
private HttpURLConnection httpConn ;
12
12
private String charset ;
13
13
private OutputStream out ;
14
- private PrintWriter writer ;
15
14
16
15
public Multipart (String requestURL , String charset ) {
17
16
this .charset = charset ;
@@ -27,7 +26,6 @@ public Multipart(String requestURL, String charset) {
27
26
httpConn .setRequestProperty ("Content-Type" , "multipart/form-data; boundary=" + boundary );
28
27
httpConn .setRequestProperty ("User-Agent" , "Java IPFS CLient" );
29
28
out = httpConn .getOutputStream ();
30
- writer = new PrintWriter (new OutputStreamWriter (out , charset ), true );
31
29
} catch (IOException e ) {
32
30
throw new RuntimeException (e .getMessage (), e );
33
31
}
@@ -41,15 +39,20 @@ public static String createBoundary() {
41
39
b .append (allowed .charAt (r .nextInt (allowed .length ())));
42
40
return b .toString ();
43
41
}
42
+
43
+ private Multipart append (String value ) throws IOException {
44
+ out .write (value .getBytes (charset ));
45
+ return this ;
46
+ }
44
47
45
- public void addFormField (String name , String value ) {
46
- writer . append ("--" ).append (boundary ).append (LINE_FEED );
47
- writer . append ("Content-Disposition: form-data; name=\" " ).append (name ).append ("\" " )
48
+ public void addFormField (String name , String value ) throws IOException {
49
+ append ("--" ).append (boundary ).append (LINE_FEED );
50
+ append ("Content-Disposition: form-data; name=\" " ).append (name ).append ("\" " )
48
51
.append (LINE_FEED );
49
- writer . append ("Content-Type: text/plain; charset=" ).append (charset ).append (LINE_FEED );
50
- writer . append (LINE_FEED );
51
- writer . append (value ).append (LINE_FEED );
52
- writer .flush ();
52
+ append ("Content-Type: text/plain; charset=" ).append (charset ).append (LINE_FEED );
53
+ append (LINE_FEED );
54
+ append (value ).append (LINE_FEED );
55
+ out .flush ();
53
56
}
54
57
55
58
public void addSubtree (Path parentPath , NamedStreamable dir ) throws IOException {
@@ -63,14 +66,14 @@ public void addSubtree(Path parentPath, NamedStreamable dir) throws IOException
63
66
}
64
67
}
65
68
66
- public void addDirectoryPart (Path path ) {
67
- writer . append ("--" ).append (boundary ).append (LINE_FEED );
68
- writer . append ("Content-Disposition: file; filename=\" " ).append (encode (path .toString ())).append ("\" " ).append (LINE_FEED );
69
- writer . append ("Content-Type: application/x-directory" ).append (LINE_FEED );
70
- writer . append ("Content-Transfer-Encoding: binary" ).append (LINE_FEED );
71
- writer . append (LINE_FEED );
72
- writer . append (LINE_FEED );
73
- writer .flush ();
69
+ public void addDirectoryPart (Path path ) throws IOException {
70
+ append ("--" ).append (boundary ).append (LINE_FEED );
71
+ append ("Content-Disposition: file; filename=\" " ).append (encode (path .toString ())).append ("\" " ).append (LINE_FEED );
72
+ append ("Content-Type: application/x-directory" ).append (LINE_FEED );
73
+ append ("Content-Transfer-Encoding: binary" ).append (LINE_FEED );
74
+ append (LINE_FEED );
75
+ append (LINE_FEED );
76
+ out .flush ();
74
77
}
75
78
76
79
private static String encode (String in ) {
@@ -81,17 +84,17 @@ private static String encode(String in) {
81
84
}
82
85
}
83
86
84
- public void addFilePart (String fieldName , Path parent , NamedStreamable uploadFile ) {
87
+ public void addFilePart (String fieldName , Path parent , NamedStreamable uploadFile ) throws IOException {
85
88
Optional <String > fileName = uploadFile .getName ().map (n -> encode (parent .resolve (n ).toString ().replace ('\\' ,'/' )));
86
- writer . append ("--" ).append (boundary ).append (LINE_FEED );
89
+ append ("--" ).append (boundary ).append (LINE_FEED );
87
90
if (!fileName .isPresent ())
88
- writer . append ("Content-Disposition: file; name=\" " ).append (fieldName ).append ("\" ;" ).append (LINE_FEED );
91
+ append ("Content-Disposition: file; name=\" " ).append (fieldName ).append ("\" ;" ).append (LINE_FEED );
89
92
else
90
- writer . append ("Content-Disposition: file; filename=\" " ).append (fileName .get ()).append ("\" ;" ).append (LINE_FEED );
91
- writer . append ("Content-Type: application/octet-stream" ).append (LINE_FEED );
92
- writer . append ("Content-Transfer-Encoding: binary" ).append (LINE_FEED );
93
- writer . append (LINE_FEED );
94
- writer .flush ();
93
+ append ("Content-Disposition: file; filename=\" " ).append (fileName .get ()).append ("\" ;" ).append (LINE_FEED );
94
+ append ("Content-Type: application/octet-stream" ).append (LINE_FEED );
95
+ append ("Content-Transfer-Encoding: binary" ).append (LINE_FEED );
96
+ append (LINE_FEED );
97
+ out .flush ();
95
98
96
99
try {
97
100
InputStream inputStream = uploadFile .getInputStream ();
@@ -105,20 +108,21 @@ public void addFilePart(String fieldName, Path parent, NamedStreamable uploadFil
105
108
throw new RuntimeException (e .getMessage (), e );
106
109
}
107
110
108
- writer . append (LINE_FEED );
109
- writer .flush ();
111
+ append (LINE_FEED );
112
+ out .flush ();
110
113
}
111
114
112
- public void addHeaderField (String name , String value ) {
113
- writer . append (name + ": " + value ).append (LINE_FEED );
114
- writer .flush ();
115
+ public void addHeaderField (String name , String value ) throws IOException {
116
+ append (name + ": " + value ).append (LINE_FEED );
117
+ out .flush ();
115
118
}
116
119
117
- public String finish () {
120
+ public String finish () throws IOException {
118
121
StringBuilder b = new StringBuilder ();
119
122
120
- writer .append ("--" + boundary + "--" ).append (LINE_FEED );
121
- writer .close ();
123
+ append ("--" + boundary + "--" ).append (LINE_FEED );
124
+ out .flush ();
125
+ out .close ();
122
126
123
127
try {
124
128
int status = httpConn .getResponseCode ();
0 commit comments