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

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

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

URLConnection.getFileNameMap介绍

[英]Returns the table which is used by all URLConnection instances to determine the MIME-type according to a file extension.
[中]返回所有URLConnection实例用于根据文件扩展名确定MIME类型的表。

代码示例

代码示例来源:origin: jeasonlzy/okhttp-OkGo

  1. /** 根据文件名获取MIME类型 */
  2. public static MediaType guessMimeType(String fileName) {
  3. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  4. fileName = fileName.replace("#", ""); //解决文件名中含有#号异常的问题
  5. String contentType = fileNameMap.getContentTypeFor(fileName);
  6. if (contentType == null) {
  7. return HttpParams.MEDIA_TYPE_STREAM;
  8. }
  9. return MediaType.parse(contentType);
  10. }

代码示例来源:origin: looly/hutool

  1. /**
  2. * 根据文件扩展名获得MimeType
  3. *
  4. * @param filePath 文件路径或文件名
  5. * @return MimeType
  6. * @since 4.1.15
  7. */
  8. public static String getMimeType(String filePath) {
  9. return URLConnection.getFileNameMap().getContentTypeFor(filePath);
  10. }

代码示例来源:origin: looly/hutool

  1. /**
  2. * 根据文件扩展名获得MimeType
  3. *
  4. * @param filePath 文件路径或文件名
  5. * @return MimeType
  6. * @since 4.1.15
  7. */
  8. public static String getMimeType(String filePath) {
  9. return URLConnection.getFileNameMap().getContentTypeFor(filePath);
  10. }

代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking

  1. public static String getMimeType(String path) {
  2. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  3. String contentTypeFor = fileNameMap.getContentTypeFor(path);
  4. if (contentTypeFor == null) {
  5. contentTypeFor = "application/octet-stream";
  6. }
  7. return contentTypeFor;
  8. }

