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

我需要在邮递员和文件中传递会话id。我该怎么做呢?


当前回答

在弹簧架侧使用以下代码:

@PostMapping(value = Constant.API_INITIAL + "/uploadFile")
    public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file,String jsonFileVo) {
        FileUploadVo fileUploadVo = null;
        try {
            fileUploadVo = new ObjectMapper().readValue(jsonFileVo, FileUploadVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

其他回答

如果有人需要:

Body -> form-data

将字段名添加为数组

像这样:

Body -> form-data -> select file

你必须写“file”而不是“name”

你也可以从Body ->原始字段发送JSON数据。(只需粘贴JSON字符串)

您可以同时发送Image和可选/必选参数。

在postman中,有Params标签。

如果有人想以form-data格式发送json数据,只需要像这样声明变量

邮差:

如你所见,description参数将是基本的json格式,结果是:

{ description: { spanish: 'hola', english: 'hello' } }

如果你想把Id和File放在一个对象中,你可以把请求对象添加到一个标准的方法中,然后在Postman中设置Body为form-data,并在键前加上请求对象的名称。如请求。SessionId和request.File。