-
[ANDROID] RETROFIT2 파일 + 텍스트 전송ANDROID 2020. 1. 17. 11:12반응형
서버 Controller
@RequestMapping(value="/app/uploadFile.do", method = {RequestMethod.GET, RequestMethod.POST}) public @ResponseBody Object uploadFile(MultipartHttpServletRequest request) throws Exception{ ModelAndView mv = new ModelAndView("jsonView"); System.out.println("test===" + request.getParameter("test")); return mv; }
앱 RetrofitService.java
import java.util.LinkedHashMap; import java.util.List; import okhttp3.MultipartBody; import okhttp3.RequestBody; import retrofit2.Call; import retrofit2.http.Multipart; import retrofit2.http.POST; import retrofit2.http.Part; import retrofit2.http.PartMap; public interface RetrofitService { @Multipart @POST("/app/uploadFile.do") Call<JsonObject> uploadFile(@PartMap() LinkedHashMap<String, RequestBody> partMap, @Part List<MultipartBody.Part> names); }
앱 RetrofitImg.java
import java.io.IOException; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class RetrofitImg { private static RetrofitImg ourInstance = new RetrofitImg(); public static RetrofitImg getInstance() { return ourInstance; } private RetrofitImg() { } OkHttpClient defaultHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder() .addHeader("Content-Type","image/jpeg") .build(); return chain.proceed(request); } }).build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://172.16.75.54:7070") .addConverterFactory(GsonConverterFactory.create()) // 파싱등록 .client(defaultHttpClient) .build(); RetrofitService service = retrofit.create(RetrofitService.class); public RetrofitService getService() { return service; } }
앱 MainActivity.java
public static void fileUpload (File file) { Log.d(TAG, "file===" + file.getName()); RequestBody requestBody; MultipartBody.Part body; LinkedHashMap<String, RequestBody> mapRequestBody = new LinkedHashMap<String, RequestBody>(); List<MultipartBody.Part> arrBody = new ArrayList<>(); requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); mapRequestBody.put("file\"; filename=\"" + file.getName(), requestBody); mapRequestBody.put("test", RequestBody.create(MediaType.parse("text/plain"), "gogogogogogogog")); body = MultipartBody.Part.createFormData("fileName", file.getName(), requestBody); arrBody.add(body); Call<JsonObject> call = RetrofitImg.getInstance().getService().uploadFile(mapRequestBody, arrBody); call.enqueue(new Callback<JsonObject>() { @Override public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { if (response.body() != null) { } } @Override public void onFailure(Call<JsonObject> call, Throwable t) { Log.e(TAG + "Err", t.getMessage()); } }); }
반응형'ANDROID' 카테고리의 다른 글
[ANDROID] 이전 Activity 로 데이터 전송하기 (0) 2020.01.08 [ANDROID] 동적 layout 추가 삭제(LayoutInflater) (1) 2020.01.07 [ANDROID] MainActivity.java 에서 다른 xml ID 호출(LayoutInflater) (0) 2020.01.07