package archives.commom.utils; import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.*; import java.util.Map.Entry; /** * @author liwenlong */ public class LinkHelper { private static Map<String, String> types; private static List<String> extensions = new ArrayList<String>(); public final static Map<String, String> FILE_TYPE_MAP = new HashMap<String, String>(); static { getAllFileType(); initContentType(); initExtentsion(); } public static void main(String[] args) throws IOException { System.out.println(getExtNameFromUrl("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1596098400&di=c0f190652af46efab92e7b6cd4a9caab&src=http://a3.att.hudong.com/14/75/01300000164186121366756803686.jpg")); } public static String getExtNameFromUrl(String url) throws IOException { URL obj = new URL(url); URLConnection conn = obj.openConnection(); String contentType = conn.getContentType(); String disposition = conn.getHeaderField("Content-disposition"); conn.connect(); InputStream inputStream = conn.getInputStream(); ByteOutputStream byteOutputStream = new ByteOutputStream(); byte[] bytes = new byte[1024]; int len = 0; byteOutputStream.write(bytes, 0, 50); byte[] byteArray = byteOutputStream.toByteArray(); inputStream.close(); byteOutputStream.close(); String fileType = getFileType(url, disposition, contentType, byteArray); return fileType; } /** * @author liwenlong * @DateTime * @Desc 获取文件后缀 */ public static String getFileType(String url, String disposition, String contentType, byte[] byteArray) { String ext = null; ext = getTypeByExtenssion(url); { if (ext != null) return ext; } ext = getTypeByDisposition(disposition); { if (ext != null) return ext; } ext = getTypeByContentType(contentType); { if (ext != null) return ext; } ext = getFileTypeByStream(byteArray); { if (ext != null) return ext; } return ".html"; } /** * @DateTime * @Desc 通过文件头 获取文件后缀 */ private static String getFileTypeByStream(byte[] b) { if (ArrayUtils.isNotEmpty(b)) { b = ArrayUtils.subarray(b, 0, 50); String filetypeHex = String.valueOf(getFileHexString(b)); Iterator<Entry<String, String>> entryiterator = FILE_TYPE_MAP.entrySet().iterator(); while (entryiterator.hasNext()) { Entry<String, String> entry = entryiterator.next(); String fileTypeHexValue = entry.getValue(); if (filetypeHex.toUpperCase().startsWith(fileTypeHexValue)) { return entry.getKey(); } } } return null; } private static String getFileHexString(byte[] b) { StringBuilder stringBuilder = new StringBuilder(); if (b == null || b.length <= 0) { return null; } for (int i = 0; i < b.length; i++) { int v = b[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } /** * @DateTime * @Desc 通过链接的后缀名判断文件的类型 */ public static String getTypeByExtenssion(String linkUrl) { if (linkUrl == null) return null; linkUrl = linkUrl.toLowerCase(); for (String ext : extensions) { if (linkUrl.endsWith(ext)) { return ext; } } return null; } private static String getTypeByDisposition(String disposition) { String ext = null; if (!StringUtils.isEmpty(disposition)) { disposition = StringUtils.replace(disposition, "\"", ""); String[] strs = disposition.split(";"); for (String string : strs) { if (string.toLowerCase().indexOf("filename=") >= 0) { ext = StringUtils.substring(string, string.lastIndexOf(".")); break; } } } return ext; } /** * @DateTime * @Desc 根据ContentType获取文件后缀 */ private static String getTypeByContentType(String contentType) { if (types.containsKey(contentType)) return types.get(contentType); return null; } // 通过文件头判断会出现重复的情况 private static void getAllFileType() { FILE_TYPE_MAP.put(".pdf", "255044462D312E"); // Adobe Acrobat (pdf) FILE_TYPE_MAP.put(".doc", "D0CF11E0"); // MS Word FILE_TYPE_MAP.put(".xls", "D0CF11E0"); // MS Excel 注意:word 和 excel的文件头一样 FILE_TYPE_MAP.put(".jpg", "FFD8FF"); // JPEG (jpg) FILE_TYPE_MAP.put(".png", "89504E47"); // PNG (png) FILE_TYPE_MAP.put(".gif", "47494638"); // GIF (gif) FILE_TYPE_MAP.put(".tif", "49492A00"); // TIFF (tif) FILE_TYPE_MAP.put(".bmp", "424D"); // Windows Bitmap (bmp) FILE_TYPE_MAP.put(".dwg", "41433130"); // CAD (dwg) FILE_TYPE_MAP.put(".html", "68746D6C3E"); // HTML (html) FILE_TYPE_MAP.put(".rtf", "7B5C727466"); // Rich Text Format (rtf) FILE_TYPE_MAP.put(".xml", "3C3F786D6C"); FILE_TYPE_MAP.put(".zip", "504B0304"); // docx的文件头与zip的一样 FILE_TYPE_MAP.put(".rar", "52617221"); FILE_TYPE_MAP.put(".psd", "38425053"); // Photoshop (psd) FILE_TYPE_MAP.put(".eml", "44656C69766572792D646174653A"); // Email FILE_TYPE_MAP.put(".dbx", "CFAD12FEC5FD746F"); // Outlook Express (dbx) FILE_TYPE_MAP.put(".pst", "2142444E"); // Outlook (pst) FILE_TYPE_MAP.put(".mdb", "5374616E64617264204A"); // MS Access (mdb) FILE_TYPE_MAP.put(".wpd", "FF575043"); // WordPerfect (wpd) FILE_TYPE_MAP.put(".eps", "252150532D41646F6265"); FILE_TYPE_MAP.put(".ps", "252150532D41646F6265"); FILE_TYPE_MAP.put(".qdf", "AC9EBD8F"); // Quicken (qdf) FILE_TYPE_MAP.put(".pwl", "E3828596"); // Windows Password (pwl) FILE_TYPE_MAP.put(".wav", "57415645"); // Wave (wav) FILE_TYPE_MAP.put(".avi", "41564920"); FILE_TYPE_MAP.put(".ram", "2E7261FD"); // Real Audio (ram) FILE_TYPE_MAP.put(".rm", "2E524D46"); // Real Media (rm) FILE_TYPE_MAP.put(".mpg", "000001BA"); // FILE_TYPE_MAP.put(".mov", "6D6F6F76"); // Quicktime (mov) FILE_TYPE_MAP.put(".asf", "3026B2758E66CF11"); // Windows Media (asf) FILE_TYPE_MAP.put(".mid", "4D546864"); // MIDI (mid) } // 对应的http contenttype private static void initContentType() { types = new HashMap<String, String>(); types.put("application/pdf", ".pdf"); types.put("application/msword", ".doc"); types.put("text/plain", ".txt"); types.put("application/javascript", ".js"); types.put("application/x-xls", ".xls"); types.put("application/-excel", ".xls"); types.put("text/html", ".html"); types.put("application/x-rtf", ".rtf"); types.put("application/x-ppt", ".ppt"); types.put("image/jpeg", ".jpg"); types.put("application/vnd.openxmlformats-officedocument.wordprocessingml.template", ".docx"); types.put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".xlsx"); types.put("application/vnd.openxmlformats-officedocument.presentationml.presentation", ".pptx"); types.put("message/rfc822", ".eml"); types.put("application/xml", ".xml"); } //自定义需要匹配链接的文件后缀,不满足的可以自行添加 private static void initExtentsion() { extensions.add(".pdf"); extensions.add(".doc"); extensions.add(".txt"); extensions.add(".xls"); extensions.add(".html"); extensions.add(".rtf"); extensions.add(".mht"); extensions.add(".rar"); extensions.add(".ppt"); extensions.add(".jpg"); extensions.add(".docx"); extensions.add(".xlsx"); extensions.add(".pptx"); extensions.add(".eml"); extensions.add(".zip"); extensions.add(".docm"); extensions.add(".xlsm"); extensions.add(".xlsb"); extensions.add(".dox"); extensions.add(".csv"); extensions.add(".pdf"); extensions.add(".doc"); extensions.add(".xls"); extensions.add(".jpg"); extensions.add(".png"); extensions.add(".gif"); extensions.add(".tif"); extensions.add(".bmp"); extensions.add(".dwg"); extensions.add(".html"); extensions.add(".rtf"); extensions.add(".xml"); extensions.add(".zip"); extensions.add(".rar"); extensions.add(".psd"); extensions.add(".eml"); extensions.add(".dbx"); extensions.add(".pst"); extensions.add(".mdb"); extensions.add(".wpd"); extensions.add(".eps"); extensions.add(".ps"); extensions.add(".qdf"); extensions.add(".pwl"); extensions.add(".wav"); extensions.add(".avi"); extensions.add(".ram"); extensions.add(".rm"); extensions.add(".mpg"); extensions.add(".mov"); extensions.add(".asf"); extensions.add(".mid"); } }
从url判断文件类型,有的url带有文件后缀,有的不带,本方法可以统一处理