Friday, January 25, 2013

Get file content type

This example show some methods to get content type of file:

Get file content type
Get file content type


package javafx_fileops;

import java.io.File;
import java.io.IOException;
import java.net.URLConnection;
import java.nio.file.Files;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.activation.MimetypesFileTypeMap;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_FileOps extends Application {
    
    Label label;
    
    @Override
    public void start(Stage primaryStage) {
        Button btnLoad = new Button();
        btnLoad.setText("Load File");
        btnLoad.setOnAction(btnLoadEventListener);
        
        label = new Label();
        
        VBox rootBox = new VBox();
        rootBox.getChildren().addAll(btnLoad, label);
        
        Scene scene = new Scene(rootBox, 400, 350);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    EventHandler<ActionEvent> btnLoadEventListener
    = new EventHandler<ActionEvent>(){
  
        @Override
        public void handle(ActionEvent t) {
            FileChooser fileChooser = new FileChooser();
              
            //Set extension filter
            FileChooser.ExtensionFilter extFilter 
                    = new FileChooser.ExtensionFilter("ALL files (*.*)", "*.*");
            fileChooser.getExtensionFilters().add(extFilter);
               
            //Show open file dialog
            File file = fileChooser.showOpenDialog(null);

            String fileInfo = 
                    "AbsoluteFile: " + file.getAbsoluteFile() + " of " + file.getAbsoluteFile().getClass() + "\n\n"
                    + "toPath(): " + file.toPath() + " of " + file.toPath().getClass() + "\n";
            
            try {
                fileInfo += "by Files.probeContentType(): " + Files.probeContentType(file.toPath()) + "\n";
            } catch (IOException ex) {
                Logger.getLogger(JavaFX_FileOps.class.getName()).log(Level.SEVERE, null, ex);
            }

            fileInfo += "\n";
            fileInfo +=  "getName(): " + file.getName() + " of " + file.getName().getClass() + "\n"
                    + "by URLConnection.guessContentTypeFromName(): " 
                    + URLConnection.guessContentTypeFromName(file.getName()) 
                    + "\n";
            
            fileInfo += "by URLConnection.getFileNameMap().getContentTypeFor(): " 
                    + URLConnection.getFileNameMap().getContentTypeFor(file.getName())
                    + "\n";
            
            MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
            fileInfo += "\n";
            fileInfo += "by MimetypesFileTypeMap().getContentType(): " 
                    + mimeTypesMap.getContentType(file) + "\n";
            
            label.setText(fileInfo);
        }
    };
}


No comments:

Post a Comment