java下载文件方法

java下载文件方法

package cn.dreamit.p1001.model_220629102020KIRSAEgmCU6iBOm1a0k.until;

import cn.dreamit.dreamweb.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class FileDownLoadUtil {

    public static void downloadFile(HttpServletRequest request, HttpServletResponse response, String fileName, String filePath) throws UnsupportedEncodingException {

        File file = new File(filePath);
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentLength((int) file.length());
        //文件后缀
        String fileExtension = filePath.substring(filePath.lastIndexOf("."));
        String contentType = getFileContentType(fileExtension);
        if (StringUtil.isEmpty(contentType)) {
            contentType = "application/octet-stream;charset=utf-8";
        }
        response.setContentType(contentType);
        String userAgent = request.getHeader("User-Agent");
        if (StringUtil.hasLength(userAgent)) {
            userAgent = userAgent.toUpperCase();
        }
        boolean isHasSpace = fileName.contains(" ");
        byte[] bytes;
        if (!StringUtil.hasLength(userAgent) || userAgent.indexOf("MSIE") <= 0 && (userAgent.indexOf("GECKO") <= 0 || userAgent.indexOf("RV:11") <= 0)) {
            bytes = fileName.getBytes("UTF-8");
        } else {
            bytes = fileName.getBytes("gb2312");
        }
        fileName = new String(bytes, "ISO-8859-1");
        if (isHasSpace) {
            response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", fileName));
        } else {
            response.setHeader("Content-disposition", String.format("attachment; filename=%s", fileName));
        }
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(file));
            int i = 0;
            while ((i = bis.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 根据路径上的文件名获取  contentType
     * @param type
     * @return
     */
    public static String getFileContentType(String type) {
        String contentType = "";
        if ("3gpp".equalsIgnoreCase(type)) {
            contentType = "audio/3gpp, video/3gpp";
        } else if ("ac3".equalsIgnoreCase(type)) {
            contentType = "audio/ac3";
        } else if ("asf".equalsIgnoreCase(type)) {
            contentType = "allpication/vnd.ms-asf";
        } else if ("au".equalsIgnoreCase(type)) {
            contentType = "audio/basic";
        } else if ("css".equalsIgnoreCase(type)) {
            contentType = "text/css";
        } else if ("csv".equalsIgnoreCase(type)) {
            contentType = "text/csv";
        } else if ("doc".equalsIgnoreCase(type)) {
            contentType = "application/msword";
        } else if ("dot".equalsIgnoreCase(type)) {
            contentType = "application/msword";
        } else if ("dtd".equalsIgnoreCase(type)) {
            contentType = "application/xml-dtd";
        } else if ("dwg".equalsIgnoreCase(type)) {
            contentType = "image/vnd.dwg";
        } else if ("dxf".equalsIgnoreCase(type)) {
            contentType = "image/vnd.dxf";
        } else if ("gif".equalsIgnoreCase(type)) {
            contentType = "image/gif";
        } else if ("htm".equalsIgnoreCase(type)) {
            contentType = "text/html";
        } else if ("html".equalsIgnoreCase(type)) {
            contentType = "text/html";
        } else if ("jp2".equalsIgnoreCase(type)) {
            contentType = "image/jp2";
        } else if ("jpe".equalsIgnoreCase(type)) {
            contentType = "image/jpeg";
        } else if ("jpeg".equalsIgnoreCase(type)) {
            contentType = "image/jpeg";
        } else if ("jpg".equalsIgnoreCase(type)) {
            contentType = "image/jpeg";
        } else if ("js".equalsIgnoreCase(type)) {
            contentType = "text/javascript, application/javascript";
        } else if ("json".equalsIgnoreCase(type)) {
            contentType = "application/json";
        } else if ("mp2".equalsIgnoreCase(type)) {
            contentType = "audio/mpeg, video/mpeg";
        } else if ("mp3".equalsIgnoreCase(type)) {
            contentType = "audio/mpeg";
        } else if ("mp4".equalsIgnoreCase(type)) {
            contentType = "video/mp4";
        } else if ("mpeg".equalsIgnoreCase(type)) {
            contentType = "video/mpeg";
        } else if ("mpg".equalsIgnoreCase(type)) {
            contentType = "video/mpeg";
        } else if ("mpp".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-project";
        } else if ("ogg".equalsIgnoreCase(type)) {
            contentType = "application/ogg, audio/ogg";
        } else if ("pdf".equalsIgnoreCase(type)) {
            contentType = "application/pdf";
        } else if ("png".equalsIgnoreCase(type)) {
            contentType = "image/png";
        } else if ("bmp".equalsIgnoreCase(type)) {
            contentType = "image/bmp";
        } else if ("pot".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-powerpoint";
        } else if ("pps".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-powerpoint";
        } else if ("ppt".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-powerpoint";
        } else if ("rtf".equalsIgnoreCase(type)) {
            contentType = "application/rtf, text/rtf";
        } else if ("svf".equalsIgnoreCase(type)) {
            contentType = "image/vnd.svf";
        } else if ("tif".equalsIgnoreCase(type)) {
            contentType = "image/tiff";
        } else if ("tiff".equalsIgnoreCase(type)) {
            contentType = "image/tiff";
        } else if ("txt".equalsIgnoreCase(type)) {
            contentType = "text/plain";
        } else if ("wdb".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-works";
        } else if ("wps".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-works";
        } else if ("xhtml".equalsIgnoreCase(type)) {
            contentType = "application/xhtml+xml";
        } else if ("xlc".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-excel";
        } else if ("xlm".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-excel";
        } else if ("xls".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-excel";
        } else if ("xlt".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-excel";
        } else if ("xlw".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-excel";
        } else if ("xml".equalsIgnoreCase(type)) {
            contentType = "text/xml, application/xml";
        } else if ("zip".equalsIgnoreCase(type)) {
            contentType = "aplication/zip";
        } else if ("xlsx".equalsIgnoreCase(type)) {
            contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        } else if ("xltx".equalsIgnoreCase(type)) {
            contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.template";
        } else if ("potx".equalsIgnoreCase(type)) {
            contentType = "application/vnd.openxmlformats-officedocument.presentationml.template";
        } else if ("ppsx".equalsIgnoreCase(type)) {
            contentType = "application/vnd.openxmlformats-officedocument.presentationml.slideshow";
        } else if ("pptx".equalsIgnoreCase(type)) {
            contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
        } else if ("sldx".equalsIgnoreCase(type)) {
            contentType = "application/vnd.openxmlformats-officedocument.presentationml.slide";
        } else if ("docx".equalsIgnoreCase(type)) {
            contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        } else if ("dotx".equalsIgnoreCase(type)) {
            contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.template";
        } else if ("xlsm".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-excel.addin.macroEnabled.12";
        } else if ("xlsb".equalsIgnoreCase(type)) {
            contentType = "application/vnd.ms-excel.sheet.binary.macroEnabled.12";
        } else if ("ofd".equalsIgnoreCase(type)) {
            contentType = "application/ofd";
        }

        return contentType;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值