Spring 休息:上传文件

Spring rest: upload file

我正在使用此代码通过 resteasy 在我的 java 应用程序中上传文件,它运行良好。

import javax.ws.rs.FormParam;
import org.jboss.resteasy.annotations.providers.multipart.PartType;

public class FileUploadForm {

    public FileUploadForm() {
    }

    private byte[] data;

    public byte[] getData() {
        return data;
    }

    @FormParam("uploadedFile")
    @PartType("application/octet-stream")
    public void setData(byte[] data) {
        this.data = data;
    }

}

现在我想通过 spring 引导和 spring 休息来做同样的事情。 我搜索了很多关于如何在 spring 中使用 @FormParam@PartType 的信息,但我没有找到任何东西。

那么我如何使用这个 class 来上传我的文件呢? spring 中 @PartType@FormParam 的等效项是什么?

您想在 spring 中编写用于文件上传的代码,剩下的很简单,您只需要使用 multipart 文件对象,如下面的代码所示。

 @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
    public URL uploadFileHandler(@RequestParam("name") String name,
                                 @RequestParam("file") MultipartFile file) throws IOException {

/***Here you will get following parameters***/
 System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
        System.out.println("file.getContentType()" + file.getContentType());
        System.out.println("file.getInputStream() " + file.getInputStream());
        System.out.println("file.toString() " + file.toString());
        System.out.println("file.getSize() " + file.getSize());
        System.out.println("name " + name);
        System.out.println("file.getBytes() " + file.getBytes());
        System.out.println("file.hashCode() " + file.hashCode());
        System.out.println("file.getClass() " + file.getClass());
        System.out.println("file.isEmpty() " + file.isEmpty());
/***
Bussiness logic
***/

}