我正在使用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标签下的正文标签邮递员,在其中,你可以提供数据在键值格式和每个键,你可以选择值文本/文件的类型。当你选择文件类型时,出现上传文件的选项。

其他回答

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

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

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

在看到所有的答案后,我感到困惑,我找不到任何合适的截图来带来内容类型列。过了一段时间,我自己找到了它。希望这能帮助到像我这样的人。

以下是步骤:

点击邮递员的红色标记区域。 现在检查绿色标记的选项(Content Type)。 现在改变搜索内容类型,在黄色标记的区域。

在我的例子中:

Invoice_id_ls (key)包含json数据。 文档包含文件数据。 Placed_amount包含普通文本字符串。

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

首先,设置post in方法和填充链接API

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