@@ -59,35 +59,47 @@ public void totalBytesLoaded(long bytes) {
5959 };
6060 }
6161
62- MultipartUtility multipart = new MultipartUtility (apiUrl , "UTF-8" , this .cloudinary ().randomPublicId (), (Map <String , String >) options .get ("extra_headers" ), multipartCallback );
62+ MultipartUtility multipart = null ;
63+ HttpURLConnection connection ;
6364
64- // Remove blank parameters
65- for (Map .Entry <String , Object > param : params .entrySet ()) {
66- if (param .getValue () instanceof Collection ) {
67- for (Object value : (Collection ) param .getValue ()) {
68- multipart .addFormField (param .getKey () + "[]" , ObjectUtils .asString (value ));
69- }
70- } else {
71- if (StringUtils .isNotBlank (param .getValue ())) {
72- multipart .addFormField (param .getKey (), param .getValue ().toString ());
65+ try {
66+ multipart = new MultipartUtility (apiUrl , "UTF-8" , this .cloudinary ().randomPublicId (), (Map <String , String >) options .get ("extra_headers" ));
67+
68+ // Remove blank parameters
69+ for (Map .Entry <String , Object > param : params .entrySet ()) {
70+ if (param .getValue () instanceof Collection ) {
71+ for (Object value : (Collection ) param .getValue ()) {
72+ multipart .addFormField (param .getKey () + "[]" , ObjectUtils .asString (value ));
73+ }
74+ } else {
75+ if (StringUtils .isNotBlank (param .getValue ())) {
76+ multipart .addFormField (param .getKey (), param .getValue ().toString ());
77+ }
7378 }
7479 }
75- }
7680
77- if (file instanceof String && !((String ) file ).matches ("(?s)ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n =]+)" )) {
78- file = new File ((String ) file );
79- }
80- String filename = (String ) options .get ("filename" );
81- if (file instanceof File ) {
82- multipart .addFilePart ("file" , (File ) file , filename );
83- } else if (file instanceof String ) {
84- multipart .addFormField ("file" , (String ) file );
85- } else if (file instanceof InputStream ) {
86- multipart .addFilePart ("file" , (InputStream ) file , filename );
87- } else if (file instanceof byte []) {
88- multipart .addFilePart ("file" , new ByteArrayInputStream ((byte []) file ), filename );
89- }
90- HttpURLConnection connection = multipart .execute ();
81+ if (file instanceof String && !((String ) file ).matches ("(?s)ftp:.*|https?:.*|s3:.*|data:[^;]*;base64,([a-zA-Z0-9/+\n =]+)" )) {
82+ file = new File ((String ) file );
83+ }
84+ String filename = (String ) options .get ("filename" );
85+ if (file instanceof File ) {
86+ multipart .addFilePart ("file" , (File ) file , filename );
87+ } else if (file instanceof String ) {
88+ multipart .addFormField ("file" , (String ) file );
89+ } else if (file instanceof InputStream ) {
90+ multipart .addFilePart ("file" , (InputStream ) file , filename );
91+ } else if (file instanceof byte []) {
92+ multipart .addFilePart ("file" , new ByteArrayInputStream ((byte []) file ), filename );
93+ }
94+
95+ connection = multipart .execute ();
96+ } finally {
97+ if (multipart != null ){
98+ // Closing more than once has no effect so we can call it safely without having to check state
99+ multipart .close ();
100+ }
101+ }
102+
91103 int code ;
92104 try {
93105 code = connection .getResponseCode ();
0 commit comments