1 // 此类中判断类型所截取的byte 长度暂不确定,请使用者测试过使用 2 package com.tg.common.other; 3 4 import com.tg.common.tginterface.TG_FileUpload; 5 import java.io.ByteArrayOutputStream; 6 import java.io.File; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.net.InetAddress; 10 import java.text.SimpleDateFormat; 11 import java.util.Date; 12 import java.util.HashMap; 13 import java.util.Iterator; 14 import java.util.Map; 15 import java.util.Map.Entry; 16 import javax.servlet.http.HttpServletRequest; 17 import org.springframework.web.multipart.MultipartFile; 18 import org.springframework.web.multipart.MultipartHttpServletRequest; 19 20 public class MultipartFileUpLoad { 21 public Map<String, String> FILE_TYPE_MAP = new HashMap<String, String>(); 22 23 /** 24 * 封装上传 25 * 26 * @param request 27 * @param fileName 28 * 页面表单file元素name 29 * @param folderName创建的文件夹名 30 * @param type 判断类型 31 * @return 本地文件路径 32 */ 33 34 public String transferToFile(HttpServletRequest request, String fileName, String folderName, 35 FileTypeEnum type) throws Exception { 36 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 37 MultipartFile mfile = multipartRequest.getFile(fileName); 38 39 if (mfile.getSize() == 0) { 40 throw new Exception("请选择文件!"); 41 } 42 FILE_TYPE_MAP.clear(); 43 if (type.equals(FileTypeEnum.audio)) { 44 getAudioFileType(); 45 } else if (type.equals(FileTypeEnum.picture)) { 46 getPicFileType(); 47 } else if (type.equals(FileTypeEnum.video)) { 48 getVideoFileType(); 49 } 50 51 InputStream is = mfile.getInputStream(); 52 ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); 53 54 for (int i = 0; i < 10; i++) { 55 int a = is.read(); 56 bytestream.write(a); 57 } 58 byte imgdata[]=bytestream.toByteArray(); 59 60 61 if (getFileTypeByStream(imgdata) == null) { 62 throw new Exception("不是指定类型文件!"); 63 } 64 try { 65 InetAddress addr = InetAddress.getLocalHost(); // 获取本地计算机信息 66 67 File file = new File(request.getSession().getServletContext() 68 .getRealPath( 69 "/upload/" 70 + addr.getHostName().toString() 71 + folderName 72 + "/" 73 + new SimpleDateFormat("yyyy-MM-dd") 74 .format(new Date()) + "/" 75 + mfile.getOriginalFilename())); 76 if (!file.exists()) { // 判断文件夹是否存在 77 file.mkdirs(); // 不存在则创建 78 } 79 mfile.transferTo(file); 80 System.out.println(file.getPath()); 81 return file.getPath(); 82 } catch (IllegalStateException e) { 83 e.printStackTrace(); 84 } catch (IOException e) { 85 e.printStackTrace(); 86 } 87 return null; 88 } 89 90 91 public String getFileTypeByStream(byte[] b) { 92 String filetypeHex = String.valueOf(getFileHexString(b)); 93 System.out.println(filetypeHex); 94 Iterator<Entry<String, String>> entryiterator = FILE_TYPE_MAP 95 .entrySet().iterator(); 96 while (entryiterator.hasNext()) { 97 Entry<String, String> entry = entryiterator.next(); 98 String fileTypeHexValue = entry.getValue().toUpperCase(); 99 if (filetypeHex.toUpperCase().startsWith(fileTypeHexValue)) { 100 return entry.getKey(); 101 } 102 } 103 return null; 104 } 105 106 public String getFileHexString(byte[] b) { 107 StringBuilder stringBuilder = new StringBuilder(); 108 if (b == null || b.length <= 0) { 109 return null; 110 } 111 for (int i = 0; i < b.length; i++) { 112 int v = b[i] & 0xFF; 113 String hv = Integer.toHexString(v); 114 if (hv.length() < 2) { 115 stringBuilder.append(0); 116 } 117 stringBuilder.append(hv); 118 } 119 return stringBuilder.toString(); 120 } 121 122 public void getVideoFileType() { 123 FILE_TYPE_MAP.put("ram", "2E7261FD"); // Real Audio (ram) 124 FILE_TYPE_MAP.put("rm", "2E524D46"); // Real Media (rm) 125 FILE_TYPE_MAP.put("mov", "00000014667479707174"); // Quicktime (mov) 126 // FILE_TYPE_MAP.put("rmvb", "2e524d46000000120001"); // rmvb 127 FILE_TYPE_MAP.put("avi", "41564920"); 128 FILE_TYPE_MAP.put("avi", "52494646b440c02b4156"); 129 FILE_TYPE_MAP.put("flv", "464C5601050000000900"); 130 FILE_TYPE_MAP.put("mp4", "00000020667479706d70"); 131 FILE_TYPE_MAP.put("wmv", "3026b2758e66CF11a6d9"); 132 // FILE_TYPE_MAP.put("3gp", "00000014667479703367"); 133 FILE_TYPE_MAP.put("mkv", "1a45dfa3010000000000"); 134 } 135 136 public void getPicFileType() { 137 FILE_TYPE_MAP.put("jpg", "FFD8FF"); // JPEG (jpg) 138 FILE_TYPE_MAP.put("png", "89504E47"); // PNG (png) 139 FILE_TYPE_MAP.put("gif", "47494638"); // GIF (gif) 140 FILE_TYPE_MAP.put("bmp", "424D"); // Windows Bitmap (bmp) 141 FILE_TYPE_MAP.put("png", "89504E470D0a1a0a0000"); // PNG (png) 142 FILE_TYPE_MAP.put("bmp", "424d228c010000000000"); // 16色位图(bmp) 143 FILE_TYPE_MAP.put("bmp", "424d8240090000000000"); // 24位位图(bmp) 144 FILE_TYPE_MAP.put("bmp", "424d8e1b030000000000"); // 256色位图(bmp 145 } 146 147 public void getAudioFileType() { 148 FILE_TYPE_MAP.put("wav", "57415645"); // Wave (wav) 149 FILE_TYPE_MAP.put("mid", "4D546864"); // MIDI (mid) 150 FILE_TYPE_MAP.put("mp3", "49443303000000002176"); 151 FILE_TYPE_MAP.put("wav", "52494646e27807005741"); 152 FILE_TYPE_MAP.put("aac", "fff1508003fffcda004c"); 153 FILE_TYPE_MAP.put("wv", "7776706ba22100000704"); 154 FILE_TYPE_MAP.put("flac", "664c6143800000221200"); 155 } 156 157 public MultipartFileUpLoad(){} 158 }
1 FILE_TYPE_MAP.put("ffd8ffe000104a464946", "jpg"); //JPEG (jpg) 2 FILE_TYPE_MAP.put("89504e470d0a1a0a0000", "png"); //PNG (png) 3 FILE_TYPE_MAP.put("47494638396126026f01", "gif"); //GIF (gif) 4 FILE_TYPE_MAP.put("49492a00227105008037", "tif"); //TIFF (tif) 5 FILE_TYPE_MAP.put("424d228c010000000000", "bmp"); //16色位图(bmp) 6 FILE_TYPE_MAP.put("424d8240090000000000", "bmp"); //24位位图(bmp) 7 FILE_TYPE_MAP.put("424d8e1b030000000000", "bmp"); //256色位图(bmp) 8 FILE_TYPE_MAP.put("41433130313500000000", "dwg"); //CAD (dwg) 9 FILE_TYPE_MAP.put("3c21444f435459504520", "html"); //HTML (html) 10 FILE_TYPE_MAP.put("3c21646f637479706520", "htm"); //HTM (htm) 11 FILE_TYPE_MAP.put("48544d4c207b0d0a0942", "css"); //css 12 FILE_TYPE_MAP.put("696b2e71623d696b2e71", "js"); //js 13 FILE_TYPE_MAP.put("7b5c727466315c616e73", "rtf"); //Rich Text Format (rtf) 14 FILE_TYPE_MAP.put("38425053000100000000", "psd"); //Photoshop (psd) 15 FILE_TYPE_MAP.put("46726f6d3a203d3f6762", "eml"); //Email [Outlook Express 6] (eml) 16 FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "doc"); //MS Excel 注意:word、msi 和 excel的文件头一样 17 FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "vsd"); //Visio 绘图 18 FILE_TYPE_MAP.put("5374616E64617264204A", "mdb"); //MS Access (mdb) 19 FILE_TYPE_MAP.put("252150532D41646F6265", "ps"); 20 FILE_TYPE_MAP.put("255044462d312e350d0a", "pdf"); //AdobeAcrobat (pdf) 21 FILE_TYPE_MAP.put("2e524d46000000120001", "rmvb"); //rmvb/rm相同 22 FILE_TYPE_MAP.put("464c5601050000000900", "flv"); //flv与f4v相同 23 FILE_TYPE_MAP.put("00000020667479706d70", "mp4"); 24 FILE_TYPE_MAP.put("49443303000000002176", "mp3"); 25 FILE_TYPE_MAP.put("000001ba210001000180", "mpg"); // 26 FILE_TYPE_MAP.put("3026b2758e66cf11a6d9", "wmv"); //wmv与asf相同 27 FILE_TYPE_MAP.put("52494646e27807005741", "wav"); //Wave (wav) 28 FILE_TYPE_MAP.put("52494646d07d60074156", "avi"); 29 FILE_TYPE_MAP.put("4d546864000000060001", "mid"); //MIDI (mid) 30 FILE_TYPE_MAP.put("504b0304140000000800", "zip"); 31 FILE_TYPE_MAP.put("526172211a0700cf9073", "rar"); 32 FILE_TYPE_MAP.put("235468697320636f6e66", "ini"); 33 FILE_TYPE_MAP.put("504b03040a0000000000", "jar"); 34 FILE_TYPE_MAP.put("4d5a9000030000000400", "exe");//可执行文件 35 FILE_TYPE_MAP.put("3c25402070616765206c", "jsp");//jsp文件 36 FILE_TYPE_MAP.put("4d616e69666573742d56", "mf");//MF文件 37 FILE_TYPE_MAP.put("3c3f786d6c2076657273", "xml");//xml文件 38 FILE_TYPE_MAP.put("494e5345525420494e54", "sql");//xml文件 39 FILE_TYPE_MAP.put("7061636b616765207765", "java");//java文件 40 FILE_TYPE_MAP.put("406563686f206f66660d", "bat");//bat文件 41 FILE_TYPE_MAP.put("1f8b0800000000000000", "gz");//gz文件 42 FILE_TYPE_MAP.put("6c6f67346a2e726f6f74", "properties");//bat文件 43 FILE_TYPE_MAP.put("cafebabe0000002e0041", "class");//bat文件 44 FILE_TYPE_MAP.put("49545346030000006000", "chm");//bat文件 45 FILE_TYPE_MAP.put("04000000010000001300", "mxp");//bat文件 46 FILE_TYPE_MAP.put("504b0304140006000800", "docx");//docx文件 47 FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "wps");//WPS文字wps、表格et、演示dps都是一样的 48 FILE_TYPE_MAP.put("6431303a637265617465", "torrent"); 49 50 51 FILE_TYPE_MAP.put("6D6F6F76", "mov"); //Quicktime (mov) 52 FILE_TYPE_MAP.put("FF575043", "wpd"); //WordPerfect (wpd) 53 FILE_TYPE_MAP.put("CFAD12FEC5FD746F", "dbx"); //Outlook Express (dbx) 54 FILE_TYPE_MAP.put("2142444E", "pst"); //Outlook (pst) 55 FILE_TYPE_MAP.put("AC9EBD8F", "qdf"); //Quicken (qdf) 56 FILE_TYPE_MAP.put("E3828596", "pwl"); //Windows Password (pwl) 57 FILE_TYPE_MAP.put("2E7261FD", "ram"); //Real Audio (ram)