2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

How to get file MIME type dynamically using Java code

I am getting files using rest api and stored into a folder. Now i want to create a service which will download the files. I am not sure how to get file MIME type dynamically using Java code. Anyone have any solution for this situation?

userimage

You can below simple code to get the MIME type based on the file name :


package test.mimetype;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
	public static void main(String[] args) throws IOException {
		Path source = Paths.get("fileName.jpg");
		System.out.println(Files.probeContentType(source));
	}
}
Answer is