¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.yssh.utils; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.FileInputStream; |
| | | 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()); |
| | | } |
| | | 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 ""; |
| | | } |
| | | |
| | | int idx = fileName.lastIndexOf(POINT); |
| | | if (idx == -1) { |
| | | return ""; |
| | | } |
| | | |
| | | return fileName.substring(idx).toLowerCase(); |
| | | } |
| | | |
| | | /** |
| | | * è·åå¤ç¨éäºèç½é®ä»¶æ©å±ç±»å |
| | | * |
| | | * @param ext æä»¶æ©å±å |
| | | * @return MIME |
| | | */ |
| | | public static String getMime(String ext) { |
| | | switch (ext) { |
| | | // å¾ç |
| | | case ".tif": |
| | | case ".tiff": |
| | | return "image/tiff"; |
| | | case ".img": |
| | | return "application/x-img"; |
| | | case ".gif": |
| | | return "image/gif"; |
| | | case ".jpg": |
| | | case ".jpeg": |
| | | return "image/jpeg"; |
| | | case ".png": |
| | | return "image/png"; |
| | | // é³/è§é¢ |
| | | case ".mp3": |
| | | return "audio/mp3"; |
| | | case ".mp4": |
| | | return "video/mpeg4"; |
| | | case ".avi": |
| | | return "video/avi"; |
| | | case ".mpg": |
| | | case ".mpeg": |
| | | return "video/mpg"; |
| | | case ".wav": |
| | | return "audio/wav"; |
| | | case ".wma": |
| | | return "audio/x-ms-wma"; |
| | | case ".swf": |
| | | return "application/x-shockwave-flash"; |
| | | case ".wmv": |
| | | return "video/x-ms-wmv"; |
| | | case ".rm": |
| | | return "application/vnd.rn-realmedia"; |
| | | case ".rmvb": |
| | | return "application/vnd.rn-realmedia-vbr"; |
| | | // ç½é¡µ |
| | | case ".js": |
| | | return "application/x-javascript"; |
| | | case ".css": |
| | | return "text/css"; |
| | | case ".asp": |
| | | return "text/asp"; |
| | | case ".mht": |
| | | return "message/rfc822"; |
| | | case ".jsp": |
| | | case ".htm": |
| | | case ".html": |
| | | case ".xhtml": |
| | | return "text/html"; |
| | | case ".xml": |
| | | case ".svg": |
| | | return "text/xml"; |
| | | // æä»¶ |
| | | case ".txt": |
| | | return "text/plain"; |
| | | case ".dbf": |
| | | return "application/x-dbf"; |
| | | case ".mdb": |
| | | return "application/msaccess"; |
| | | case ".pdf": |
| | | return "application/pdf"; |
| | | case ".ppt": |
| | | case ".pptx": |
| | | return "application/x-ppt"; |
| | | case ".doc": |
| | | case ".docx": |
| | | return "application/msword"; |
| | | case ".xls": |
| | | case ".xlsx": |
| | | return "application/vnd.ms-excel"; |
| | | case ".dgn": |
| | | return "application/x-dgn"; |
| | | case ".dwg": |
| | | return "application/x-dwg"; |
| | | case ".ext": |
| | | return "application/x-msdownload"; |
| | | // é»è®¤ |
| | | default: |
| | | return "application/octet-stream"; |
| | | } |
| | | } |
| | | } |