当前位置: 代码网 > 服务器>网络>https > 使用Okhttp实现上传文件+参数请求接口form-data

使用Okhttp实现上传文件+参数请求接口form-data

2024年10月23日 https 我要评论
okhttp实现上传文件+参数请求接口form-data有时候需要对接一些接口,而且接口传参不仅需要各种类型的参数,甚至还要上传文件,所以遇到挺多坑,用postman的生成代码也不好用,于是就有了这篇

okhttp实现上传文件+参数请求接口form-data

有时候需要对接一些接口,而且接口传参不仅需要各种类型的参数,甚至还要上传文件,所以遇到挺多坑,用postman的生成代码也不好用,于是就有了这篇文章。

话不多说,我们直接上代码

首先是service层

    /**
     * 写注释是个好习惯
     *
     * @param mfile
     * @param accountindex
     * @param exporttype
     * @param clear
     * @param email
     * @param dimensions
     * @return
     * @throws ioexception
     */
    public string upload(multipartfile mfile, integer accountindex, string exporttype,
                         boolean clear, string email, string dimensions) throws ioexception {
        // 这里是multipartfile转file的过程
        file file = new file(objects.requirenonnull(mfile.getoriginalfilename()));
        fileutils.copyinputstreamtofile(mfile.getinputstream(), file);
        // url接口路径
        string url = "http://localhost:8080/upload";
        // file是要上传的文件 file()  这边我上传的是excel,其他类型可以自己改这个parse
        requestbody filebody = requestbody.create(mediatype.parse("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), file);//这边是把file写进来,也有写路径的,但我这边是写file文件,parse不行的话可以直接改这个"multipart/form-data"
        // 创建okhttpclient实例,设置超时时间
        okhttpclient okhttpclient = new okhttpclient.builder()
                .connecttimeout(60l, timeunit.seconds)
                .writetimeout(60l, timeunit.seconds)
                .readtimeout(60l, timeunit.seconds)
                .build();
        // 不仅可以支持传文件,还可以在传文件的同时,传参数
        multipartbody requestbody = new multipartbody.builder()
                .settype(multipartbody.form) // 设置传参为form-data格式
                .addformdatapart("account_index", string.valueof(accountindex))
                .addformdatapart("export_type", exporttype)
                .addformdatapart("clear", string.valueof(clear))
                .addformdatapart("email", email)
                .addformdatapart("dimensions", dimensions)
                .addformdatapart("file", file.getname(), filebody) // 中间参数为文件名
                .build();

        // 构建request请求体,有需要传请求头自己加
        request request = new request.builder()
                .url(url)
                .post(requestbody)
                .build();
        response response = null;
        string result = "";
        try {
            // 发送请求
            response = okhttpclient.newcall(request).execute();
            result = response.body().string();
            log.info(url + "发送请求结果:" + result);
            if (!response.issuccessful()) {
                log.info("请求失败");
                return "请求失败";
            }
            response.body().close();
        } catch (ioexception e) {
            log.error(e.getmessage());
        }
        // 会在本地产生临时文件,用完后需要删除
        if (file.exists()) {
            file.delete();
        }
        return result;
    }

然后controller层的传参需要用@requestparam或者直接一个请求的实体类

如果使用实体类,千万不要加@requestbody,不然结合上传文件会失效,上传文件使用

@requestpart("file") multipartfile file进行传参

(@requestpart("file") multipartfile file,
@requestparam("accountindex") integer accountindex,
@requestparam("exporttype") string exporttype,
@requestparam(value = "clear", required = false) boolean clear,
@requestparam("email") string email,
@requestparam(value = "dimensions", required = false) string dimensions)

示例如上,或者

(@requestpart("file") multipartfile file, requestvo req)

请求成功,问题解决。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com