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


Java Files.probeContentType方法代碼示例

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


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

示例1: readMetdataFor

import java.nio.file.Files; //導入方法依賴的package包/類
public Metadata readMetdataFor(File f) throws JpegProcessingException {
  Metadata m = null;
  try {
      String mime = Files.probeContentType(f.toPath());
      System.out.println(mime);
      switch(mime){
          case "image/jpeg":
              m = JpegMetadataReader.readMetadata(f,readers);
              break;
          case "image/png":
              m = PngMetadataReader.readMetadata(f);
              break;
          case "image/gif":
              m = GifMetadataReader.readMetadata(f);
              break;
          case "image/bmp":
              m = BmpMetadataReader.readMetadata(f);
              break;
      }
  } catch (IOException | PngProcessingException ex) {
      Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
  }
  return m;
}
 
開發者ID:Obsidiam,項目名稱:joanne,代碼行數:25,代碼來源:ImageManager.java

示例2: read

import java.nio.file.Files; //導入方法依賴的package包/類
/**
 * Reads the file
 * 
 * @param file The file
 * @return Image
 * @throws IOException Impossible read the file 
 */
public Image read(File file) throws IOException {
    String fileType = Files.probeContentType(file.toPath());
    if (isDicom(fileType) || file.getName().endsWith(".dcm")) {
        return readDicom(file);
    }
    return readImage(file);
}
 
開發者ID:VISNode,項目名稱:VISNode,代碼行數:15,代碼來源:InputReader.java

示例3: invoke

import java.nio.file.Files; //導入方法依賴的package包/類
@Override
Object invoke(final MinijaxRequestContext ctx) throws Exception {
    final String resourceName;

    if (directory) {
        resourceName = baseResourceName + "/" + ctx.getUriInfo().getPathParameters().getFirst("file");
    } else {
        resourceName = baseResourceName;
    }

    final URL resourceUrl = MinijaxStaticResource.class.getClassLoader().getResource(resourceName);
    if (resourceUrl == null) {
        return Response.status(Status.NOT_FOUND).build();
    }

    final File resourceFile = new File(resourceUrl.getFile());
    if (resourceFile.isDirectory()) {
        return Response.status(Status.NOT_FOUND).build();
    }

    final String contentType = Files.probeContentType(resourceFile.toPath());
    final MediaType mediaType = contentType == null ? MediaType.APPLICATION_OCTET_STREAM_TYPE : MediaType.valueOf(contentType);
    return Response.ok(resourceUrl.openStream(), mediaType).cacheControl(PUBLIC).build();
}
 
開發者ID:minijax,項目名稱:minijax,代碼行數:25,代碼來源:MinijaxStaticResource.java

示例4: isValid

import java.nio.file.Files; //導入方法依賴的package包/類
@Override
public boolean isValid(File file, ConstraintValidatorContext context) {
    if(Optional.ofNullable(file).isPresent()) {
        String contentType;
        try {
            contentType = Files.probeContentType(file.toPath());
        } catch (IOException e) {
            throw new ResourcePreconditionException("An I/O error occurs");
        }
        return contentType.equals("image/png")
                || contentType.equals("image/jpeg");
    }

    return true;
}
 
開發者ID:JonkiPro,項目名稱:REST-Web-Services,代碼行數:16,代碼來源:ImageValidator.java

示例5: validImage

import java.nio.file.Files; //導入方法依賴的package包/類
/**
 * Convenience method of validation of an image file.
 *
 * @param file The file for validation
 * @throws ResourcePreconditionException if an I/O error occurs or incorrect content type
 */
public static void validImage(final File file) throws ResourcePreconditionException {
    String contentType;
    try {
        contentType = Files.probeContentType(file.toPath());
    } catch (IOException e) {
        throw new ResourcePreconditionException("An I/O error occurs");
    }
    if(!contentType.equals(PNG_MIME_TYPE)
            && !contentType.equals(JPEG_MIME_TYPE)) {
        throw new ResourcePreconditionException("Incorrect content type");
    }
}
 
開發者ID:JonkiPro,項目名稱:REST-Web-Services,代碼行數:19,代碼來源:FileUtils.java

示例6: contentType

