本文整理汇总了Java中net.sf.jmimemagic.Magic类的典型用法代码示例。如果您正苦于以下问题:Java Magic类的具体用法?Java Magic怎么用?Java Magic使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Magic类属于net.sf.jmimemagic包,在下文中一共展示了Magic类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: getFileType
import net.sf.jmimemagic.Magic; //导入依赖的package包/类
/**
* Determines the file type for the source path.
*
* @param sourceFilePaths list of source file paths
* @param fileSystem {@link FileSystem}
* @return {@link FileType}
* @throws Exception when input file format cannot be determined and source file paths is empty.
*/
private FileType getFileType(final List<Path> sourceFilePaths, FileSystem fileSystem) throws Exception {
if (!sourceFilePaths.isEmpty()) {
final FSDataInputStream fsDataInputStream = fileSystem.open(sourceFilePaths.get(0));
FileType fileType = null;
try {
final byte[] header = new byte[1000];
fsDataInputStream.read(header);
final byte[] magicHeader = {header[0], header[1], header[2]};
final String fileTypeString = new String(magicHeader);
log.info("File header " + fileTypeString);
if (FileType.ORC.toString().equals(fileTypeString)) {
fileType = FileType.ORC;
} else if (FileType.SEQ.toString().equals(fileTypeString)) {
fileType = FileType.SEQ;
} else {
final String mimeType = Magic.getMagicMatch(header, false).getMimeType();
log.info("File mime Type {}", mimeType);
if (FileType.TEXT.getValue().equalsIgnoreCase(mimeType)) {
log.info("name {}", FileType.TEXT.name());
fileType = FileType.TEXT;
}
}
} catch (MagicMatchNotFoundException e) {
log.info("MagicMatch failed to find file type {}", e.toString());
} finally {
fsDataInputStream.close();
}
if (null != fileType) {
return fileType;
} else {
throw new IllegalStateException("Input file format cannot be determined. Currently supported TEXT, ORC, SEQ");
}
}
throw new IllegalStateException("Source Path does not have any files to compact.");
}
示例7: 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;
}
}
示例8: main
import net.sf.jmimemagic.Magic; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
Thread t = new Thread() {
public void run() {
try {
Magic.initialize();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
};
t.setPriority(Thread.MIN_PRIORITY);
t.start();
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
HttpClient httpClient = new HttpClient();
BdbClient.init();
JFrame frame = new JFrame();
ProxyFactory proxyFactory = VeracityProxyFactory.create();
BdbAdminClient client = new BdbAdminClient(httpClient, frame, new File("config.xml"), proxyFactory);
frame.setTitle("Bdb Network Explorer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(client);
frame.setSize(1024, 768);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
示例9: 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;
}