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


当前回答

如果您使用cookie来保持会话,则可以使用拦截器将cookie从浏览器共享给邮递员。

也可以上传一个文件,你可以使用form-data标签下的正文标签邮递员,在其中,你可以提供数据在键值格式和每个键,你可以选择值文本/文件的类型。当你选择文件类型时,出现上传文件的选项。

其他回答

缺失的视觉指南

您必须首先找到几乎看不见的“文件”的浅灰色下拉菜单,这是解锁“选择文件”按钮的魔法键。

在你选择POST后,然后选择Body->form-data,然后找到File下拉菜单,然后选择File,然后才会神奇地出现choose Files按钮:

发送包含json数据文件的多部分数据的方法如下,我们需要在邮递员body选项卡中设置相应json关键字段的content-type为'application/json',如下所示:

后端部分

控制器中的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

要在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']]);
}

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

我需要传递两个:一个文件和一个整数。我是这样做的:

需要传递一个文件来上传: 按照Sumit的回答做了。 请求类型:POST Body -> form-data 在KEY标题下,输入变量的名称(后端代码中的'file')。 在后端: File = request.files[' File '] 在“文件”旁边,有一个下拉框,允许你在“文件”或“文本”之间进行选择。选择“File”,在VALUE标题下,出现了“Select files”。点击这个,打开一个窗口选择文件。

2. 需要传递一个整数:

去:

参数个数

在KEY下输入变量名(例如:id),在value下输入变量值(例如:1)

在后端:

id = request.args.get('id')

成功了!