import java.nio.file.Files; //導入方法依賴的package包/類
public static final String contentType(String filename){
	String type = null;
	Path path = Paths.get(filename);
	try {
		type = Files.probeContentType(path);
	} catch (IOException e) {
		e.printStackTrace();
	}
	return type;
}
 
開發者ID:Anyfast-Team,項目名稱:Anyfast-Java,代碼行數:11,代碼來源:FileAPI.java

示例7: probeFileTypes

import java.nio.file.Files; //導入方法依賴的package包/類
private static void probeFileTypes() throws Exception {		
	List<String> missmatches = new LinkedList<String>();		
	for (Path path: fileToMimeMap.keySet()) {
		System.out.print("Checking " + path + " ...\t");
		Path filename = rootDirectory.resolve(path);
		if (Files.notExists(filename)) {
			System.err.println(filename + " doesn't exist, unable to probe file type");
		} else {
			String fileType = Files.probeContentType(filename);
			String expectedFileType = fileToMimeMap.get(path);
			
			// The return value can be legitimately null if it doesn't know what it is
			// So skip the checking if it is, and carry on
			if (fileType != null) {
				if (!expectedFileType.equalsIgnoreCase(fileType)) {
					missmatches.add(path +"'s filetype was '" + fileType +  "' but we expected '" + expectedFileType + "'");
					System.out.println("Fail");
				} else {
					System.out.println("OK");
				}
			} else {
				System.out.println("Unknown");
			}
		}
	}
	
	if (missmatches.size() > 0) {
		for (String message: missmatches) {
			System.out.println(message);
		}
		// Wait until here to fail so that we can list all the failures, 
		//  rather than just the first one.
		fail(Arrays.toString(missmatches.toArray()));
	}		
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-systemtest,代碼行數:36,代碼來源:ProbeContentTypeTest.java

示例8: getFileInfo

import java.nio.file.Files; //導入方法依賴的package包/類
private FileInfo getFileInfo(File file) {
    String fileType = "";
    try {
        fileType =  Files.probeContentType(file.toPath());
    } catch (IOException e) {
        LOGGER.info("Can't get file type for file: " + file);
    }

    String url = this.getPathUrlFunction.apply(file.toPath());

    return new FileInfo(file.getName(), fileType, file.length(), Instant.ofEpochMilli(file.lastModified()), url);
}
 
開發者ID:chaokunyang,項目名稱:amanda,代碼行數:13,代碼來源:FileSystemStorageService.java

示例9: postImage

import java.nio.file.Files; //導入方法依賴的package包/類
public static String postImage(String id, String url, File file,
                               String mediatype, Map<String, Object> uploadMediaRequest,
                               String passTicket) throws IOException {

    log.debug("local cookies: " + localCookie.toString());
    String webwxDataTicket = StringUtils.substringBetween(localCookie.toString(), "webwx_data_ticket=", "; expires");
    if (StringUtils.isBlank(webwxDataTicket)) {
        log.error("No webwx_data_ticket in cookies");
        return null;
    }

    String type = Files.probeContentType(file.toPath());

    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("id", id)
            .addFormDataPart("name", file.getName())
            .addFormDataPart("type", type)
            .addFormDataPart("lastModifiedDate", LocalDate.now().getMonthValue()
                    + "/" + LocalDate.now().getDayOfMonth()
                    + "/" + LocalDate.now().getYear()
                    + ", " + LocalTime.now().withNano(0)
                    + " GMT+0800 (CST)")
            .addFormDataPart("size", String.valueOf(Files.size(file.toPath())))
            .addFormDataPart("mediatype", mediatype)
            .addFormDataPart("uploadmediarequest", mapper.writeValueAsString(uploadMediaRequest))
            .addFormDataPart("webwx_data_ticket", webwxDataTicket)
            .addFormDataPart("pass_ticket", passTicket)
            .addFormDataPart("filename", file.getName(), RequestBody.create(MediaType.parse(type), file))
            .build();

    Request request = new Request.Builder()
            .addHeader(USER_AGENT, USER_AGENT_CONTENT)
            .url(url)
            .post(requestBody)
            .build();

    try (ResponseBody responseBody = client.newCall(request).execute().body()) {
        return responseBody.string();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
開發者ID:ingbyr,項目名稱:WechatBot,代碼行數:45,代碼來源:NetUtils.java


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