java upload file type detection

During file upload, especially when uploading files open to the general user, it is necessary to control the format to upload files to prevent hackers uploading the virus script. A simple way to type the file name of interception is very vulnerable to cracking, virus uploader only need to change the file name will be able to complete the upload.

You can read files hex file header to determine the true file format.

When reading binary data file and convert it to hexadecimal, the same type of data file header is the same, even changed its suffix, the data will not change

Import the java.io. * ;
 Import the java.util.HashMap; 

public  class GetFileType { 

        // cache file header information - file header information 
        public  static  Final the HashMap <String, String> = mFileTypes new new the HashMap <String, String> ();
         static { 
            mFileTypes.put ( "0D0A3C68", "DOC" ); 
            mFileTypes.put ( "504B0304", "docx" ); 
            mFileTypes.put ( "25,504,446", "PDF" );
        } 
        / ** 
     * Get header information according to the file path 
     * 
     * @param  filePath file path
     *@return file header information
      * / 
    public  static String getFileType (String filePath) { 
        String type = getFileHeader (filePath); 
        System.out.println (type); 
        return mFileTypes.get (type); 
    } 

    / ** 
     * obtained according to the file path header information 
     * 
     * @param filePath file path 
     * @return file header information
      * / 
    public  static String getFileHeader (String filePath) { 
        the FileInputStream IS = null ; 
        String value = null ;
         the try{ 
            IS = new new the FileInputStream (filePath);
             byte [] B = new new  byte [. 4 ];
             / * 
             * int Read () to read a byte of data from this input stream. int read (byte [] b) from this input stream up to b.length 
             * bytes of data read into a byte array. Read int (byte [] B, OFF int, int len) 
             * from this input stream up to len bytes of read data into a byte array. 
             * / 
            Is.read (B, 0 , to b.length); 
            value = bytesToHexString (B); 
        } the catch (Exception E) { 
        } the finally {
             IF ( null! = IS) {
                 the try { 
                    is.close (); 
                } the catch (IOException E) { 
                } 
            } 
        } 
        return value; 
    } 

    / ** 
     byte array * file header information to be read into a file type indicates string 
     * 
     * @ param byte array src to read header information file 
     * @return file header
      * / 
    Private  static String bytesToHexString ( byte [] src) { 
        the StringBuilder Builder = new new the StringBuilder ();
        IF(the src == null || src.length <= 0 ) {
             return  null ; 
        } 
        String HV; 
        for ( int I = 0; I <src.length; I ++ ) {
             // hexadecimal (base 16) None signed integer returns a string representation of an integer parameter, and is converted to uppercase 
            HV = Integer.toHexString (the src [I] & 0xFF ) .toUpperCase ();
             IF (hv.length () <2 ) { 
                builder.append ( 0 ); 
            } 
            builder.append (HV); 
        } 
        return builder.toString (); 
    } 

    public  static  void main(String[] args) {
        String path = "E:/file/1.pdf";
        String type = getFileType(path);
        System.out.println(type);
    }
    
}

 Run output

25504446
pdf

Guess you like

Origin www.cnblogs.com/baby123/p/11322972.html
Recommended