Verwenden Sie Spring WebClient zum Hochladen einer Datei in JavaJava

Java-Forum
Anonymous
 Verwenden Sie Spring WebClient zum Hochladen einer Datei in Java

Post by Anonymous »

Ich versuche, von normalen HTTPPOST -Methoden zu Spring WebClient zu migrieren, und ich habe eine API, die zwei Dateien (ein JSON und ein PDF) zum Upload akzeptiert.String jsonData ="";
ByteArrayOutputStream file;

MultipartBodyBuilder builder = new MultipartBodyBuilder();
String header1 = String.format("form-data; name=%s; filename=%s", "attach", "file.pdf");
String header2 = String.format("form-data; name=%s; filename=%s", "jsonfile", "jsonfile.json");

// This line is causing the problem, Am I making a mistake here?
builder.part("attach", file.toByteArray()).header("Content-Disposition", header1);
// This line works fine.
builder.part("jsonfile", jsonData.getBytes()).header("Content-Disposition", header2);

WebClient webClient = WebClient.create("a url");

byte[] fileContent = null;
try {
fileContent = webClient.post()
.body(BodyInserters.fromMultipartData(builder.build()))
.retrieve()
.onStatus(HttpStatus::isError, res -> handleError(res))
.bodyToMono(byte[].class)
.block();
} catch (Exception e) {
return null;
}
< /code>

Wenn ich die PDF -Datei in der Anforderung nicht sende, funktioniert sie nur mit der JSON -Datei. Datei. < /p>

Wenn mir jemand sagen könnte, was hier möglicherweise falsch sein könnte. < /p>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post