Es tritt der Fehler auf, dass die aktuelle Anfrage keine mehrteilige Anfrage ist.
2024-12-21T15:33:28.898+05:30 ERROR 3312 --- [fileupload] [nio-8080- exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service()
for Servlet [dispatcherServlet] im Kontext mit Pfad [] hat die Ausnahme [Anfrageverarbeitung fehlgeschlagen: org.springframework.web.multipart.MultipartException: Aktuelle Anfrage ist keine mehrteilige Anfrage] mit Grundursache ausgelöst
org .springframework.web.multipart.MultipartException: Aktuelle Anfrage ist keine mehrteilige Anfrage
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValueInternal(RequestParamMethodArgumentResolver.java:213) ~[spring-web-6.2.0.jar:6.2.0]
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:196) ~[spring-web-6.2.0.jar:6.2.0]
Hier Code eingeben
[das ist der Fehler in Postman]
{
"timestamp": "2024-12-21T10:03:29.001+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/api/s3/upload"
}
Ich habe s3Client in der S3ClientConfig-Klasse konfiguriert. mit Anmeldeinformationen, die in der Datei application.yml gespeichert sind.
@Configuration
public class S3ClientConfig {
@Value("${aws.s3.region}")
private String region;
@Value("${aws.s3.accessKey}")
private String accessKey;
@Value("${aws.s3.secretKey}")
private String secretAccessKey;
@Bean
S3Client s3Client() {
S3Client s3client = S3Client.builder()
.region(Region.of(region))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretAccessKey)))
.forcePathStyle(true)
.build();
return s3client;
}
}
Hier sieht meine S3Service-Klasse aus
package com.dimuthu.fileupload.service;
import java.io.File;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import lombok.extern.slf4j.Slf4j;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
@Service
@Slf4j
public class S3Service {
@Autowired
private S3Client s3Client;
public PutObjectResponse putObject(String bucketName, String key, MultipartFile file) throws IOException {
log.info("Uploading file: {} to bucket: {} with key: {}", file.getOriginalFilename(), bucketName, key);
File tempFile = File.createTempFile("upload-", file.getOriginalFilename());
try {
file.transferTo(tempFile);
PutObjectResponse response = s3Client.putObject(req -> req.bucket(bucketName).key(key),
RequestBody.fromFile(tempFile.toPath()));
log.info("File uploaded successfully with ETag: {}", response.eTag());
return response;
} catch (Exception e) {
log.error("Error uploading file: {}", e.getMessage(), e);
throw e;
} finally {
tempFile.delete();
}
}
}
und das ist der My-Rest-Controller. Ich füge hinzu, verbraucht mediatype.ALL_VALUE
@RestController
@CrossOrigin(origins = "*")
public class UploadControlller {
@Value("${aws.s3.bucketName}")
private String bucketName;
@Autowired
private S3Service s3Service;
@PostMapping(value = "/api/s3/upload", consumes = MediaType.ALL_VALUE)
public ResponseEntity uploadFile(@RequestParam("uploadfile") MultipartFile file){
try {
String key = "uploads/" + file.getOriginalFilename();
s3Service.putObject(bucketName, key, file);
return ResponseEntity.ok("File uploaded successfully!");
} catch (IOException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("" + e.getStackTrace());
}
}
}
So laden Sie mehrteilige Dateien mit Spring Boot in den AWS S3-Bucket hoch ⇐ Java
-
- Similar Topics
- Replies
- Views
- Last post