當前位置: 首頁>>代碼示例>>Java>>正文


Java MediaType.parse方法代碼示例

本文整理匯總了Java中org.apache.tika.mime.MediaType.parse方法的典型用法代碼示例。如果您正苦於以下問題:Java MediaType.parse方法的具體用法?Java MediaType.parse怎麽用?Java MediaType.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.tika.mime.MediaType的用法示例。


在下文中一共展示了MediaType.parse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: render

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
@Override
protected void render(RenderingContext context)
{
    ContentReader contentReader = context.makeContentReader();
    String sourceMimeType = contentReader.getMimetype();
    
    // Check that Tika supports the supplied file
    AutoDetectParser p = new AutoDetectParser(tikaConfig);
    MediaType sourceMediaType = MediaType.parse(sourceMimeType);
    if(! p.getParsers().containsKey(sourceMediaType))
    {
       throw new RenditionServiceException(
             "Source mime type of " + sourceMimeType + 
             " is not supported by Tika for HTML conversions"
       );
    }
    
    // Make the HTML Version using Tika
    // This will also extract out any images as found
    generateHTML(p, context);
}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:22,代碼來源:HTMLRenderingEngine.java

示例2: create

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
@RequestMapping(method = POST)
@PreAuthorize("isAuthenticated()")
@ResponseBody
@ResponseStatus(CREATED)
public String create(
        @RequestParam("assetData") final MultipartFile assetData
) throws IOException {

    // Check duplicates
    final GridFSDBFile file = this.gridFs.findOne(Query.query(Criteria.where("filename").is(assetData.getOriginalFilename())));
    if (file != null) {
        throw new DataIntegrityViolationException(String.format("Asset with name '%s' already exists", assetData.getOriginalFilename()));
    } else {
        try (InputStream usedStream = TikaInputStream.get(assetData.getInputStream())) {
            MediaType mediaType = null;
            try {
                mediaType = MediaType.parse(tika.detect(usedStream, assetData.getOriginalFilename()));
            } catch (IOException e) {
                log.warn("Could not detect content type", e);
            }
            this.gridFs.store(assetData.getInputStream(), assetData.getOriginalFilename(), Optional.ofNullable(mediaType).map(MediaType::toString).orElse(null));
            return assetData.getOriginalFilename();
        }
    }
}
 
開發者ID:EuregJUG-Maas-Rhine,項目名稱:site,代碼行數:26,代碼來源:AssetApiController.java

示例3: detectOpenDocument

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
 * OpenDocument files, along with EPub files, have a mimetype
 *  entry in the root of their Zip file. This entry contains the
 *  mimetype of the overall file, stored as a single string.  
 */
private static MediaType detectOpenDocument(ZipFile zip) {
    try {
        ZipArchiveEntry mimetype = zip.getEntry("mimetype");
        if (mimetype != null) {
            InputStream stream = zip.getInputStream(mimetype);
            try {
                return MediaType.parse(IOUtils.toString(stream, "UTF-8"));
            } finally {
                stream.close();
            }
        } else {
            return null;
        }
    } catch (IOException e) {
        return null;
    }
}
 
開發者ID:kolbasa,項目名稱:OCRaptor,代碼行數:23,代碼來源:ZipContainerDetector.java

示例4: addHtmlMetadata

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
 * Adds a metadata setting from the HTML <head/> to the Tika metadata
 * object. The name and value are normalized where possible.
 */
private void addHtmlMetadata(String name, String value) {
    if (name == null || value == null) {
        // ignore
    } else if (name.equalsIgnoreCase("ICBM")) {
        Matcher m = ICBM.matcher(value);
        if (m.matches()) {
            metadata.set("ICBM", m.group(1) + ", " + m.group(2));
            metadata.set(Metadata.LATITUDE, m.group(1));
            metadata.set(Metadata.LONGITUDE, m.group(2));
        } else {
            metadata.set("ICBM", value);
        }
    } else if (name.equalsIgnoreCase(Metadata.CONTENT_TYPE)){
        MediaType type = MediaType.parse(value);
        if (type != null) {
            metadata.set(Metadata.CONTENT_TYPE, type.toString());
        } else {
            metadata.set(Metadata.CONTENT_TYPE, value);
        }
    } else {
        metadata.set(name, value);
    }
}
 
開發者ID:kolbasa,項目名稱:OCRaptor,代碼行數:28,代碼來源:HtmlHandler.java

