文件下载

原创 2018年04月17日 10:21:56
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipOutputStream;

import com.infoservice.infox.exception.SystemException;
import com.svw.bmrvw.actions.zip.ZipFileBean;
import com.svw.bmrvw.actions.zip.ZipFileUtil;

/**
 * <p>
 * ClassName: DownloadUtil
 * </p>
 * <p>
 * Description: 文件下载工具类
 * </p>
 * <p>
 * Author: hxz
 * </p>
 * <p>
 * Date: 2018-1-29
 * </p>
 */
public class DownloadUtil {
    /**
     * <p>
     * Field LOG: 日志输出
     * </p>
     */
    static final Logger LOG = Logger.getLogger(DownloadUtil.class);

    /**
     * <p>
     * Description: 单个文件下载
     * </p>
     * 
     * @param fileName 下载的文件名称(如:123.txt),若为空,则取文件本身的名称
     * @param file 需下载的文件
     * @param response 响应
     * @throws IOException IOException
     */
    public static void downloadFile(String fileName, File file, HttpServletResponse response) throws IOException {
        byte[] data = getBytesFromFile(file);
        if (fileName == null || "".equals(fileName)) {
            fileName = file.getName();
        }
        download(fileName, data, response);
    }

    /**
     * <p>
     * Description: 单个文件下载
     * </p>
     * 
     * @param fileName 下载的文件名称(如:123.txt),若为空,则为"未命名"
     * @param data 文件的字节数据
     * @param response response
     * @throws IOException IOException
     */
    public static void download(String fileName, byte[] data, HttpServletResponse response) throws IOException {
        if (fileName == null) {
            fileName = "未命名";
        }
        if (data != null) {
            response.setCharacterEncoding("UTF-8");
            response.setContentLength(data.length);
            fileName = SysUtil.filterHeather(fileName);
            response.addHeader("Content-Disposition",
                "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
            response.setContentType("application/octet-stream");
            response.getOutputStream().write(data);
            response.getOutputStream().flush();
        }
    }

    /**
     * <p>
     * Description: 文件类型转换为byte
     * </p>
     * 
     * @param file 文件
     * @return byte
     */
    public static byte[] getBytesFromFile(File file) {
        if (file == null) {
            return null;
        }
        FileInputStream stream = null;         //文件字节流读取对象
        ByteArrayOutputStream out = null;
        try {
            stream = new FileInputStream(file);
            out = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = stream.read(b)) != -1) {       //一个字节一个字节的读
                out.write(b, 0, n);

            }
            return out.toByteArray();
        } catch (IOException e) {
            LOG.error(e);
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                LOG.error(e);
            }
        }
        return null;
    }

    /**
     * <p>
     * Description: 判断当前操作系统是否是linux
     * </p>
     * 
     * @return boolean
     */
    public static boolean isLinux() {
        boolean bool = false;
        //编码处理
        Properties pro = System.getProperties();
        String osName = pro.getProperty("os.name");
        if ("Linux".equals(osName) || "linux".equals(osName)) {
            bool = true;
        }
        return bool;
    }

}

收藏助手
不良信息举报
您举报文章:文件下载
举报原因:
原因补充:

(最多只允许输入30个字)