@@ -95,8 +95,9 @@ private Boolean uploadImage(String uploadDestinationUrl, CreateUploadDestination
9595 try (InputStream is = new FileInputStream (imageFilePath )) {
9696 byte [] fileBytes = is .readAllBytes ();
9797
98- okhttp3 .MultipartBody .Builder formBuilder = new okhttp3 .MultipartBody .Builder ()
99- .setType (okhttp3 .MultipartBody .FORM );
98+ // Create multipart form data manually
99+ String boundary = "----WebKitFormBoundary" + System .currentTimeMillis ();
100+ StringBuilder formData = new StringBuilder ();
100101
101102 // Extract query parameters from URL and add as form fields
102103 java .net .URL url = new java .net .URL (uploadDestinationUrl );
@@ -108,30 +109,43 @@ private Boolean uploadImage(String uploadDestinationUrl, CreateUploadDestination
108109 if (keyValue .length == 2 ) {
109110 String key = java .net .URLDecoder .decode (keyValue [0 ], "UTF-8" );
110111 String value = java .net .URLDecoder .decode (keyValue [1 ], "UTF-8" );
111- formBuilder .addFormDataPart (key , value );
112+ formData .append ("--" ).append (boundary ).append ("\r \n " );
113+ formData .append ("Content-Disposition: form-data; name=\" " ).append (key ).append ("\" \r \n \r \n " );
114+ formData .append (value ).append ("\r \n " );
112115 }
113116 }
114117 }
115118
116- // Add file as form field
117- okhttp3 .RequestBody fileBody = okhttp3 .RequestBody .create (fileBytes , okhttp3 .MediaType .get (contentType ));
118- formBuilder .addFormDataPart ("File" , "test_image.jpg" , fileBody );
119+ // Add file field header
120+ formData .append ("--" ).append (boundary ).append ("\r \n " );
121+ formData .append ("Content-Disposition: form-data; name=\" File\" ; filename=\" test_image.jpg\" \r \n " );
122+ formData .append ("Content-Type: " ).append (contentType ).append ("\r \n \r \n " );
119123
120- okhttp3 .RequestBody requestBody = formBuilder .build ();
124+ // Combine form data with file bytes
125+ byte [] formDataBytes = formData .toString ().getBytes ("UTF-8" );
126+ String endBoundary = "\r \n --" + boundary + "--\r \n " ;
127+ byte [] endBoundaryBytes = endBoundary .getBytes ("UTF-8" );
128+
129+ byte [] requestBody = new byte [formDataBytes .length + fileBytes .length + endBoundaryBytes .length ];
130+ System .arraycopy (formDataBytes , 0 , requestBody , 0 , formDataBytes .length );
131+ System .arraycopy (fileBytes , 0 , requestBody , formDataBytes .length , fileBytes .length );
132+ System .arraycopy (endBoundaryBytes , 0 , requestBody , formDataBytes .length + fileBytes .length , endBoundaryBytes .length );
121133
122134 // Use base URL without query parameters
123135 String baseUrl = uploadDestinationUrl .split ("\\ ?" )[0 ];
124- okhttp3 .Request request = new okhttp3 .Request .Builder ()
125- .url (baseUrl )
126- .post (requestBody )
136+
137+ java .net .http .HttpClient client = java .net .http .HttpClient .newHttpClient ();
138+ java .net .http .HttpRequest request = java .net .http .HttpRequest .newBuilder ()
139+ .uri (java .net .URI .create (baseUrl ))
140+ .POST (java .net .http .HttpRequest .BodyPublishers .ofByteArray (requestBody ))
141+ .header ("Content-Type" , "multipart/form-data; boundary=" + boundary )
127142 .build ();
128-
129- okhttp3 .OkHttpClient client = new okhttp3 .OkHttpClient ();
130- try (okhttp3 .Response response = client .newCall (request ).execute ()) {
131- System .out .println ("Upload response code: " + response .code ());
132- return response .isSuccessful ();
133- }
134- } catch (IOException e ) {
143+
144+ java .net .http .HttpResponse <String > response = client .send (request ,
145+ java .net .http .HttpResponse .BodyHandlers .ofString ());
146+ System .out .println ("Upload response code: " + response .statusCode ());
147+ return response .statusCode () >= 200 && response .statusCode () < 300 ;
148+ } catch (IOException | InterruptedException e ) {
135149 throw new RuntimeException ("Failed to upload image" , e );
136150 }
137151 }
0 commit comments