确定一个字节[]是否是一个pdf文件 [英] Determine if a byte[] is a pdf file

查看:35
本文介绍了确定一个字节[]是否是一个pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法不用打开就可以检查一个byte[]是否是pdf文件?

Is there any way of checking if a byte[] is a pdf without opening?

我有一些代码可以将 byte[] 列表显示为 pdf 缩略图.我以前知道所有字节 [] 都是 pdf 的,因为我们过滤了 servlet 以仅返回这些.现在要求已经改变,我需要恢复所有文件类型.有什么方法可以检查 byte[] 是什么,或者更具体地说,确定它不是 pdf?

I have some code to display a list of byte[] as pdf thumbnails. I previously knew all the byte[] were pdf's because we filtered the servlet to only return these. Now the requirement has changed and I need to bring all file types back. Is there any way of checking what the byte[] is, or more specifically determining if it isn't, a pdf?

推荐答案

检查数组的前 4 个字节.

Check the first 4 bytes of the array.

如果那些是 0x25 0x50 0x44 0x46 那么它很可能是一个 PDF 文件.

If those are 0x25 0x50 0x44 0x46 then it's most probably a PDF file.

这篇关于确定一个字节[]是否是一个pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