上传带有其他信息的多个文件(Spring RestController) [英] Upload multiple files with additional information (Spring RestController)

查看:252
本文介绍了上传带有其他信息的多个文件(Spring RestController)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多问题

I know that there are plenty of questions covering the topic, but I cannot figure out how to achieve the following requirement.

我想上传一个文件列表,每个包含一些额外的信息. 在Java世界中,这意味着:

I would like to upload a list of files, each one containing some extra information. In the java world this would mean the following:

@NoArgsConstructor
@Getter
public class SkillsVerificationData {

   String type; // this information is related to the file 
   MultipartFile file;
}

问题1: RestController是否有可能使用包装对象实现这种映射? (请参阅所引用问题的第一个答案-@ModelAttribute)

Question 1: Is this possible for a RestController to achieve such a mapping using a wrapper object? (See first answer of the referenced question- @ModelAttribute)

问题2: 使用上面提到的问题中回答的以下控制器方法

Question 2: Using the following controller method answered in the question referenced above

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = { "multipart/form-data" })
public void upload(@RequestPart("type") @Valid String type,
        @RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
}

我假设它适用于单个文件.如何定义/描述请求部分以实现上传List<SkillsVerificationData>SkillsVerificationData []?

I assume that it applies for a single file. How should the request parts be defined/described to achieve uploading a List<SkillsVerificationData> or SkillsVerificationData[] ?

请注意,客户端使用FormData发送信息.

Note that the client sends the information using FormData.

提前谢谢!

推荐答案

如果我正确理解了您的问题,则要上传文件列表以及每个文件的特定信息.我可以将其与一个示例关联起来,以使其更加清晰.用户希望上载候选人的技术资料或简历列表以及候选人信息.如果是这种情况,您可以创建一个MultiPart文件数组和一个json结构,该结构包含有关每个唯一fileName的信息,并在json结构中包含唯一的id和候选信息.如果这是必需的,则可以在下面的代码中进行参考.

If i understand your question correctly, you want to upload a list of files along with certain information specific to each file. I can correlate with an example to bring more clarity. A user wants to upload a list of candidates' technical profiles or resumes along with candidate information. If that is the case, you can create an array of MultiPart file and a json structure which contains information about each unique fileName, with unique id and candidate information in a json structure. If that is the requirement, you can refer below the code.

@PostMapping(
      value = "/multiUpload",
      consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
      produces = MediaType.TEXT_PLAIN_VALUE)
  public ResponseEntity<?> uploadingMultipleFiles(
      @RequestParam("files") MultipartFile[] uploadingFiles, @RequestPart(value = "emps", required = true) String empJsonTxt) {
      System.out.println("EMP as Json String = " + empJsonTxt);
      //Process the empJsonTxt
    ObjectMapper objectMapper = new ObjectMapper();
    try {
      Employee emp = objectMapper.readValue(empJsonTxt, Employee.class);
      System.out.println("Now emp = " + emp);
    } catch (IOException e) {
      e.printStackTrace();
    }
    for (MultipartFile uploadedFile : uploadingFiles) {
      System.out.println("Uploaded File Name = " + uploadedFile.getOriginalFilename());
      File file = new File("E:/sure-delete/" + uploadedFile.getOriginalFilename());

    //Upload functionality
      try {
        uploadedFile.transferTo(file);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return ResponseEntity.ok("All Files uploaded successfully ...");
  }

在这种情况下,empJsonTxt是一串json内容,其中包含有关员工及其简历/个人资料的所有必需信息.分段文件将仅用于上传.但是,您可以外推此部分以使其适合您的要求.这是可行的,也许还有更多更好的方法.

In this case empJsonTxt is a string of json contents which contains all the required information about the employees and their resume/profile. Multipart files will be used only for uploading. However, you can extrapolate this part to make suitable to your requirement. This is doable, there may be many more good approaches also.

这篇关于上传带有其他信息的多个文件(Spring RestController)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