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


当前回答

像这样:

Body -> form-data -> select file

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

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

其他回答

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

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

在postman中,将方法类型设置为POST。

然后选择 Body -> form-data ->输入参数名称(根据您的代码文件)

在Key字段的右侧,当鼠标悬停在它上面时,有一个下拉菜单可以在文本/文件之间进行选择。选择文件,然后“选择文件”按钮将出现在值字段。

对于其他基于“文本”的参数,你可以像邮递员一样发送它。只需输入参数名称,并从右边的下拉菜单中选择“文本”,并为它输入任何值,点击发送按钮。你的控制器方法应该被调用。

通过邮递员上传文件以及传递一些输入数据的步骤在下面的博客以及截图中进行了很好的讨论。在本博客中,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']]);
}

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

也许你可以这样做: