org.eu.vooo.commons.net.util.FileUtil Maven / Gradle / Ivy
package org.eu.vooo.commons.net.util;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import org.eu.vooo.commons.lang.exception.ServiceException;
/**
* @author [email protected]
*/
public class FileUtil {
public static final String DEFAULT_FORM_DATA_FILE_NAME = "file";
public static MultipartBody.Part toRequestBody(byte[] fileBytes, String fileName) throws ServiceException {
if (fileBytes.length <= 0) {
throw new ServiceException("文件上传异常");
}
RequestBody fileRequestBody = RequestBody.create(fileBytes, MediaType.parse("multipart/form-data"));
return MultipartBody.Part.createFormData(DEFAULT_FORM_DATA_FILE_NAME, fileName, fileRequestBody);
}
public static MultipartBody.Part toRequestBody(byte[] fileBytes, String formDataFileName, String fileName) throws ServiceException {
if (fileBytes.length <= 0) {
throw new ServiceException("文件上传异常");
}
RequestBody fileRequestBody = RequestBody.create(fileBytes, MediaType.parse("multipart/form-data"));
return MultipartBody.Part.createFormData(formDataFileName, fileName, fileRequestBody);
}
}