代码示例来源:origin: biezhi/wechat-api

  1. /**
  2. * 获取文件 MimeType
  3. *
  4. * @param fileUrl
  5. * @return
  6. */
  7. public static String getMimeType(String fileUrl) {
  8. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  9. String type = fileNameMap.getContentTypeFor(fileUrl);
  10. return type;
  11. }

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

  1. /**
  2. * Determines the MIME-type of the given resource {@code url} by resolving
  3. * the filename extension with the internal FileNameMap. Any fragment
  4. * identifier is removed before processing.
  5. *
  6. * @param url
  7. * the URL with the filename to get the MIME type.
  8. * @return the guessed content type or {@code null} if the type could not be
  9. * determined.
  10. */
  11. public static String guessContentTypeFromName(String url) {
  12. return getFileNameMap().getContentTypeFor(url);
  13. }

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

  1. public File getCroppedImage(File originalFile, int x1, int y1, int width,
  2. int height) throws Exception {
  3. if(!this.cropeable) {
  4. return originalFile;
  5. }
  6. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  7. String contentType = fileNameMap.getContentTypeFor(originalFile.getName());
  8. String extension = contentType.substring(contentType.indexOf("/"),contentType.length());
  9. BufferedImage image = ImageIO.read(originalFile);
  10. BufferedImage out = image.getSubimage(x1, y1, width, height);
  11. File tempFile = File.createTempFile("temp", "." + extension );
  12. tempFile.deleteOnExit();
  13. ImageIO.write(out, extension, tempFile);
  14. return tempFile;
  15. }

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

  1. try {
  2. FileNameMap fileNameMap = URLConnection.getFileNameMap();

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

  1. FileNameMap fileNameMap = URLConnection.getFileNameMap();

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

  1. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  2. StringBuilder nodePath = new StringBuilder();
  3. nodePath.append(product.getMerchantStore().getCode());

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

  1. @Override
  2. public List<OutputContentFile> getFiles(final String merchantStoreCode,
  3. final FileContentType staticContentType) throws ServiceException {
  4. if (cacheManager.getTreeCache() == null) {
  5. throw new ServiceException(
  6. "CmsStaticContentFileManagerInfinispan has a null cacheManager.getTreeCache()");
  7. }
  8. List<OutputContentFile> images = new ArrayList<OutputContentFile>();
  9. try {
  10. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  11. String nodePath = this.getNodePath(merchantStoreCode, staticContentType);
  12. final Node<String, Object> merchantNode = this.getNode(nodePath);
  13. for (String key : merchantNode.getKeys()) {
  14. byte[] imageBytes = (byte[]) merchantNode.get(key);
  15. OutputContentFile contentImage = new OutputContentFile();
  16. InputStream input = new ByteArrayInputStream(imageBytes);
  17. ByteArrayOutputStream output = new ByteArrayOutputStream();
  18. IOUtils.copy(input, output);
  19. String contentType = fileNameMap.getContentTypeFor(key);
  20. contentImage.setFile(output);
  21. contentImage.setMimeType(contentType);
  22. contentImage.setFileName(key);
  23. images.add(contentImage);
  24. }
  25. } catch (final Exception e) {
  26. LOGGER.error("Error while fetching file for {} merchant ", merchantStoreCode);
  27. throw new ServiceException(e);
  28. }
  29. return images;
  30. }

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

  1. FileNameMap fileNameMap = URLConnection.getFileNameMap();

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

  1. outputStaticContentData.setFile(output);
  2. outputStaticContentData
  3. .setMimeType(URLConnection.getFileNameMap().getContentTypeFor(contentFileName));
  4. outputStaticContentData.setFileName(contentFileName);
  5. outputStaticContentData.setFileContentType(fileContentType);

代码示例来源:origin: 0opslab/opslabJutil

  1. /**
  2. * 获取文件的Mime类型
  3. *
  4. * @param file 需要处理的文件
  5. * @return 返回文件的mime类型
  6. * @throws java.io.IOException
  7. */
  8. public final static String mimeType(String file) throws java.io.IOException {
  9. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  10. return fileNameMap.getContentTypeFor(file);
  11. }

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

  1. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  2. String mimeType = fileNameMap.getContentTypeFor("alert.gif");

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

  1. File request = new File(uri);
  2. mbuffer = new FileInputStream(request);
  3. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  4. String mimeType = fileNameMap.getContentTypeFor(uri);

代码示例来源:origin: cn-ljb/rxjava_for_android

  1. /**
  2. * 初始化Body类型请求参数
  3. * init Body type params
  4. */
  5. private RequestBody initRequestBody(Map<String, Object> params) {
  6. MultipartBuilder bodyBuilder = new MultipartBuilder().type(MultipartBuilder.FORM);
  7. Set<Map.Entry<String, Object>> entries = params.entrySet();
  8. for (Map.Entry<String, Object> entry : entries) {
  9. String key = entry.getKey();
  10. Object value = entry.getValue();
  11. if (value instanceof File) {
  12. File file = (File) value;
  13. try {
  14. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  15. String mimeType = fileNameMap.getContentTypeFor(file.getAbsolutePath());
  16. XgoLog.w("mimeType::" + mimeType);
  17. bodyBuilder.addFormDataPart(key, file.getName(), RequestBody.create(MediaType.parse(mimeType), file));
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. XgoLog.e("mimeType is Error !");
  21. }
  22. } else {
  23. XgoLog.w(key + "::" + value);
  24. bodyBuilder.addFormDataPart(key, value.toString());
  25. }
  26. }
  27. return bodyBuilder.build();
  28. }

代码示例来源:origin: apache/wicket

  1. /**
  2. * Returns the mime type for given filename.
  3. *
  4. * @param fileName
  5. * @return mime type
  6. */
  7. public String getMimeType(final String fileName)
  8. {
  9. return URLConnection.getFileNameMap().getContentTypeFor(fileName);
  10. }

代码示例来源:origin: huxq17/SwipeCardsView

  1. private String getContentType(String path) {
  2. FileNameMap fileNameMap = URLConnection.getFileNameMap();
  3. String contentType = fileNameMap.getContentTypeFor(path);
  4. if (contentType == null) {
  5. contentType = "application/octet-stream";
  6. }
  7. return contentType;
  8. }

代码示例来源:origin: Odoo-mobile/framework

  1. private void requestIntent(Uri uri) {
  2. Intent intent = new Intent(Intent.ACTION_VIEW);
  3. FileNameMap mime = URLConnection.getFileNameMap();
  4. String mimeType = mime.getContentTypeFor(uri.getPath());
  5. intent.setDataAndType(uri, mimeType);
  6. try {
  7. mActivity.startActivity(intent);
  8. } catch (ActivityNotFoundException e) {
  9. Toast.makeText(mActivity, OResource.string(mActivity, R.string.toast_no_activity_found_to_handle_file),
  10. Toast.LENGTH_LONG).show();
  11. }
  12. }

相关文章

URLConnection类方法