燕山石化溯源三维电子沙盘-【后端】-服务
1
13693261870
2024-11-13 2f55cebbad3dea187a5f91d16ec80a9677dab699
src/main/java/com/yssh/utils/WebUtils.java
@@ -6,25 +6,9 @@
import java.net.URLEncoder;
import java.util.Date;
/**
 * Web工具类
 * @author WWW
 * @date 2023-08-09
 */
public class WebUtils {
    /**
     * 字符点
     */
    public final static String POINT = ".";
    /**
     * 下载文件
     *
     * @param file     文件
     * @param fileName 文件名
     * @param res      响应
     * @throws Exception 异常
     */
    public static void download(String file, String fileName, boolean inline, HttpServletResponse res) throws Exception {
        if (StringUtils.isEmpty(fileName)) {
            fileName = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, new Date());
@@ -32,37 +16,27 @@
        fileName = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
        String dispose = inline ? "inline" : "attachment";
        // 设置响应头中文件的下载方式为附件方式,以及设置文件名
        res.setHeader("Content-Disposition", dispose + "; filename*=UTF-8''" + fileName);
        // 设置响应头的编码格式为 UTF-8
        res.setCharacterEncoding("UTF-8");
        // 通过response对象设置响应数据格式(如:"text/plain; charset=utf-8")
        String ext = getExtension(file);
        String mime = getMime(ext);
        res.setContentType(mime);
        // 通过response对象,获取到输出流
        ServletOutputStream outputStream = res.getOutputStream();
        // 定义输入流,通过输入流读取文件内容
        FileInputStream fileInputStream = new FileInputStream(file);
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = fileInputStream.read(bytes)) != -1) {
            // 通过输入流读取文件数据,然后通过上述的输出流写回浏览器
            outputStream.write(bytes, 0, len);
            outputStream.flush();
        }
        // 关闭资源
        fileInputStream.close();
        outputStream.close();
    }
    /**
     * 获取文件扩展名
     */
    public static String getExtension(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return "";
@@ -76,15 +50,8 @@
        return fileName.substring(idx).toLowerCase();
    }
    /**
     * 获取多用途互联网邮件扩展类型
     *
     * @param ext 文件扩展名
     * @return MIME
     */
    public static String getMime(String ext) {
        switch (ext) {
            // 图片
            case ".tif":
            case ".tiff":
                return "image/tiff";
@@ -97,7 +64,6 @@
                return "image/jpeg";
            case ".png":
                return "image/png";
            // 音/视频
            case ".mp3":
                return "audio/mp3";
            case ".mp4":
@@ -119,7 +85,6 @@
                return "application/vnd.rn-realmedia";
            case ".rmvb":
                return "application/vnd.rn-realmedia-vbr";
            // 网页
            case ".js":
                return "application/x-javascript";
            case ".css":
@@ -136,7 +101,6 @@
            case ".xml":
            case ".svg":
                return "text/xml";
            // 文件
            case ".txt":
                return "text/plain";
            case ".dbf":
@@ -160,7 +124,6 @@
                return "application/x-dwg";
            case ".ext":
                return "application/x-msdownload";
            // 默认
            default:
                return "application/octet-stream";
        }