Okhttp implements uploading files + parameter request interface form-data
Sometimes it is necessary to connect to some interfaces, and the interface parameter transfer not only requires various types of parameters, but also requires uploading files. Therefore, there are quite a lot of pitfalls, and using postman to generate code is not easy to use, so this article comes up.
Without further ado, let's just upload the code
First, the service layer
/** * Writing comments is a good habit * * @param mFile * @param accountIndex * @param exportType * @param clear * @param email * @param dimensions * @return * @throws IOException */ public String upload(MultipartFile mFile, Integer accountIndex, String exportType, Boolean clear, String email, String dimensions) throws IOException { // This is the process of converting MultipartFile to File File file = new File((())); ((), file); // URL interface path String url = "http://localhost:8080/upload"; // file is the file to be uploaded File() Here I upload excel, and other types can change this parse by yourself RequestBody fileBody = (("application/"), file);//This is the file written in, and there is also a path, but I am writing the file file here. If parse does not work, you can directly change the "multipart/form-data" // Create an OkHttpClient instance and set the timeout time OkHttpClient okHttpClient = new () .connectTimeout(60L, ) .writeTimeout(60L, ) .readTimeout(60L, ) .build(); // Not only can it support file transmission, but it can also pass parameters while passing files. MultipartBody requestBody = new () .setType() // Set the parameter transfer to form-data format .addFormDataPart("account_index", (accountIndex)) .addFormDataPart("export_type", exportType) .addFormDataPart("clear", (clear)) .addFormDataPart("email", email) .addFormDataPart("dimensions", dimensions) .addFormDataPart("file", (), fileBody) // The intermediate parameter is the file name .build(); // Build a request request body, and if you need to pass the request header, add it yourself Request request = new () .url(url) .post(requestBody) .build(); Response response = null; String result = ""; try { // Send a request response = (request).execute(); result = ().string(); (url + "Send request result:" + result); if (!()) { ("Request failed"); return "Request failed"; } ().close(); } catch (IOException e) { (()); } // Temporary files will be generated locally and need to be deleted after use if (()) { (); } return result; }
Then the parameter transfer of controller layer needs to be used with @RequestParam or a requested entity class directly
If you use entity classes, do not add @RequestBody, otherwise the uploaded file will be invalid.
@RequestPart("file") MultipartFile file to pass parameters
(@RequestPart("file") MultipartFile file, @RequestParam("accountIndex") Integer accountIndex, @RequestParam("exportType") String exportType, @RequestParam(value = "clear", required = false) Boolean clear, @RequestParam("email") String email, @RequestParam(value = "dimensions", required = false) String dimensions)
Examples are as above, or
(@RequestPart("file") MultipartFile file, RequestVo req)
The request is successful and the problem is solved.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.