我正在使用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。我该怎么做呢?


当前回答

如果您想发出PUT请求,只需将所有事情都作为POST请求进行,但将_method => PUT添加到您的form-data参数中。

其他回答

不要给出任何标题。 将json数据放在.json文件中。 选择你的两个文件,一个是.txt文件,另一个是.json文件 用于您的请求参数键。

要在postman中发送图像和json数据,您只需要遵循以下步骤。

把你的方法交给邮递员 转到正文部分并单击form-data 从下拉列表中提供字段名称选择文件,如下所示 您还可以提供其他字段。 现在只需在控制器中编写图像存储代码,如下所示。

邮差:

我的控制器:

public function sendImage(Request $request)
{
    $image=new ImgUpload;  
    if($request->hasfile('image'))  
    {  
        $file=$request->file('image');  
        $extension=$file->getClientOriginalExtension();  
        $filename=time().'.'.$extension;  
        $file->move('public/upload/userimg/',$filename);  
        $image->image=$filename;  
    }  
    else  
    {  
        return $request;  
        $image->image='';  
    }  
    $image->save();
    return response()->json(['response'=>['code'=>'200','message'=>'image uploaded successfull']]);
}

就是这些,希望对你有所帮助

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

如果你需要 上传文件在多个部分使用表单数据和发送json数据(Dto对象)在同一个POST请求

在控制器中获取JSON对象作为字符串,并通过添加这一行使其反序列化

ContactDto contactDto  = new ObjectMapper().readValue(yourJSONString, ContactDto.class);

后端部分

控制器中的Rest服务将混合@RequestPart和MultipartFile来服务这种Multipart + JSON请求。

@RequestMapping(value = "/executesampleservice", method = RequestMethod.POST,
consumes = {"multipart/form-data"})

@ResponseBody
public boolean yourEndpointMethod(
    @RequestPart("properties") @Valid ConnectionProperties properties,
    @RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
return projectService.executeSampleService(properties, file);
}

前端:

formData = new FormData();

formData.append("file", document.forms[formName].file.files[0]);
formData.append('properties', new Blob([JSON.stringify({
            "name": "root",
            "password": "root"                    
        })], {
            type: "application/json"
        }));

如图所示(邮差请求):

点击查看邮差请求在表单数据为文件和json