java.nio.file.Files.probeContentType()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(458)

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

Files.probeContentType介绍

暂无

代码示例

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

@Override
public String getMimeType(String filePath) {
  String extension = StringUtils.getFilenameExtension(filePath);
  if (this.mimeTypes.containsKey(extension)) {
    return this.mimeTypes.get(extension).toString();
  }
  else {
    try {
      return Optional.of(Files.probeContentType(Paths.get(filePath))).orElse("application/octet-stream");
    } catch (IOException e) {
      return "application/octet-stream";
    }
  }
}

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

@ExpectWarning("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
void npe3() throws IOException{
  use(Files.probeContentType(PATH).length());
}

代码示例来源:origin: googleapis/google-cloud-java

@Override
Tuple<Path, BlobInfo> parse(String... args) throws IOException {
 if (args.length < 2 || args.length > 3) {
  throw new IllegalArgumentException();
 }
 Path path = Paths.get(args[0]);
 String contentType = Files.probeContentType(path);
 String blob = args.length < 3 ? path.getFileName().toString() : args[2];
 return Tuple.of(path, BlobInfo.newBuilder(args[1], blob).setContentType(contentType).build());
}

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

public static String getMimeType(File file) {
  try {
    String answer = java.nio.file.Files.probeContentType(file.toPath());
    if (Strings.isNotBlank(answer)) {
      return answer;

代码示例来源:origin: google/jimfs

MoreObjects.firstNonNull(Files.probeContentType(path), DEFAULT_CONTENT_TYPE);

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

IOUtils.copyLarge(in, out);
String contentType = Files.probeContentType(tempFile);
if (contentType == null) {
  if (LOGGER.isLoggable(Level.WARNING)) {

代码示例来源:origin: pinterest/secor

.setContentType(Files.probeContentType(localFile.toPath()))
.build();

代码示例来源:origin: awslabs/aws-serverless-java-container

@Override
@SuppressFBWarnings("PATH_TRAVERSAL_IN") // suppressing because we are using the getValidFilePath
public String getMimeType(String s) {
  try {
    String validatedPath = SecurityUtils.getValidFilePath(s);
    return Files.probeContentType(Paths.get(validatedPath));
  } catch (IOException e) {
    log.warn("Could not find content type for file:  " + SecurityUtils.encode(s), e);
    return null;
  }
}

代码示例来源:origin: AsamK/signal-cli

static SignalServiceAttachmentStream createAttachment(File attachmentFile) throws IOException {
  InputStream attachmentStream = new FileInputStream(attachmentFile);
  final long attachmentSize = attachmentFile.length();
  String mime = Files.probeContentType(attachmentFile.toPath());
  if (mime == null) {
    mime = "application/octet-stream";
  }
  // TODO mabybe add a parameter to set the voiceNote, preview, width, height and caption option
  Optional<byte[]> preview = Optional.absent();
  Optional<String> caption = Optional.absent();
  return new SignalServiceAttachmentStream(attachmentStream, mime, attachmentSize, Optional.of(attachmentFile.getName()), false, preview, 0, 0, caption, null);
}

代码示例来源:origin: net.anwiba.commons/anwiba-commons-core

private String getContentType(final Path path) {
  try {
   return Files.probeContentType(path);
  } catch (final IOException exception) {
   return "application/octet-stream"; //$NON-NLS-1$
  }
 }
};

代码示例来源:origin: org.daisy.dotify/dotify.task-api

/**
 * Sets the media type to the media type detected by {@link java.nio.file.Files#probeContentType(java.nio.file.Path)} on
 * the specified file.
 * @param value the file to use
 * @return returns this builder
 * @throws IOException if an I/O error occurs
 */
public Builder mediaType(File value) throws IOException {
  this.mediaType = Files.probeContentType(value.toPath());
  return this;
}

代码示例来源:origin: io.github.splotycode.mosaik/WebApi

@Override
  public String getContentType() throws IOException {
    return Files.probeContentType(file.toPath());
  }
}

代码示例来源:origin: google/graphicsfuzz

private static String getContentType(String filename) {
 try {
  return (Files.probeContentType(Paths.get(filename)));
 } catch (IOException exception) {
  return "text/plain";
 }
}

代码示例来源:origin: nl.goodbytes.xmpp.xep/httpfileuploadcomponent

@Override
public String getContentType( UUID uuid ) throws IOException
{
  final Path path = Paths.get( repository.toString(), uuid.toString() );
  final String result = Files.probeContentType( path );
  Log.debug( "UUID '{}' content type: {}", uuid, result );
  return result;
}

代码示例来源:origin: net.java.linoleum/application

private MimeType getMimeType(final Path path) {
  try {
    return new MimeType(Files.probeContentType(path));
  } catch (final IOException | MimeTypeParseException ex) {
    ex.printStackTrace();
  }
  return null;
}

代码示例来源:origin: org.daisy.streamline/streamline-api

/**
 * Sets the media type to the media type detected by {@link java.nio.file.Files#probeContentType(java.nio.file.Path)} on
 * the specified file.
 * @param value the path to use
 * @return returns this builder
 * @throws IOException if an I/O error occurs
 */
public Builder mediaType(Path value) throws IOException {
  details.mediaType(Files.probeContentType(value));
  return this;
}

代码示例来源:origin: org.wso2.carbon.appmgt/org.wso2.carbon.appmgt.rest.api.util

public static String readFileContentType(String filePath) throws AppManagementException{
  String fileContentType = null;
  try {
   fileContentType = Files.probeContentType(Paths.get(filePath));
  } catch (IOException e) {
    throw new AppManagementException("Error occurred while reading file details from file "+filePath);
  }
  return fileContentType;
}

代码示例来源:origin: com.github.danny02/Util

public static BufferedImage loadImageByMimeType(Path file) throws IOException {
  String mime = Files.probeContentType(file);
  Iterable<ImageReader> iter = new IterableFacade<>(ImageIO.getImageReadersByMIMEType(mime));
  for (ImageReader reader : iter) {
    InputStream in = Files.newInputStream(file);
    try (ImageInputStream ii = ImageIO.createImageInputStream(in);) {
      reader.setInput(ii);
      return reader.read(0);
    }
  }
  throw new IOException("No ImageReader found for the file: " + file);
}

代码示例来源:origin: net.anwiba.commons/anwiba-commons-reference

@Override
public InputStream visitPathResource(final PathResourceReference pathResourceReference) throws IOException {
 final String contentType = Files.probeContentType(pathResourceReference.getPath());
 if (!contentTypeAcceptor.accept(contentType)) {
  throw new IOException("Unexcepted mime type '" + contentType + "'"); //$NON-NLS-1$//$NON-NLS-2$
 }
 return Files.newInputStream(pathResourceReference.getPath());
}

代码示例来源:origin: net.anwiba.commons/anwiba-commons-core

@Override
public InputStream visitPathResource(final PathResourceReference pathResourceReference) throws IOException {
 final String contentType = Files.probeContentType(pathResourceReference.getPath());
 if (!contentTypeAcceptor.accept(contentType)) {
  throw new IOException("Unexcepted mime type '" + contentType + "'"); //$NON-NLS-1$//$NON-NLS-2$
 }
 return Files.newInputStream(pathResourceReference.getPath());
}

相关文章

微信公众号

最新文章

更多