java - Spring 休息 : upload file

标签 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;
    }

}

现在我想通过使用 Spring Boot 和 Spring 支架来做同样的事情。 我搜索了很多有关如何在 Spring Rest 中使用 @FormParam@PartType 的信息,但没有找到任何内容。

那么我如何使用这个类来上传我的文件呢? Spring Rest 中的 @PartType@FormParam 相当于什么?

最佳答案

您想在 Spring Rest 中编写文件上传代码,这很简单,您只需要使用多部分文件对象,如以下代码所示。

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

}

关于java - Spring 休息 : upload file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43591736/

相关文章:

java - 模拟 oracle STRUCT 类以实现 junit 覆盖

java - Axis2 用户认证

android - 在android中使用亚马逊使用进度条上传多个文件

java - 实现 JWT 身份验证服务

java - 在 Spring Boot 2 中配置 micrometer-registry-statsd

java - 如何在子项目之间共享 application.conf 文件?

java - java中分割字符串后如何重新排列?

php - 使用 cURL 上传多个文件

php-chmod() : No such file or directory

java - 避免多次检查 != null