SpringMVC上传⽂件以流⽅式判断类型附常⽤类型此类中判断类型所截取的byte 长度暂不确定,请使⽤者测试过使⽤
other;
tginterface.TG_FileUpload;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.InetAddress;
SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
public class MultipartFileUpLoad {
public Map<String, String> FILE_TYPE_MAP = new HashMap<String, String>();
/**
* 封装上传
*
* @param request
* @param fileName
* 页⾯表单file元素name
* @param folderName创建的⽂件夹名
* @param type 判断类型
* @return本地⽂件路径
*/
public String transferToFile(HttpServletRequest request, String fileName, String folderName,
FileTypeEnum type) throws Exception {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile mfile = File(fileName);
if (Size() == 0) {
throw new Exception("请选择⽂件!");
}
FILE_TYPE_MAP.clear();
if (type.equals(FileTypeEnum.audio)) {
getAudioFileType();
} else if (type.equals(FileTypeEnum.picture)) {
getPicFileType();
} else if (type.equals(FileTypeEnum.video)) {
getVideoFileType();
}
InputStream is = InputStream();
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
for (int i = 0; i < 10; i++) {
int a = is.read();
bytestream.write(a);
}
byte imgdata[]=ByteArray();
if (getFileTypeByStream(imgdata) == null) {
throw new Exception("不是指定类型⽂件!");
}
try {
InetAddress addr = LocalHost(); // 获取本地计算机信息
File file = new Session().getServletContext()
.getRealPath(
"/upload/"
+ HostName().toString()
+ folderName
+ "/"
+ new SimpleDateFormat("yyyy-MM-dd")
.format(new Date()) + "/"
+ OriginalFilename()));
if (!ists()) { // 判断⽂件夹是否存在
file.mkdirs(); // 不存在则创建
}
System.out.Path());
Path();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String getFileTypeByStream(byte[] b) {
String filetypeHex = String.valueOf(getFileHexString(b));
System.out.println(filetypeHex);
Iterator<Entry<String, String>> entryiterator = FILE_TYPE_MAP
.entrySet().iterator();
while (entryiterator.hasNext()) {
Entry<String, String> entry = ();
String fileTypeHexValue = Value().toUpperCase();
if (UpperCase().startsWith(fileTypeHexValue)) {
Key();
}
}
return null;
}
public 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 = HexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
String();
}
public void getVideoFileType() {
FILE_TYPE_MAP.put("ram", "2E7261FD"); // Real Audio (ram)
FILE_TYPE_MAP.put("rm", "2E524D46"); // Real Media (rm)
FILE_TYPE_MAP.put("mov", "00000014667479707174"); // Quicktime (mov) // FILE_TYPE_MAP.put("rmvb", "2e524d46000000120001"); // rmvb
FILE_TYPE_MAP.put("avi", "41564920");
FILE_TYPE_MAP.put("avi", "52494646b440c02b4156");
FILE_TYPE_MAP.put("flv", "464C5601050000000900");
FILE_TYPE_MAP.put("mp4", "00000020667479706d70");
FILE_TYPE_MAP.put("wmv", "3026b2758e66CF11a6d9");
// FILE_TYPE_MAP.put("3gp", "00000014667479703367");
FILE_TYPE_MAP.put("mkv", "1a45dfa3010000000000");
}
public void getPicFileType() {
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("bmp", "424D"); // Windows Bitmap (bmp)
FILE_TYPE_MAP.put("png", "89504E470D0a1a0a0000"); // PNG (png)
FILE_TYPE_MAP.put("bmp", "424d228c010*********"); // 16⾊位图(bmp) FILE_TYPE_MAP.put("bmp", "424d8240090000000000"); // 24位位图(bmp) FILE_TYPE_MAP.put("bmp", "424d8e1b030000000000"); // 256⾊位图(bmp }
public void getAudioFileType() {
FILE_TYPE_MAP.put("wav", "57415645"); // Wave (wav)
FILE_TYPE_MAP.put("mid", "4D546864"); // MIDI (mid)
FILE_TYPE_MAP.put("mp3", "49443303000000002176");
FILE_TYPE_MAP.put("wav", "52494646e27807005741");
FILE_TYPE_MAP.put("aac", "fff1508003fffcda004c");
FILE_TYPE_MAP.put("wv", "7776706ba22100000704");
FILE_TYPE_MAP.put("flac", "664c6143800000221200");
}
public MultipartFileUpLoad(){}
}
FILE_TYPE_MAP.put("ffd8ffe000104a464946", "jpg"); //JPEG (jpg)
FILE_TYPE_MAP.put("89504e470d0a1a0a0000", "png"); //PNG (png)
FILE_TYPE_MAP.put("47494638396126026f01", "gif"); //GIF (gif)
FILE_TYPE_MAP.put("49492a00227105008037", "tif"); //TIFF (tif)
FILE_TYPE_MAP.put("424d228c010*********", "bmp"); //16⾊位图(bmp)
FILE_TYPE_MAP.put("424d8240090000000000", "bmp"); //24位位图(bmp)
FILE_TYPE_MAP.put("424d8e1b030000000000", "bmp"); //256⾊位图(bmp)
FILE_TYPE_MAP.put("41433130313500000000", "dwg"); //CAD (dwg)
FILE_TYPE_MAP.put("3c21444f435459504520", "html"); //HTML (html)
FILE_TYPE_MAP.put("3c21646f637479706520", "htm"); //HTM (htm)
FILE_TYPE_MAP.put("48544d4c207b0d0a0942", "css"); //css
FILE_TYPE_MAP.put("696b2e71623d696b2e71", "js"); //js
FILE_TYPE_MAP.put("7b5c727466315c616e73", "rtf"); //Rich Text Format (rtf)
FILE_TYPE_MAP.put("38425053000100000000", "psd"); //Photoshop (psd)
FILE_TYPE_MAP.put("46726f6d3a203d3f6762", "eml"); //Email [Outlook Express 6] (eml)
FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "doc"); //MS Excel 注意:word、msi 和 excel的⽂件头⼀样 FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "vsd"); //Visio 绘图
FILE_TYPE_MAP.put("5374616E64617264204A", "mdb"); //MS Access (mdb)
FILE_TYPE_MAP.put("252150532D41646F6265", "ps");
FILE_TYPE_MAP.put("255044462d312e350d0a", "pdf"); //AdobeAcrobat (pdf)
FILE_TYPE_MAP.put("2e524d46000000120001", "rmvb"); //rmvb/rm相同
FILE_TYPE_MAP.put("464c5601050000000900", "flv"); //flv与f4v相同
FILE_TYPE_MAP.put("00000020667479706d70", "mp4");
FILE_TYPE_MAP.put("49443303000000002176", "mp3");
FILE_TYPE_MAP.put("000001ba210001000180", "mpg"); //
FILE_TYPE_MAP.put("3026b2758e66cf11a6d9", "wmv"); //wmv与asf相同
FILE_TYPE_MAP.put("52494646e27807005741", "wav"); //Wave (wav)
FILE_TYPE_MAP.put("52494646d07d60074156", "avi");
FILE_TYPE_MAP.put("4d546864000000060001", "mid"); //MIDI (mid)
FILE_TYPE_MAP.put("504b0304140000000800", "zip");
spring怎么读取xml文件FILE_TYPE_MAP.put("526172211a0700cf9073", "rar");
FILE_TYPE_MAP.put("235468697320636f6e66", "ini");
FILE_TYPE_MAP.put("504b03040a0000000000", "jar");
FILE_TYPE_MAP.put("4d5a9000030000000400", "exe");//可执⾏⽂件
FILE_TYPE_MAP.put("3c25402070616765206c", "jsp");//jsp⽂件
FILE_TYPE_MAP.put("4d616e69666573742d56", "mf");//MF⽂件
FILE_TYPE_MAP.put("3c3f786d6c2076657273", "xml");//xml⽂件
FILE_TYPE_MAP.put("494e5345525420494e54", "sql");//xml⽂件
FILE_TYPE_MAP.put("7061636b616765207765", "java");//java⽂件
FILE_TYPE_MAP.put("406563686f206f66660d", "bat");//bat⽂件
FILE_TYPE_MAP.put("1f8b0800000000000000", "gz");//gz⽂件
FILE_TYPE_MAP.put("6c6f67346a2e726f6f74", "properties");//bat⽂件
FILE_TYPE_MAP.put("cafebabe0000002e0041", "class");//bat⽂件
FILE_TYPE_MAP.put("49545346030000006000", "chm");//bat⽂件
FILE_TYPE_MAP.put("04000000010000001300", "mxp");//bat⽂件
FILE_TYPE_MAP.put("504b0304140006000800", "docx");//docx⽂件
FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "wps");//WPS⽂字wps、表格et、演⽰dps都是⼀样的
FILE_TYPE_MAP.put("6431303a637265617465", "torrent");
FILE_TYPE_MAP.put("6D6F6F76", "mov"); //Quicktime (mov)
FILE_TYPE_MAP.put("FF575043", "wpd"); //WordPerfect (wpd)
FILE_TYPE_MAP.put("CFAD12FEC5FD746F", "dbx"); //Outlook Express (dbx)
FILE_TYPE_MAP.put("2142444E", "pst"); //Outlook (pst)
FILE_TYPE_MAP.put("AC9EBD8F", "qdf"); //Quicken (qdf)
FILE_TYPE_MAP.put("E3828596", "pwl"); //Windows Password (pwl)
FILE_TYPE_MAP.put("2E7261FD", "ram"); //Real Audio (ram)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论