Wie akzeptiere ich eine Reihe von Multipartfile in einer Feder -Boot -Methode?Java

Java-Forum
Anonymous
 Wie akzeptiere ich eine Reihe von Multipartfile in einer Feder -Boot -Methode?

Post by Anonymous »

Ich arbeite an einer Spring -Boot -Anwendung, in der ich Datei -Uploads verarbeiten muss. Ich habe eine Standardmethode, die derzeit eine einzelne MultipartFile akzeptiert, und ich möchte sie ändern, um ein Array von MultipartFile zu akzeptieren. Hier ist mein aktueller Code: < /p>
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

// Override this method
default ResponseEntity uploadFiles(String msName, MultipartFile files) {
return new ResponseEntity(HttpStatus.NOT_IMPLEMENTED);
}
< /code>
Ich möchte diese Methode ändern, um ein Array von MultipartFile wie folgt zu akzeptieren: < /p>
default ResponseEntity uploadFiles(String msName, MultipartFile[] files) {
// Implementation to handle multiple files
}
< /code>
Swagger-Datei, mit der Code im Zielordner generiert wird.openapi: 3.0.1
info:
title: Upload API
description: API to upload multiple files with an associated microservice name.
version: 1.0.0
paths:
/upload:
post:
summary: Upload multiple files with a microservice name
operationId: uploadFiles
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
ms_name:
type: string
description: Name of the microservice
files:
type: array
items:
type: string
format: binary
description: Array of files to upload
responses:
"200":
description: Files uploaded successfully
"400":
description: Bad request
"500":
description: Internal server error
< /code>
pom.xml

4.0.0


com.builder.ms
parent
0.0.1-SNAPSHOT
../parent


com.builder.api
api
jar


17
${project.basedir}/arc/main/resources
${resource.dir}/swagger/external-api
com.vishal.nikita.ms.productorder.resources.interfaces
com.vishal.nikita.ms.productorder.resources.models
product-management
${project.build.directory}/asciidoc/generated
${project.build.directory}/asciidoc/html
${project.basedir}/src/main/resources/docs





jakarta.validation
jakarta.validation-api
3.0.2


jakarta.annotation
jakarta.annotation-api
2.1.1


jakarta.inject
jakarta.inject-api
2.0.1
provided


jakarta.servlet
jakarta.servlet-api
5.0.0
provided


jakarta.ws.rs
jakarta.ws.rs-api
3.1.0
provided




org.springframework
spring-context


org.springframework
spring-web


org.springframework.boot
spring-boot-autoconfigure


org.springframework.boot
spring-boot-starter-actuator


org.apache.tomcat.embed
tomcat-embed-core




junit
junit


org.junit.jupiter
junit-jupiter


org.springframework.boot
spring-boot-test




org.springdoc
springdoc-openapi-starter-webmvc-ui
2.3.0


org.openapitools
jackson-databind-nullable
0.2.6


org.yaml
snakeyaml
2.0






${project.build.directory}/generated-sources/src/main/resources


${resource.dir}





org.openapitools
openapi-generator-maven-plugin
7.1.0


generate-api

generate


${project.basedir}/src/main/resources/swaggerapi.yaml
spring
${project.build.directory}/generated-sources/api
com.builder.api
com.builder.model
com.builder.client

true
true
true
true









< /code>
Was ich ausprobiert habe: < /p>

Ich habe die Methode -Signatur aktualisiert, um MultipartFile [] Dateien zu akzeptieren. /> Hier ist mein aktualisierter Code: < /p>
default ResponseEntity uploadFiles(String msName, MultipartFile[] files) {
if (files == null || files.length == 0) {
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}

for (MultipartFile file : files) {
if (!file.isEmpty()) {
// Process each file
}
}

return new ResponseEntity(HttpStatus.OK);
}
< /code>
Meine Fragen: < /p>

Ist dies der richtige Weg, um ein Array von Multipartfile in Spring -Boot zu verarbeiten? Mehrere Datei -Uploads? (z. B. große Dateien, ungültige Dateitypen). Obwohl ich Änderungen im Zielverzeichnis (Uploadapi) vorgenommen habe, werden sie immer dann entfernt, wenn ich eine Reinigungsanlagen ausführe, da OpenAPI -Tools die Schnittstelle regeneriert. Lokal kann ich es ändern, aber wenn Jenkins in Echtzeit bereitgestellt wird, erstellt Jenkins ein neues Zielverzeichnis, wodurch es fehlschlägt. Ich brauche eine Lösung>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post