Spring rest:上传文件

时间:2017-04-24 15:10:49

标签: java file-upload spring-boot multipartform-data spring-rest

我正在使用此代码使用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;
    }

}

现在我想通过使用弹簧靴和弹簧支架来做同样的事情。 我搜索了很多关于如何在春季休息时使用@FormParam@PartType但我没有找到任何东西。

那么如何使用这个类来上传我的文件呢?春季休息时@PartType@FormParam相当于什么?

1 个答案:

答案 0 :(得分:1)

你想在春季休息时编写一个文件上传代码,这很简单你只需要使用多部分文件对象,如下面的代码所示。

 @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
***/

}