示例5: detectMediaType

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
public static MediaType detectMediaType(InputStream contentStream, @Nullable String fileName) {
	try {
		return MediaType.parse(tika.detect(contentStream, fileName));
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
開發者ID:jmfgdev,項目名稱:gitplex-mit,代碼行數:8,代碼來源:ContentDetector.java

示例6: UniversalEncodingListener

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
public UniversalEncodingListener(Metadata metadata) {
    MediaType type = MediaType.parse(metadata.get(Metadata.CONTENT_TYPE));
    if (type != null) {
        hint = type.getParameters().get("charset");
    }
    if (hint == null) {
        hint = metadata.get(Metadata.CONTENT_ENCODING);
    }
}
 
開發者ID:jmfgdev,項目名稱:gitplex-mit,代碼行數:10,代碼來源:UniversalEncodingListener.java

示例7: typeBasedOnDetectedTypeAndExtension

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
private MediaType typeBasedOnDetectedTypeAndExtension(MediaType type, String filename)
{
    if (filename != null && type != null)
    {
        String[] detectedAndPossibleTypes = new String[]
        {
            MIMETYPE_PDF, MIMETYPE_APPLICATION_ILLUSTRATOR,
            MIMETYPE_APPLICATION_PS, MIMETYPE_APPLICATION_EPS
        };

        for (int i=detectedAndPossibleTypes.length-1; i>=0; i-=2)
        {
            String detectedType = detectedAndPossibleTypes[i-1];
            if (detectedType.equals(type.toString()))
            {
                String possibleType = detectedAndPossibleTypes[i];
                String extension = getExtension(possibleType);
                if (filename.endsWith("."+extension))
                {
                    type = MediaType.parse(possibleType);
                    break;
                }
            }
        }
    }
    return type;
}
 
開發者ID:Alfresco,項目名稱:alfresco-data-model,代碼行數:28,代碼來源:MimetypeMap.java

示例8: getMimetypeIfNotMatches

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
 * Use Apache Tika to check if the mime type of the document really matches
 * what it claims to be. This is typically used when a transformation or
 * metadata extractions fails, and you want to know if someone has renamed a
 * file and consequently it has the wrong mime type.
 * 
 * @return Null if the mime type seems ok, otherwise the mime type it
 *         probably is
 */
public String getMimetypeIfNotMatches(ContentReader reader)
{
    MediaType type = detectType(null, reader);
    if (type == null)
    {
        // Tika doesn't know so we can't help, sorry...
        return null;
    }

    // Is it a good match?
    if (type.toString().equals(reader.getMimetype())) { return null; }

    // Is it close?
    MediaType claimed = MediaType.parse(reader.getMimetype());
    if (tikaConfig.getMediaTypeRegistry().isSpecializationOf(claimed, type)
            || tikaConfig.getMediaTypeRegistry().isSpecializationOf(type, claimed))
    {
        // Probably close enough
        return null;
    }
    
    // Check through known aliases of the type
    SortedSet<MediaType> aliases = tikaConfig.getMediaTypeRegistry().getAliases(type);
    for (MediaType alias : aliases)
    {
        String aliasType = alias.toString();
        if (aliasType.equals(claimed.toString())) 
        {
            return null; 
        }
    }

    // If we get here, then most likely the type is wrong
    return type.toString();
}
 
開發者ID:Alfresco,項目名稱:alfresco-data-model,代碼行數:45,代碼來源:MimetypeMap.java

示例9: asMediaType

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
 * Makes a non-normalized tika MediaType instance (non-normalized means it may be an alias).
 * Result NOT necessarily exists in the registry.
 */
public static MediaType asMediaType(String mediaType) {
    return MediaType.parse(mediaType);
    // this code returned null for aliases.
    //MimeType mimeType = getMimeTypeForMediaTypeSafe(mediaType, getMimeTypeRegistry(), exact);
    //return mimeType != null ? mimeType.getType() : null;
}
 
開發者ID:ilscipio,項目名稱:scipio-erp,代碼行數:11,代碼來源:TikaUtil.java

示例10: getHttpMediaType

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
MediaType getHttpMediaType(Header[] headers) {
    MediaType httpMediaType = null;
    for (Header h : headers) {
        if (h.getName().equalsIgnoreCase("content-type")) {
            String v = h.getValue();
            httpMediaType = MediaType.parse(v);
        }
    }
    return httpMediaType;
}
 
開發者ID:tballison,項目名稱:SimpleCommonCrawlExtractor,代碼行數:11,代碼來源:AbstractExtractor.java

示例11: parseSiteMap

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
 * Parse a sitemap, given the MIME type, the content bytes, and the URL.
 * Note that contentType is assumed to be correct; in general it is more
 * robust to use the method that doesn't take a contentType, but instead
 * detects this using Tika.
 * 
 * @param contentType
 *            MIME type of content
 * @param content
 *            raw bytes of sitemap file
 * @param url
 *            URL to sitemap file
 * @return Extracted SiteMap/SiteMapIndex
 * @throws UnknownFormatException
 *             if there is an error parsing the sitemap
 * @throws IOException
 *             if there is an error reading in the site map
 *             {@link java.net.URL}
 */
public AbstractSiteMap parseSiteMap(String contentType, byte[] content, URL url) throws UnknownFormatException, IOException {
    MediaType mediaType = MediaType.parse(contentType);

    // Octet-stream is the father of all binary types
    while (mediaType != null && !mediaType.equals(MediaType.OCTET_STREAM)) {
        if (XML_MEDIA_TYPES.contains(mediaType)) {
            return processXml(url, content);
        } else if (TEXT_MEDIA_TYPES.contains(mediaType)) {
            return processText(url, content);
        } else if (GZ_MEDIA_TYPES.contains(mediaType)) {
            InputStream decompressed;
            MediaType embeddedType;
            try {
                decompressed = new GZIPInputStream(new ByteArrayInputStream(content));
                embeddedType = MediaType.parse(TIKA.detect(decompressed));
            } catch (Exception e) {
                UnknownFormatException err = new UnknownFormatException("Failed to detect embedded MediaType of gzipped sitemap: " + url + ", caused by " + e);
                err.initCause(e);
                throw err;
            }
            if (XML_MEDIA_TYPES.contains(embeddedType)) {
                return processGzippedXML(url, content);
            } else if (TEXT_MEDIA_TYPES.contains(embeddedType)) {
                // re-open decompressed stream and parse as text
                decompressed = new GZIPInputStream(new ByteArrayInputStream(content));
                return processText(url, decompressed);
            } else if (GZ_MEDIA_TYPES.contains(embeddedType)) {
                throw new UnknownFormatException("Can't parse gzip recursively: " + url);
            }
            throw new UnknownFormatException("Can't parse a gzipped sitemap with the embedded MediaType of: " + embeddedType + " (at: " + url + ")");
        }
        mediaType = MEDIA_TYPE_REGISTRY.getSupertype(mediaType); // Check
                                                                 // parent
    }

    throw new UnknownFormatException("Can't parse a sitemap with the MediaType of: " + contentType + " (at: " + url + ")");
}
 
開發者ID:crawler-commons,項目名稱:crawler-commons,代碼行數:57,代碼來源:SiteMapParser.java

示例12: initMediaTypes

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
 * Performs a one time intialization of Tika's Media-Type components and
 * media type collection constants <br/>
 * Please note that this is a private static method which is called once per
 * CLASS (not per instance / object)
 */
private static void initMediaTypes() {
    /* XML media types (and all aliases) */
    XML_MEDIA_TYPES.add(APPLICATION_XML);
    XML_MEDIA_TYPES.addAll(MEDIA_TYPE_REGISTRY.getAliases(APPLICATION_XML));

    /* TEXT media types (and all aliases) */
    TEXT_MEDIA_TYPES.add(TEXT_PLAIN);
    TEXT_MEDIA_TYPES.addAll(MEDIA_TYPE_REGISTRY.getAliases(TEXT_PLAIN));

    /* GZIP media types (and all aliases) */
    MediaType gzipMediaType = MediaType.parse("application/gzip");
    GZ_MEDIA_TYPES.add(gzipMediaType);
    GZ_MEDIA_TYPES.addAll(MEDIA_TYPE_REGISTRY.getAliases(gzipMediaType));
}
 
開發者ID:crawler-commons,項目名稱:crawler-commons,代碼行數:21,代碼來源:SiteMapParser.java

示例13: getMimeTypeFromContentType

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
protected static String getMimeTypeFromContentType(String contentType) {
    String result = "";
    MediaType mt = MediaType.parse(contentType);
    if (mt != null) {
        result = mt.getType() + "/" + mt.getSubtype();
    }

    return result;
}
 
開發者ID:ViDA-NYU,項目名稱:ache,代碼行數:10,代碼來源:BaseFetcher.java

示例14: hasValidMimeType

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
@Override
public boolean hasValidMimeType(FileUpload file) {
	MediaType type = MediaType.parse(file.getContentType());
	if (!"image".equalsIgnoreCase(type.getType())) {
		return false;
	}
	boolean gif = "gif".equalsIgnoreCase(type.getSubtype());
	boolean png = "png".equalsIgnoreCase(type.getSubtype());
	boolean jpg = "jpeg".equalsIgnoreCase(type.getSubtype());
	return gif || png || jpg;
}
 
開發者ID:U-QASAR,項目名稱:u-qasar.platform,代碼行數:12,代碼來源:UserProfilePictureUploadHelper.java

示例15: detectOfficeOpenXML

import org.apache.tika.mime.MediaType; //導入方法依賴的package包/類
/**
 * Detects the type of an OfficeOpenXML (OOXML) file from
 *  opened Package 
 */
public static MediaType detectOfficeOpenXML(OPCPackage pkg) {
    PackageRelationshipCollection core = 
       pkg.getRelationshipsByType(ExtractorFactory.CORE_DOCUMENT_REL);
    if (core.size() != 1) {
        // Invalid OOXML Package received
        return null;
    }

    // Get the type of the core document part
    PackagePart corePart = pkg.getPart(core.getRelationship(0));
    String coreType = corePart.getContentType();

    // Turn that into the type of the overall document
    String docType = coreType.substring(0, coreType.lastIndexOf('.'));

    // The Macro Enabled formats are a little special
    if(docType.toLowerCase().endsWith("macroenabled")) {
        docType = docType.toLowerCase() + ".12";
    }

    if(docType.toLowerCase().endsWith("macroenabledtemplate")) {
        docType = MACRO_TEMPLATE_PATTERN.matcher(docType).replaceAll("macroenabled.12");
    }

    // Build the MediaType object and return
    return MediaType.parse(docType);
}
 
開發者ID:kolbasa,項目名稱:OCRaptor,代碼行數:32,代碼來源:ZipContainerDetector.java


注:本文中的org.apache.tika.mime.MediaType.parse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。