首页 > 解决方案 > 在 python3 中上传文件异常使用“请求”模块

问题描述

我尝试运行以下 python 代码,但服务器 错误消息出现错误:org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部分“文件”不存在\n\tat org.springframework.web。 method.annotation.RequestParamMethodArgumentResolver.handleMissingValuexxx

下面的python代码

    header = {'Content-Type': 'multipart/form-data;'}
    with open(filepath, 'rb') as ff:
        files = { 'file': ff }
        r = requests.post( url=url, headers=header, files=files)
        print(r.text)

下面是java的服务器代码

public MessageVO uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            BackResultVO<UploadfileVo> mes = fileService.uploadCommonFile(file);
            MessageVO<UploadfileVo> uploadfileVoMessageVO = MessageVO.back(mes.getCode(),mes.getMsg());
            MessageVO<String> loadfile = this.loadfile(mes.getData().getFilePath());
            mes.getData().setFileDownUrl(loadfile.getData());
            uploadfileVoMessageVO.setError(mes.getError());
            uploadfileVoMessageVO.setData(mes.getData());
            return uploadfileVoMessageVO;
        } catch (Exception e) {
            e.printStackTrace();
            return MessageVO.back(MessageVO.Code.EXCEPTION, "failed"+e.getMessage());
        }
    }

请帮忙

标签: javapython

解决方案


推荐阅读