@@ -74,6 +74,75 @@ public HeaderOption header() {
7474 return new HeaderOption ("Content-Type" , contentType + "; boundary=\" " + boundary + "\" " );
7575 }
7676
77+ private void writePartData (String partContent , byte [] byteArray ) throws IOException {
78+ out .write (partContent .getBytes (MULTIPART_ENCODING ));
79+ out .write (byteArray );
80+ String returnContent = RETURN + RETURN ;
81+ out .write (returnContent .getBytes (MULTIPART_ENCODING ));
82+ }
83+
84+ private String createPartHeader (Map <String , String > headers , String name , String contentType , String filename ) {
85+ String partContent = addBoundary ();
86+ if (headers != null ) {
87+ List <String > listContentDisposition = Arrays .asList ("filename" ,"creation-date" ,"modification-date" ,"read-date" ,
88+ "size" ,"name" ,"voice" ,"handling" ,"preview-type" );
89+ List <String > mainHeaders = Arrays .asList ("Content-Disposition" ,"Content-Type" ,"charset" );
90+
91+ if (headers .containsKey ("Content-Disposition" )) {
92+ partContent += "Content-Disposition:" +headers .get ("Content-Disposition" )+";" ;
93+ for (Map .Entry <String ,String > entry : headers .entrySet ()) {
94+ if (listContentDisposition .contains (entry .getKey ())) {
95+ partContent += " " + entry .getKey () + "=\" " + entry .getValue () + "\" ;" ;
96+ }
97+ }
98+ partContent = partContent .substring (0 , partContent .length ()-1 );
99+ partContent += RETURN ;
100+
101+ }
102+
103+ if (headers .containsKey ("Content-Type" )) {
104+ partContent += "Content-Type:" +headers .get ("Content-Type" )+";" ;
105+ if (headers .containsKey ("charset" )) {
106+ partContent += "charset=\" " + headers .get ("charset" ) + "\" ;" ;
107+ }
108+ partContent = partContent .substring (0 , partContent .length ()-1 );
109+ partContent += RETURN ;
110+ }
111+
112+ for (Map .Entry <String ,String > entry : headers .entrySet ()) {
113+ if (mainHeaders .contains (entry .getKey ())==false && listContentDisposition .contains (entry .getKey ())==false ) {
114+ partContent += entry .getKey () +":" +entry .getValue () + RETURN ;
115+ }
116+ }
117+ partContent += RETURN ;
118+ }
119+ else if (filename != null && name != null ) {
120+ partContent +=
121+ "Content-Disposition:form-data; name=\" " + name + "\" " + "; filename=\" " + filename + "\" " + RETURN +
122+ "Content-Type:" + contentType + RETURN +
123+ RETURN ;
124+ }
125+ else if (filename != null ) {
126+ partContent +=
127+ "Content-Disposition:form-data; filename=\" " + filename + "\" " + RETURN +
128+ "Content-Type:" + contentType + RETURN +
129+ RETURN ;
130+ }
131+ else if (name != null ){
132+ partContent +=
133+ "Content-Disposition:form-data; name=\" " + name + "\" " + RETURN +
134+ "Content-Type:" + contentType + RETURN +
135+ RETURN ;
136+ }
137+ else {
138+ partContent +=
139+ "Content-Disposition:form-data;" + RETURN +
140+ "Content-Type:" + contentType + RETURN +
141+ RETURN ;
142+ }
143+ return partContent ;
144+ }
145+
77146 /**
78147 * Add a part to the multipart body
79148 * @param name The name of the part
@@ -82,16 +151,8 @@ public HeaderOption header() {
82151 * @throws IOException Throws an exception if the output stream cannot be written to
83152 */
84153 public void addFormData (String name , String contentType , byte [] byteArray ) throws IOException {
85- String partContent = addBoundary ();
86- partContent +=
87- "Content-Disposition:form-data; name=\" " + name + "\" " + RETURN +
88- "Content-Type:" + contentType + RETURN +
89- RETURN ;
90- out .write (partContent .getBytes (MULTIPART_ENCODING ));
91- out .write (byteArray );
92- String returnContent = RETURN + RETURN ;
93- out .write (returnContent .getBytes (MULTIPART_ENCODING ));
94- System .out .println (partContent );
154+ String partContent = createPartHeader (null , name , contentType , null );
155+ writePartData (partContent , byteArray );
95156 }
96157
97158 /**
@@ -101,15 +162,8 @@ public void addFormData(String name, String contentType, byte[] byteArray) throw
101162 * @throws IOException Throws an exception if the output stream cannot be written to
102163 */
103164 public void addPart (String contentType , byte [] byteArray ) throws IOException {
104- String partContent = addBoundary ();
105- partContent +=
106- "Content-Disposition:form-data;" + RETURN +
107- "Content-Type:" + contentType + RETURN +
108- RETURN ;
109- out .write (partContent .getBytes (MULTIPART_ENCODING ));
110- out .write (byteArray );
111- String returnContent = RETURN + RETURN ;
112- out .write (returnContent .getBytes (MULTIPART_ENCODING ));
165+ String partContent = createPartHeader (null , null , contentType , null );
166+ writePartData (partContent , byteArray );
113167 }
114168
115169 /**
@@ -132,16 +186,8 @@ public void addHtmlPart(String name, String content) throws IOException {
132186 public void addFilePart (String name , String contentType , java .io .File file ) throws IOException {
133187 InputStream fileStream = new FileInputStream (file );
134188 byte [] fileBytes = getByteArray (fileStream );
135- String partContent = addBoundary ();
136- partContent +=
137- "Content-Disposition:form-data; name=\" " + name + "\" " + "; filename=\" " + file .getName () + "\" " + RETURN +
138- "Content-Type:" + contentType + RETURN +
139- RETURN ;
140- System .out .println (partContent );
141- out .write (partContent .getBytes (MULTIPART_ENCODING ));
142- out .write (fileBytes );
143- String returnContent = RETURN + RETURN ;
144- out .write (returnContent .getBytes (MULTIPART_ENCODING ));
189+ String partContent = createPartHeader (null , name , contentType , file .getName ());
190+ writePartData (partContent , fileBytes );
145191 }
146192
147193 /**
@@ -151,44 +197,8 @@ public void addFilePart(String name, String contentType, java.io.File file) thro
151197 * @throws IOException Throws an exception if the output stream cannot be written to
152198 */
153199 public void addPart (Map <String , String > headers , byte [] content ) throws IOException {
154- String partContent = addBoundary ();
155- List <String > listContentDisposition = Arrays .asList ("filename" ,"creation-date" ,"modification-date" ,"read-date" ,
156- "size" ,"name" ,"voice" ,"handling" ,"preview-type" );
157- List <String > mainHeaders = Arrays .asList ("Content-Disposition" ,"Content-Type" ,"charset" );
158-
159- if (headers .containsKey ("Content-Disposition" )) {
160- partContent += "Content-Disposition:" +headers .get ("Content-Disposition" )+";" ;
161- for (Map .Entry <String ,String > entry : headers .entrySet ()) {
162- if (listContentDisposition .contains (entry .getKey ())) {
163- partContent += " " + entry .getKey () + "=\" " + entry .getValue () + "\" ;" ;
164- }
165- }
166- partContent = partContent .substring (0 , partContent .length ()-1 );
167- partContent += RETURN ;
168-
169- }
170-
171- if (headers .containsKey ("Content-Type" )) {
172- partContent += "Content-Type:" +headers .get ("Content-Type" )+";" ;
173- if (headers .containsKey ("charset" )) {
174- partContent += "charset=\" " + headers .get ("charset" ) + "\" ;" ;
175- }
176- partContent = partContent .substring (0 , partContent .length ()-1 );
177- partContent += RETURN ;
178- }
179-
180- for (Map .Entry <String ,String > entry : headers .entrySet ()) {
181- if (mainHeaders .contains (entry .getKey ())==false && listContentDisposition .contains (entry .getKey ())==false ) {
182- partContent += entry .getKey () +":" +entry .getValue () + RETURN ;
183- }
184- }
185-
186- System .out .println (partContent );
187- partContent += RETURN ;
188- out .write (partContent .getBytes (MULTIPART_ENCODING ));
189- out .write (content );
190- String returnContent = RETURN + RETURN ;
191- out .write (returnContent .getBytes (MULTIPART_ENCODING ));
200+ String partContent = createPartHeader (headers , null , null , null );
201+ writePartData (partContent , content );
192202 }
193203
194204 /**
0 commit comments