本文整理汇总了Java中net.sf.jmimemagic.Magic.getMagicMatch方法的典型用法代码示例。如果您正苦于以下问题:Java Magic.getMagicMatch方法的具体用法?Java Magic.getMagicMatch怎么用?Java Magic.getMagicMatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.sf.jmimemagic.Magic
的用法示例。
在下文中一共展示了Magic.getMagicMatch方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFileExtension
import net.sf.jmimemagic.Magic; //导入方法依赖的package包/类
/**
* 传入文件的前部门字节流,获取文件的实际扩展名
*
* @param metaStream
* @return
*/
public static String getFileExtension(byte[] metaStream) {
if (metaStream == null)
return "";
String extension = "";
try {
// onlyMimeMatch true
MagicMatch match = Magic.getMagicMatch(metaStream, true);
if (match != null) {
extension = match.getExtension();
}
if ("".equals(extension)) {
String[] exs = match.getMimeType().split("/");
extension = exs[1];
}
} catch (Exception e) {
}
return extension;
}
示例2: getMimeType
import net.sf.jmimemagic.Magic; //导入方法依赖的package包/类
/**
* return the mime type of a file, dont check extension
* @param barr
* @return mime type of the file
* @throws IOException
*/
public static String getMimeType(byte[] barr, String defaultValue) {
//String mt = getMimeType(new ByteArrayInputStream(barr), null);
//if(!StringUtil.isEmpty(mt,true)) return mt;
PrintStream out = System.out;
try {
System.setOut(new PrintStream(DevNullOutputStream.DEV_NULL_OUTPUT_STREAM));
MagicMatch match = Magic.getMagicMatch(barr);
return match.getMimeType();
}
catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
return defaultValue;
}
finally {
System.setOut(out);
}
}
示例3: fetch
import net.sf.jmimemagic.Magic; //导入方法依赖的package包/类
@Override
public Page fetch(Task task) throws FetchException {
try {
if (task.getUrl().startsWith("selenium://")) {
task.setUrl(task.getUrl().substring("selenium://".length()));
}
Page page = new Page();
long timer = System.currentTimeMillis();
if (cookies.size() > 0) {
cookies.forEach(cookie -> webDriver.manage().addCookie(cookie));
}
webDriver.get(task.getUrl());
task.addVisitCount();
page.setTask(task);
page.setResponseTime(System.currentTimeMillis() - timer);
byte[] bytes = webDriver.getPageSource().getBytes();
page.setContent(bytes);
// we cannot get content-type form selenium :(
// see https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/141#issuecomment-191404952
// using magic-match is a compromise.
MagicMatch match = Magic.getMagicMatch(bytes);
page.setContentType(match.getMimeType());
return page;
} catch (Throwable throwable) {
throw new FetchException(throwable.getMessage(), throwable);
}
}
示例4: fetch
import net.sf.jmimemagic.Magic; //导入方法依赖的package包/类
@Override
public Page fetch(Task task) throws FetchException {
try {
if (task.getUrl().startsWith("phantomjs://")) {
task.setUrl(task.getUrl().substring("phantomjs://".length()));
}
Page page = new Page();
long timer = System.currentTimeMillis();
if (cookies.size() > 0) {
cookies.forEach(cookie -> webDriver.manage().addCookie(cookie));
}
webDriver.get(task.getUrl());
byte[] bytes = webDriver.getPageSource().getBytes();
page.setResponseTime(System.currentTimeMillis() - timer);
task.addVisitCount();
page.setTask(task);
page.setContent(bytes);
// webDriver cannot get content-type form selenium :(
// see https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/141#issuecomment-191404952
// using magic-match is a compromise, which might cause efficiency problems.
MagicMatch match = Magic.getMagicMatch(bytes);
page.setContentType(match.getMimeType());
return page;
} catch (Throwable throwable) {
throw new FetchException(throwable.getMessage(), throwable);
}
}
示例5: checkImageContents
import net.sf.jmimemagic.Magic; //导入方法依赖的package包/类
private void checkImageContents(MultipartFile file) {
MagicMatch match;
try {
match = Magic.getMagicMatch(file.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}
String mimeType = match.getMimeType();
if (!mimeType.startsWith("image")) {
throw new IllegalArgumentException("Not an image type: " + mimeType);
}
}
示例6: perseMimeType
import net.sf.jmimemagic.Magic; //导入方法依赖的package包/类
private static String perseMimeType(byte[] bytes){
try {
MagicMatch match = Magic.getMagicMatch(bytes);
String mimeType = match.getMimeType();
return mimeType;
} catch (Exception e) {
return null;
}
}
示例7: getMimeType
import net.sf.jmimemagic.Magic; //导入方法依赖的package包/类
public static String getMimeType(byte[] data) {
String mimeType = "application/octet-stream";
MagicMatch match = null;
try {
match = Magic.getMagicMatch(data);
mimeType = match.getMimeType();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
e.printStackTrace();
}
return mimeType;
}