Example usage for java.nio.file Files probeContentType

List of usage examples for java.nio.file Files probeContentType

Introduction

In this page you can find the example usage for java.nio.file Files probeContentType.

Prototype

public static String probeContentType(Path path) throws IOException 

Source Link

Document

Probes the content type of a file.

Usage

From source file:org.ecocean.media.AssetStore.java

public static JSONObject extractMetadataAttributes(File file) throws IOException { //some "generic attributes" (i.e. not from specific sources like exif)
    JSONObject j = new JSONObject();
    j.put("contentType", Files.probeContentType(file.toPath())); //hopefully we can always/atleast get this

    //we only kinda care about bimg failure -- see: non-images
    BufferedImage bimg = null;/*from   w  w w . j a  va  2s .  c  o m*/
    try {
        bimg = ImageIO.read(file);
    } catch (javax.imageio.IIOException ex) {
    }
    if (bimg != null) {
        j.put("width", (double) bimg.getWidth());
        j.put("height", (double) bimg.getHeight());
    }
    return j;
}

From source file:edu.cornell.kfs.vnd.batch.service.impl.VendorBatchServiceImpl.java

/**
 * Extracting this out to support testing.
 * @param attachmentFile// ww  w .j a  va2s  .  c  om
 * @return
 * @throws IOException
 */
protected String getMimeTypeFromAttachmentFile(File attachmentFile) throws IOException {
    return Files.probeContentType(attachmentFile.toPath());
}

From source file:org.egov.api.controller.ComplaintController.java

/**
 * This will download the support document of the complaint.
 *
 * @param complaintNo//from w  ww.j a  v  a 2s.co m
 * @param fileNo
 * @param isThumbnail
 * @return file
 */

@RequestMapping(value = ApiUrl.COMPLAINT_DOWNLOAD_SUPPORT_DOCUMENT, method = GET)
public void getComplaintDoc(@PathVariable final String complaintNo,
        @RequestParam(value = "fileNo", required = false) Integer fileNo,
        @RequestParam(value = "isThumbnail", required = false, defaultValue = "false") final boolean isThumbnail,
        final HttpServletResponse response) throws IOException {
    try {
        final Complaint complaint = complaintService.getComplaintByCRN(complaintNo);
        final Set<FileStoreMapper> files = complaint.getSupportDocs();
        int i = 1;
        final Iterator<FileStoreMapper> it = files.iterator();
        Integer noOfFiles = fileNo;
        if (noOfFiles == null)
            noOfFiles = files.size();
        File downloadFile;
        while (it.hasNext()) {
            final FileStoreMapper fm = it.next();
            if (i == noOfFiles) {
                downloadFile = fileStoreService.fetch(fm.getFileStoreId(), PGRConstants.MODULE_NAME);
                final ByteArrayOutputStream thumbImg = new ByteArrayOutputStream();
                long contentLength = downloadFile.length();

                if (isThumbnail) {
                    final BufferedImage img = Thumbnails.of(downloadFile).size(200, 200).asBufferedImage();
                    ImageIO.write(img, "jpg", thumbImg);
                    thumbImg.close();
                    contentLength = thumbImg.size();
                }

                response.setHeader("Content-Length", String.valueOf(contentLength));
                response.setHeader("Content-Disposition", "attachment;filename=" + fm.getFileName());
                response.setContentType(Files.probeContentType(downloadFile.toPath()));
                final OutputStream out = response.getOutputStream();
                IOUtils.write(
                        isThumbnail ? thumbImg.toByteArray() : FileUtils.readFileToByteArray(downloadFile),
                        out);
                break;
            }
            i++;
        }
    } catch (final Exception e) {
        LOGGER.error(EGOV_API_ERROR, e);
        throw new IOException();
    }
}

From source file:org.apache.taverna.databundle.TestDataBundles.java

@Test
public void setWorkflowBundle() throws Exception {
    WorkflowBundleIO wfBundleIO = new WorkflowBundleIO();
    WorkflowBundle wfBundle = wfBundleIO.createBundle();
    DataBundles.setWorkflowBundle(dataBundle, wfBundle);

    Path wf = DataBundles.getWorkflow(dataBundle);
    assertEquals("/workflow.wfbundle", wf.toString());
    assertEquals("application/vnd.taverna.scufl2.workflow-bundle", Files.probeContentType(wf));
}

From source file:io.minio.MinioClient.java

/**
 * Uploads given file as object in given bucket.
 * <p>//from ww  w.j  av  a 2s. co  m
 * If the object is larger than 5MB, the client will automatically use a multipart session.
 * </p>
 * <p>
 * If the session fails, the user may attempt to re-upload the object by attempting to create
 * the exact same object again. The client will examine all parts of any current upload session
 * and attempt to reuse the session automatically. If a mismatch is discovered, the upload will fail
 * before uploading any more data. Otherwise, it will resume uploading where the session left off.
 * </p>
 * <p>
 * If the multipart session fails, the user is responsible for resuming or removing the session.
 * </p>
 *
 * @param bucketName  Bucket name.
 * @param objectName  Object name to create in the bucket.
 * @param fileName    File name to upload.
 *
 * @throws InvalidBucketNameException  upon invalid bucket name is given
 * @throws NoResponseException         upon no response from server
 * @throws IOException                 upon connection error
 * @throws XmlPullParserException      upon parsing response xml
 * @throws ErrorResponseException      upon unsuccessful execution
 * @throws InternalException           upon internal library error
 */
public void putObject(String bucketName, String objectName, String fileName)
        throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
        InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
        InternalException, InvalidArgumentException, InsufficientDataException {
    if (fileName == null || "".equals(fileName)) {
        throw new InvalidArgumentException("empty file name is not allowed");
    }

    Path filePath = Paths.get(fileName);
    if (!Files.isRegularFile(filePath)) {
        throw new InvalidArgumentException("'" + fileName + "': not a regular file");
    }

    String contentType = Files.probeContentType(filePath);
    long size = Files.size(filePath);

    RandomAccessFile file = new RandomAccessFile(filePath.toFile(), "r");
    try {
        putObject(bucketName, objectName, contentType, size, file);
    } finally {
        file.close();
    }
}