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

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

本文整理了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

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

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

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

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

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

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

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

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

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

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

  1. /**
  2. * Sets the content type header for the HTTP Response
  3. *
  4. * @param response HTTP response
  5. * @param file file to extract content type
  6. */
  7. private static void setContentTypeHeader(HttpResponse response, File file) {
  8. String contentType = StringKit.mimeType(file.getName());
  9. if (null == contentType) {
  10. contentType = URLConnection.guessContentTypeFromName(file.getName());
  11. }
  12. response.headers().set(HttpConst.CONTENT_TYPE, contentType);
  13. }

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

  1. @Signature
  2. public static String guessContentTypeFromName(String fname) {
  3. return URLConnection.guessContentTypeFromName(fname);
  4. }

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

  1. /**
  2. * Sets the content type header for the HTTP Response
  3. *
  4. * @param response HTTP response
  5. * @param file file to extract content type
  6. */
  7. private static void setContentTypeHeader(HttpResponse response, File file) {
  8. String contentType = StringKit.mimeType(file.getName());
  9. if (null == contentType) {
  10. contentType = URLConnection.guessContentTypeFromName(file.getName());
  11. }
  12. response.headers().set(HttpConst.CONTENT_TYPE, contentType);
  13. }

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

  1. contentType = URLConnection.guessContentTypeFromName(imagePath);

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

  1. @Nullable
  2. static MediaType guessFromPath(String path) {
  3. requireNonNull(path, "path");
  4. final int dotIdx = path.lastIndexOf('.');
  5. final int slashIdx = path.lastIndexOf('/');
  6. if (dotIdx < 0 || slashIdx > dotIdx) {
  7. // No extension
  8. return null;
  9. }
  10. final String extension = Ascii.toLowerCase(path.substring(dotIdx + 1));
  11. final MediaType mediaType = EXTENSION_TO_MEDIA_TYPE.get(extension);
  12. if (mediaType != null) {
  13. return mediaType;
  14. }
  15. final String guessedContentType = URLConnection.guessContentTypeFromName(path);
  16. return guessedContentType != null ? MediaType.parse(guessedContentType) : null;
  17. }

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

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

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

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

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

  1. /**
  2. * Returns an object representing the content of the resource this {@code
  3. * URLConnection} is connected to. First, it attempts to get the content
  4. * type from the method {@code getContentType()} which looks at the response
  5. * header field "Content-Type". If none is found it will guess the content
  6. * type from the filename extension. If that fails the stream itself will be
  7. * used to guess the content type.
  8. *
  9. * @return the content representing object.
  10. * @throws IOException
  11. * if an error occurs obtaining the content.
  12. */
  13. public Object getContent() throws java.io.IOException {
  14. if (!connected) {
  15. connect();
  16. }
  17. if ((contentType = getContentType()) == null) {
  18. if ((contentType = guessContentTypeFromName(url.getFile())) == null) {
  19. contentType = guessContentTypeFromStream(getInputStream());
  20. }
  21. }
  22. if (contentType != null) {
  23. return getContentHandler(contentType).getContent(this);
  24. }
  25. return null;
  26. }

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

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

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

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

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

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

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

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

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

  1. /**
  2. * Method responsible for adding content file for given merchant store in underlying Infinispan tree
  3. * cache. It will take {@link InputContentFile} and will store file for given merchant store according to its type.
  4. * it can save an image or any type of file (pdf, css, js ...)
  5. * @param merchantStoreCode Merchant store
  6. * @param contentFile {@link InputContentFile} being stored
  7. * @throws ServiceException service exception
  8. */
  9. @Override
  10. public void addContentFile( String merchantStoreCode, InputContentFile contentFile )
  11. throws ServiceException
  12. {
  13. Assert.notNull( merchantStoreCode, "Merchant store Id can not be null" );
  14. Assert.notNull( contentFile, "InputContentFile image can not be null" );
  15. Assert.notNull( contentFile.getFileName(), "InputContentFile.fileName can not be null" );
  16. Assert.notNull( contentFile.getFileContentType(), "InputContentFile.fileContentType can not be null" );
  17. String mimeType = URLConnection.guessContentTypeFromName(contentFile.getFileName());
  18. contentFile.setMimeType(mimeType);
  19. if(contentFile.getFileContentType().name().equals(FileContentType.IMAGE.name())
  20. || contentFile.getFileContentType().name().equals(FileContentType.STATIC_FILE.name())) {
  21. addFile(merchantStoreCode,contentFile);
  22. } else {
  23. addImage(merchantStoreCode,contentFile);
  24. }
  25. }

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

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

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

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

相关文章

URLConnection类方法