Android retrofit2 图片文件上传到 Spring Boot REST 端点
创始人
2024-08-15 08:30:08
0次

下面是一个示例代码,演示了如何使用Android Retrofit库将图片文件上传到Spring Boot REST端点。

  1. 首先,确保您的Android项目中添加了Retrofit库的依赖项。您可以在项目的build.gradle文件中添加以下行:
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
  1. 在Android项目中创建一个包含以下代码的Retrofit客户端类:
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;

import java.io.File;

public class RetrofitClient {

    private static final String BASE_URL = "http://your-spring-boot-server.com/";

    public interface UploadService {
        @Multipart
        @POST("/upload")
        Call uploadImage(@Part MultipartBody.Part image);
    }

    public static UploadService createUploadService() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        return retrofit.create(UploadService.class);
    }

    public static void uploadImage(File imageFile) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), imageFile);
        MultipartBody.Part imagePart = MultipartBody.Part.createFormData("image", imageFile.getName(), requestBody);

        UploadService service = createUploadService();
        Call call = service.uploadImage(imagePart);
        call.enqueue(new retrofit2.Callback() {
            @Override
            public void onResponse(Call call, retrofit2.Response response) {
                if (response.isSuccessful()) {
                    // 文件上传成功
                } else {
                    // 文件上传失败
                }
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                // 请求失败
            }
        });
    }
}
  1. 在您的Android活动或片段中,您可以使用以下代码来上传图片文件:
File imageFile = new File("/path/to/image.jpg");
RetrofitClient.uploadImage(imageFile);

请注意,在上面的代码中,您需要将your-spring-boot-server.com替换为您的Spring Boot服务器的实际URL。另外,您还需要在Spring Boot服务器端创建一个REST端点来接收上传的文件。

希望这个示例能帮助到您。

相关内容

热门资讯

揭秘作弊器!闲玩娱乐鱼虾蟹作弊... 揭秘作弊器!闲玩娱乐鱼虾蟹作弊器多功能AI方法,闲玩娱乐鱼虾蟹果然是有作弊器方法1、闲玩娱乐鱼虾蟹脚...
我来教大家透视挂!星狮娱乐透视... 我来教大家透视挂!星狮娱乐透视挂多功能AIapp,星狮娱乐原来是真的有透视挂app1、我来教大家透视...
必看攻略开挂!年年大厅开挂多功... 必看攻略开挂!年年大厅开挂多功能AI插件,年年大厅果然存在有开挂插件1、年年大厅免费脚本咨询教程、年...
一分钟了解开挂!淘讯娱乐开挂多... 一分钟了解开挂!淘讯娱乐开挂多功能AI工具,淘讯娱乐好像真的是有开挂工具1、上手简单,内置详细流程视...
推荐十款透视挂!玄灵大厅透视挂... 推荐十款透视挂!玄灵大厅透视挂多功能AI软件,玄灵大厅好像有透视挂软件1、打开软件启动之后找到中间准...
交流学习经验辅助!九天互娱辅助... 交流学习经验辅助!九天互娱辅助多功能AI插件,九天互娱确实真的是有辅助插件1、九天互娱辅助软件下载优...
推荐十款外挂!老夫子大厅外挂多... 推荐十款外挂!老夫子大厅外挂多功能AI攻略,老夫子大厅总是存在有外挂攻略小薇(辅助器软件下载)致您一...
玩家必看科普辅助!炫酷娱乐辅助... 玩家必看科普辅助!炫酷娱乐辅助多功能AI插件,炫酷娱乐总是真的有辅助插件1、用户打开应用后不用登录就...
分享一款开挂!卡米大厅开挂多功... 您好,卡米大厅这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款...
盘点十款辅助器!甘孜大厅辅助器... 盘点十款辅助器!甘孜大厅辅助器多功能AI攻略,甘孜大厅好像是有辅助器攻略小薇(辅助器软件下载)致您一...