DownloadBiz.java 3.87 KB
package com.cjs.site.biz.info;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.cjs.site.dao.info.DownloadDao;
import com.cjs.site.dao.pub.SysConfigDao;
import com.cjs.site.model.download.DownInfo;
import com.cjs.site.model.pub.SysConfigInfo;

@Service
public class DownloadBiz {

    @Value("${${env}.download.path}")
    private String       downloadPath;
    @Autowired
    private DownloadDao  downloadDao;
    @Autowired
    private SysConfigDao sysConfigDao;
    Logger               log = LogManager.getLogger();

    /** 文件下载 */
    public ResponseEntity<byte[]> download(String url,
                                           HttpServletRequest request) throws IOException {
        HttpHeaders head = new HttpHeaders();
        File file = new File(downloadPath + "downLoad/" + url);
        byte[] bits = FileUtils.readFileToByteArray(file);
        head.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        head.setContentLength(bits.length);
        // 下载的文件名
        head.setContentDispositionFormData("attachment",
            new String(file.getName().getBytes("UTF-8"), "ISO8859-1"));
        return new ResponseEntity<byte[]>(bits, head, HttpStatus.CREATED);
    }

    public void download2(String key, HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream out = null;
        try {
            SysConfigInfo config = sysConfigDao.queryBySysKey(key);
            if (config == null) {
                return;
            }
            File file = new File(downloadPath + "/download/" + config.getSysValue());
            response.setHeader("Content-Disposition",
                "attachment;filename=" + this.encodeFileName(request, file.getName()));
            response.setContentLength((int) file.length());
            response.setContentType("application/octet-stream");
            byte[] fileByte = new byte[1024 * 10];
            FileInputStream fileInputStream = new FileInputStream(file);
            out = response.getOutputStream();
            while (fileInputStream.read(fileByte) > 0) {
                out.write(fileByte, 0, fileByte.length);
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                    out = null;
                } catch (IOException e) {
                    log.error(e.getMessage());
                }
            }
        }
    }

    private String encodeFileName(HttpServletRequest request,
                                  String name) throws UnsupportedEncodingException {

        String agent = request.getHeader("USER-AGENT").toLowerCase();
        if (agent != null && agent.indexOf("firefox") < 0 && agent.indexOf("safari") < 0) {
            return URLEncoder.encode(name, "UTF8");
        }
        return new String(name.getBytes("UTF-8"), "ISO8859-1");
    }

    /**添加下载记录*/
    public void addDownLoad(DownInfo downInfo) {
        downloadDao.addDownInfo(downInfo);
    }

    /**下载统计*/
    public List<DownInfo> getCount() {
        return downloadDao.getCount();
    }

}