gpt4 book ai didi

java - Spring REST上传和处理多个文件

转载 作者:行者123 更新时间:2023-12-02 11:07:32 25 4
gpt4 key购买 nike

我正在尝试学习如何从 Spring Consumer (RestTemplate) 上传和处理多个文件到 Spring Rest Controller 。

消费者

    public static List<FileVerification> consume_post_multiple_files_upload(List<Path> paths) {
final String subAddress = "post_multiple_files_upload";
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
final MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
int counter = 1;
for (Path path : paths) {
final FileSystemResource fileSystemResource = new FileSystemResource(path.toString());
map.add("file_" + (counter++), fileSystemResource);
}
final HttpEntity<MultiValueMap<String, Object>> requestFilesEntity = new HttpEntity<MultiValueMap<String, Object>>(map, headers);
final RestTemplate restTemplate = new RestTemplate();
final ResponseEntity<List> listResponseEntity = restTemplate.postForEntity(CONSUMER_UPLOAD_TO_SERVER_ADDRESS + subAddress, requestFilesEntity, List.class);
return listResponseEntity.getBody();
}

Controller

    @RequestMapping(value = "post_multiple_files_upload", method = RequestMethod.POST)
public ResponseEntity<List<FileVerification>> post_multipleFilesUpload(@RequestParam(name = "file_1") MultipartFile multipartFile) throws IOException, NoSuchAlgorithmException {
GeneralConfig.createServerFileDirectory();
final List<FileVerification> serverFileVerificationsList = new ArrayList<>();

FileUtils.getMultipartFileInformation("post_multiple_files_upload", multipartFile).forEach(System.out::println);
final Path serverFile = GeneralConfig.getServerFileDirectoryPath().resolve(multipartFile.getOriginalFilename());
FileUtils.createFileAndWriteData(serverFile, multipartFile.getBytes());
serverFileVerificationsList.add(Factory.getFileVerification(serverFile));


final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

GeneralConfig.deleteServerFileDirectory();
System.out.println();
return new ResponseEntity<List<FileVerification>>(serverFileVerificationsList, headers, HttpStatus.OK);
}

测试和实用代码

    @Test
public void test_consume_post_multiple_files_upload() throws IOException, NoSuchAlgorithmException {
final int fileCount = 10;
final List<Path> filesPaths = new ArrayList<>(fileCount);
final List<FileVerification> clientFileVerifications = new ArrayList<>(fileCount);
try {
for (int i = 0; i < fileCount; i++) {
final Path path = FileUtils.createRandomFileInClient();
filesPaths.add(path);
clientFileVerifications.add(Factory.getFileVerification(path));
}
final List<FileVerification> serverFileVerifications = consume_post_multiple_files_upload(filesPaths);
assertArrayEquals(clientFileVerifications.toArray(new FileVerification[0]), serverFileVerifications.toArray(new FileVerification[0]));
for (int i = 0; i < fileCount; i++) {
System.out.println("clientFileVerification#" + i + " = " + clientFileVerifications.get(i));
System.out.println("serverFileVerification#" + i + " = " + serverFileVerifications.get(i));
}
} finally {
for (Path filePath : filesPaths) {
Files.deleteIfExists(filePath);
}
}
}

-

import java.util.Arrays;

public class FileVerification {

private String fileName;
private byte[] digest_SHA256;

public FileVerification setFileName(String fileName) {
this.fileName = fileName;
return this;
}

public FileVerification setDigest_SHA256(byte[] digest_SHA256) {
this.digest_SHA256 = digest_SHA256;
return this;
}

public String getFileName() {
return this.fileName;
}

public byte[] getDigest_SHA256() {
return this.digest_SHA256;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FileVerification that = (FileVerification) o;
return this.fileName.equals(that.fileName) && Arrays.equals(this.digest_SHA256, that.digest_SHA256);
}

@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
for (byte b : digest_SHA256) {
builder.append(String.format("%02X", b));
}
final String digest_SHA256_representation = builder.toString();
return "FileVerification{fileName='" + this.fileName + '\'' + ", digest_SHA256='" + digest_SHA256_representation + '\'' + '}';
}
}

-

    public static FileVerification getFileVerification(Path path) throws NoSuchAlgorithmException, IOException {
final MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
if (Files.notExists(path)) throw new IllegalArgumentException("file doesn't exist");
final byte[] digest = messageDigest.digest(Files.readAllBytes(path));
final String name = path.getFileName().toString();
return new FileVerification().setFileName(name).setDigest_SHA256(digest);
}

问题应该对使用者和/或 Controller 进行哪些更改以允许将多个文件上传到服务器?

更新:问题已解决,我会尽快发布答案。

最佳答案

Controller.post_multipleFilesUpload 更改为:

public ResponseEntity<List<FileVerification>> post_multipleFilesUpload(@RequestParam(name = "file") List<MultipartFile> multipartFiles) throws IOException, NoSuchAlgorithmException {

相应地,更新 consume_post_multiple_files_upload 以发送具有相同 key (file) 的所有文件,方法是将代码修改为 map.add("file", filesystemResourceList),而不是将每个资源添加到其自己单独的 key 下。

关于java - Spring REST上传和处理多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50855563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com