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


当前回答

缺失的视觉指南

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

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

其他回答

通过邮递员上传文件以及传递一些输入数据的步骤在下面的博客以及截图中进行了很好的讨论。在本博客中,api代码是用node js编写的。你可以再看一遍,看得更清楚些。

https://jksnu.blogspot.com/2021/09/how-to-create-post-request-with.html

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

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

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

邮差:

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

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

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

在[SHOW COLUMNS]中选择[Content Type],然后将“application/json”的Content - Type设置为json text参数。