All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eu.vooo.commons.net.util.FileUtil Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
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);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy