android图片上传 android实现图片上传功能(springMvc)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

android图片上传 android实现图片上传功能(springMvc)

  2021-03-24 我要评论
想了解android实现图片上传功能(springMvc)的相关内容吗,在本文为您仔细讲解android图片上传 的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,图片上传,springMvc,下面大家一起来学习吧。

Android端:

String fileName = tvFilename.getText().toString();
 RequestBody description =
  RequestBody.create(
   okhttp3.MultipartBody.FORM, fileName);

 File fileImage = new File(saveFileName);
 RequestBody requestBody1 =
  RequestBody.create(MediaType.parse("multipart/form-data"), fileImage);
 MultipartBody.Part body =
  MultipartBody.Part.createFormData("bannerImage", fileImage.getName(), requestBody1);


 RestClient.api().addLive(description, body)
 .enqueue(new retrofit2.Callback<ResponseInfo>() {
  @Override
  public void onResponse(retrofit2.Call<ResponseInfo> call, retrofit2.Response<ResponseInfo> response) {
  "处理成功"
  }

  @Override
  public void onFailure(retrofit2.Call<ResponseInfo> call, Throwable t) {
  "处理失败"
  }
 });

@Multipart
 @POST("add/live")
 Call<ResponseInfo> addLive(
  @Part("fileName")RequestBody fileName,//其他字段
  @Part MultipartBody.Part bannerImage);//图片

服务器端:

@RequestMapping(value = "/add/live", method = RequestMethod.POST)
 @ResponseBody
 public ResponseInfo addLive(
  @RequestParam(value = "fileName") String fileName,
  @RequestParam(value = "bannerImage") MultipartFile bannerImage
 )throws Exception {
 String logoPath = "/logo/score/";
 try {
  File file = new File(imagePath + logoPath);
  FileUtils.writeByteArrayToFile(file, bannerImage.getBytes());

 } catch (Exception e) {
  e.printStackTrace();
 }
 return new ResponseInfo(T1TConstants.SUCCESS, "成功");
 }

参考:android调用原生图片裁剪后图片尺寸缩放的解决方法

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们