C# 上传文件到 JAVA 接口

1、c#,带有jwt和请求头

/// <summary>
/// 发送文件
/// </summary>
/// <param name="url">链接</param>
/// <param name="param">参数,?xx=</param>
/// <param name="filepath">文件路径</param>
/// <param name="body">内容</param>
/// <param name="timeout">超时时间</param>
/// <returns></returns>
public ReturnModel PostSendFile(string url, string param, string filepath, string body = "{}", double timeout = 30.0)
{
    ReturnModel returnModel = new ReturnModel()
    {
        Status = ReturnStatus.Success
    };

    try
    {
        using (MultipartFormDataContent content = new MultipartFormDataContent())
        {
            //文件内容
            content.Add(new ByteArrayContent(File.ReadAllBytes(filepath)),
                "file",
                Path.GetFileName(filepath));
            //请求体
            content.Add(new StringContent(body, Encoding.UTF8),
                "body");

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(JWTTool.CreateJWTForPos());
                httpClient.DefaultRequestHeaders.Add("CustomerAuthorization", "true");
                httpClient.DefaultRequestHeaders.Add("CustomerAuthorizationName", "JWT Pos");
                httpClient.Timeout = TimeSpan.FromSeconds(timeout);

                returnModel.MessageOrString = httpClient.PostAsync(url + param, content).Result.Content.ReadAsStringAsync().Result;
            }
        }
    }
    catch (Exception ex)
    {
        returnModel.Status = ReturnStatus.Error;
        returnModel.MessageOrString = ex.Message;
    }

    return returnModel;
}

2、java接收并转换为实体

public AjaxResult saveEquipmntLocalFile(@RequestParam("body") String body,
                                            @RequestParam("file") MultipartFile file){
        log.info("保存本地文件方法saveEquipmntLocalFile:{}", body);

        PosEquipmentRequest posEquipmentRequest = JSON.parseObject(body, PosEquipmentRequest.class);

        return AjaxResult.success(JSON.toJSON(posEquipmentRequest));
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值