中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

下載網(wǎng)絡(luò)文件的Java代碼

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class DownLoadUtil {
    private static Logger logger = Logger.getLogger(DownLoadUtil.class);
    /**
     * 下載文件
     * @param name 用戶下載的文件名(*****.***)
     * @param filePath 文件路徑
     * @param response 
     * @param fileType 文件類型
     * @return
     * @throws Exception
     */
    public static boolean downLoadFile(String name,String filePath,
            HttpServletResponse response, String fileType)
            throws Exception {
        logger.info("start invoke downLoadFile,[filePath:"+filePath+" , fileType:"+fileType+"]");
        File file = new File(filePath);
        //設(shè)置文件類型
        if("pdf".equals(fileType)){
            response.setContentType("application/pdf");
        }else if("xls".equals(fileType)){
            response.setContentType("application/msexcel");
        }else if("doc".equals(fileType)){
            response.setContentType("application/msword");
        }
        response.setHeader("Content-Disposition", "attachment;filename=\""
                + new String(name.getBytes("GB2312"), "ISO8859-1") + "\"");
        //response.setHeader("Content-Disposition", "attachment;filename=\""+ URLEncoder.encode(name, "UTF-8")+ "\"");
        response.setContentLength((int) file.length());
        byte[] buffer = new byte[4096];// 緩沖區(qū)
        BufferedOutputStream output = null;
        BufferedInputStream input = null;
        try {
            output = new BufferedOutputStream(response.getOutputStream());
            input = new BufferedInputStream(new FileInputStream(file));
            int n = -1;
            while ((n = input.read(buffer, 0, 4096)) > -1) {
                output.write(buffer, 0, n);
            }
            output.flush();
            response.flushBuffer();
        } catch (Exception e) {
            logger.error("exception when invoke downLoadFile",e);
            throw e;
        } finally {
            if (input != null)
                input.close();
            if (output != null)
                output.close();
        }
        logger.info("end invoke downLoadFile!");
        return true;
    }
}

標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:php縮放圖片代碼

下一篇:Java 服務(wù)器獲取請求的IP方法