java 二进制转十六,文件头判断文件类型

@PostMapping("/upload-file")
	public R<FileResponseDTO uploadImageForSummerNote(@RequestPart("file") MultipartFile file, @RequestParam("fileInfo") String fileInfo){
          
   
	InputStream fileStream =file.getInputStream()

	if("89504E47".startsWith(fileTypePrefix)) {
          
   //PNG	
		 return
	}
}
public String getFirstBytes(InputStream is, int byteSize) {
          
   
		if( is == null || byteSize <= 0  || byteSize > 512) {
          
   
			return null;
		}
		
		byte[] b = new byte[byteSize];
        try {
          
   
			is.read(b, 0, b.length);
		} catch (IOException e) {
          
   
			e.printStackTrace();
			return null;
		}
        return bytesToHexString(b);    
        
	}
	private String bytesToHexString(byte[] src) {
          
   
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
          
   
            return null;
        }
        for (int i = 0; i < src.length; i++) {
          
   
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
          
   
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
经验分享 程序员 微信小程序 职场和发展