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));
}