我使用的是Spring MVC,这是我的方法:

/**
* Upload single file using Spring Controller.
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<GenericResponseVO<? extends IServiceVO>> uploadFileHandler(
            @RequestParam("name") String name,
            @RequestParam("file") MultipartFile file,
            HttpServletRequest request,
            HttpServletResponse response) {

    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            String rootPath = System.getProperty("catalina.home");
            File dir = new File(rootPath + File.separator + "tmpFiles");
            if (!dir.exists()) {
                dir.mkdirs();
            }

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath() + File.separator + name);
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();

            System.out.println("Server File Location=" + serverFile.getAbsolutePath());

            return null;
        } catch (Exception e) {
            return null;
        }
    }
}


推荐答案

在Postman中,将方法类型设置为POST.

Then select Body -> form-data -> Enter your parameter name (file according to your code)

在右侧的值列旁边,将有dropdown "text, file"个, Select File. Select 您的图像文件并将其发布.

对于"text" based parameters美元的睡觉,你可以像往常一样寄给postman .只需输入参数名称,然后从右侧下拉菜单中 Select "文本",然后为其输入任何值,然后单击"发送"按钮即可.应该调用您的控制器方法.

Java相关问答推荐

我想知道为什么我的控制器给出嵌套响应

Gmail Javi API批量处理太多请求

javafx getHostServices(). showDocument()调出Chrome而不是默认浏览器(Linux)

ittext pdf延迟签名,签名无效

RxJava PublishSubject缓冲区元素超时

Intellij显示项目语言级别最高为12,尽管有java版本17 SDK

springboot start loge change

使用联接和分页的SpringBoot Spring数据JPA

如何确定springboot在将json字段转换为Dto时如何处理它?

使用Spring和ActiveMQ的侦听器方法引发属性名称不能重复为空警告

将java.util.Date转换为OffsetDateTime

在JDK 1.8源代码中,为什么使用A-B 0来确定哪个更大,而不是A B?

声明MessageChannel Bean的首选方式

try 从REST API返回对象列表时出错

在Frege中,我如何将一个字符串安全地转换为一个可能的Int?

有没有办法知道在合并中执行了什么操作?

为什么mvn编译生命周期阶段不只是编译已更改的java文件?

为什么相同的数据条码在视觉上看起来不同?

OAuth:登录后无法查看Google邮箱地址

由于版本不匹配,从Java 8迁移到Java 17和Spring 6 JUnit4失败