java.net.URLConnection.guessContentTypeFromName()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(332)

本文整理了Java中java.net.URLConnection.guessContentTypeFromName()方法的一些代码示例,展示了URLConnection.guessContentTypeFromName()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.guessContentTypeFromName()方法的具体详情如下:
包路径:java.net.URLConnection
类名称:URLConnection
方法名:guessContentTypeFromName

URLConnection.guessContentTypeFromName介绍

[英]Determines the MIME-type of the given resource url by resolving the filename extension with the internal FileNameMap. Any fragment identifier is removed before processing.
[中]通过使用内部FileNameMap解析文件扩展名,确定给定资源url的MIME类型。任何片段标识符在处理之前都会被删除。

代码示例

代码示例来源:origin: stackoverflow.com

mimeType= URLConnection.guessContentTypeFromName(file.getName());

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public String getType(Uri uri) {
 return(URLConnection.guessContentTypeFromName(uri.toString()));
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public String getType(Uri uri) {
 return(URLConnection.guessContentTypeFromName(uri.toString()));
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public String getType(Uri uri) {
 return(URLConnection.guessContentTypeFromName(uri.toString()));
}

代码示例来源:origin: stackoverflow.com

writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();

代码示例来源:origin: lets-blade/blade

/**
 * Sets the content type header for the HTTP Response
 *
 * @param response HTTP response
 * @param file     file to extract content type
 */
private static void setContentTypeHeader(HttpResponse response, File file) {
  String contentType = StringKit.mimeType(file.getName());
  if (null == contentType) {
    contentType = URLConnection.guessContentTypeFromName(file.getName());
  }
  response.headers().set(HttpConst.CONTENT_TYPE, contentType);
}

代码示例来源:origin: jphp-group/jphp

@Signature
public static String guessContentTypeFromName(String fname) {
  return URLConnection.guessContentTypeFromName(fname);
}

代码示例来源:origin: lets-blade/blade

/**
 * Sets the content type header for the HTTP Response
 *
 * @param response HTTP response
 * @param file     file to extract content type
 */
private static void setContentTypeHeader(HttpResponse response, File file) {
  String contentType = StringKit.mimeType(file.getName());
  if (null == contentType) {
    contentType = URLConnection.guessContentTypeFromName(file.getName());
  }
  response.headers().set(HttpConst.CONTENT_TYPE, contentType);
}

代码示例来源:origin: primefaces/primefaces

contentType = URLConnection.guessContentTypeFromName(imagePath);

代码示例来源:origin: line/armeria

@Nullable
static MediaType guessFromPath(String path) {
  requireNonNull(path, "path");
  final int dotIdx = path.lastIndexOf('.');
  final int slashIdx = path.lastIndexOf('/');
  if (dotIdx < 0 || slashIdx > dotIdx) {
    // No extension
    return null;
  }
  final String extension = Ascii.toLowerCase(path.substring(dotIdx + 1));
  final MediaType mediaType = EXTENSION_TO_MEDIA_TYPE.get(extension);
  if (mediaType != null) {
    return mediaType;
  }
  final String guessedContentType = URLConnection.guessContentTypeFromName(path);
  return guessedContentType != null ? MediaType.parse(guessedContentType) : null;
}

代码示例来源:origin: shopizer-ecommerce/shopizer

@Override
public void removeFile(String storeCode, String fileName) throws ServiceException {
  
  String fileType = "IMAGE";
  String mimetype = URLConnection.guessContentTypeFromName(fileName);
  String type = mimetype.split("/")[0];
  if(!type.equals("image"))
    fileType = "STATIC_FILE";
  contentFileManager.removeFile( storeCode, FileContentType.valueOf(fileType), fileName);
  
}

代码示例来源:origin: stackoverflow.com

writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();

代码示例来源:origin: robovm/robovm

/**
 * Returns an object representing the content of the resource this {@code
 * URLConnection} is connected to. First, it attempts to get the content
 * type from the method {@code getContentType()} which looks at the response
 * header field "Content-Type". If none is found it will guess the content
 * type from the filename extension. If that fails the stream itself will be
 * used to guess the content type.
 *
 * @return the content representing object.
 * @throws IOException
 *             if an error occurs obtaining the content.
 */
public Object getContent() throws java.io.IOException {
  if (!connected) {
    connect();
  }
  if ((contentType = getContentType()) == null) {
    if ((contentType = guessContentTypeFromName(url.getFile())) == null) {
      contentType = guessContentTypeFromStream(getInputStream());
    }
  }
  if (contentType != null) {
    return getContentHandler(contentType).getContent(this);
  }
  return null;
}

代码示例来源:origin: robovm/robovm

if ((contentType = guessContentTypeFromName(url.getFile())) == null) {
  contentType = guessContentTypeFromStream(getInputStream());

代码示例来源:origin: libgdx/libgdx

sb.append(asset.file.isDirectory() ? 0 : asset.file.length());
sb.append(":");
String mimetype = URLConnection.guessContentTypeFromName(asset.file.name());
sb.append(mimetype == null ? "application/unknown" : mimetype);
sb.append("\n");

代码示例来源:origin: libgdx/libgdx

sb.append(asset.file.isDirectory() ? 0 : asset.file.length());
sb.append(":");
String mimetype = URLConnection.guessContentTypeFromName(asset.file.name());
sb.append(mimetype == null ? "application/unknown" : mimetype);
sb.append("\n");

代码示例来源:origin: shopizer-ecommerce/shopizer

files = new ArrayList<OutputContentFile>();
String mimetype = URLConnection.guessContentTypeFromName(os.getKey());
if (!StringUtils.isBlank(mimetype)) {
 S3Object o = s3.getObject(bucketName, os.getKey());

代码示例来源:origin: shopizer-ecommerce/shopizer

/**
 * Method responsible for adding content file for given merchant store in underlying Infinispan tree
 * cache. It will take  {@link InputContentFile} and will store file for given merchant store according to its type.
 * it can save an image or any type of file (pdf, css, js ...)
 * @param merchantStoreCode Merchant store
 * @param contentFile {@link InputContentFile} being stored
 * @throws ServiceException service exception
 */
@Override
public void addContentFile(  String merchantStoreCode,  InputContentFile contentFile )
  throws ServiceException
{
  Assert.notNull( merchantStoreCode, "Merchant store Id can not be null" );
  Assert.notNull( contentFile, "InputContentFile image can not be null" );
  Assert.notNull( contentFile.getFileName(), "InputContentFile.fileName can not be null" );
  Assert.notNull( contentFile.getFileContentType(), "InputContentFile.fileContentType can not be null" );
  
  String mimeType = URLConnection.guessContentTypeFromName(contentFile.getFileName());
  contentFile.setMimeType(mimeType);
  
  if(contentFile.getFileContentType().name().equals(FileContentType.IMAGE.name())
      || contentFile.getFileContentType().name().equals(FileContentType.STATIC_FILE.name())) {
    addFile(merchantStoreCode,contentFile);
  } else {
    addImage(merchantStoreCode,contentFile);
  }
  
  }

代码示例来源:origin: shopizer-ecommerce/shopizer

files = new ArrayList<OutputContentFile>();
String mimetype = URLConnection.guessContentTypeFromName(os.getKey());
if (!StringUtils.isBlank(mimetype)) {
 S3Object o = s3.getObject(bucketName, os.getKey());

代码示例来源:origin: shopizer-ecommerce/shopizer

fileNames = new ArrayList<String>();
String mimetype = URLConnection.guessContentTypeFromName(os.getKey());
if (!StringUtils.isBlank(mimetype)) {
 fileNames.add(getName(os.getKey()));

相关文章

URLConnection类方法