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

cn.majingjing.http.client.response.handle.FileDataHandler Maven / Gradle / Ivy

The newest version!
package cn.majingjing.http.client.response.handle;

import cn.majingjing.http.client.exception.HttpClientException;
import cn.majingjing.http.client.util.HttpUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件数据处理
 *
 * @author MaMarion
 * @date 2020/4/24
 */
public class FileDataHandler implements DataHandler{

    private final File file;

    public FileDataHandler(File file) {
        this.file = file;
    }

    /**
     * Convert body to File
     *
     * @param body response body byte[]
     * @return File
     */
    @Override
    public File handle(byte[] body) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(body);
            fos.flush();
            return file;
        } catch (IOException e) {
            throw new HttpClientException(e);
        } finally {
            HttpUtils.close(fos);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy