java 传输 获取文件类型

java 传输 获取文件类型,第1张

  获取文件类型,一般的是列出目前所有的文件类型,根据表头进行相应判断,示例如下:

/

 件头是位于文件开头的一段承担一定任务的数据,一般都在开头的部分。

 头文件作为一种包含功能函数、数据接口声明的载体文件,用于保存程序的声明(declaration),而定义文件用于保存程序的实现 (implementation)。

 为了解决在用户上传文件的时候在服务器端判断文件类型的问题,故用获取文件头的方式,直接读取文件的前几个字节,来判断上传文件是否符合格式。具体代码如下:

 Java代码 : 

 

/

package comyonyousudfile;

import javaioFileInputStream;

import javaioIOException;

import javautilHashMap;

/

 获取和判断文件头信息

 @author Sud

/

public class GetTypeByHead {

//缓存文件头信息-文件头信息

public static final HashMap<String, String> mFileTypes = new HashMap<String, String>();

static {

// images

mFileTypesput("FFD8FF", "jpg");

mFileTypesput("89504E47", "png");

mFileTypesput("47494638", "gif");

mFileTypesput("49492A00", "tif");

mFileTypesput("424D", "bmp");

//

mFileTypesput("41433130", "dwg"); // CAD

mFileTypesput("38425053", "psd");

mFileTypesput("7B5C727466", "rtf"); // 日记本

mFileTypesput("3C3F786D6C", "xml");

mFileTypesput("68746D6C3E", "html");

mFileTypesput("44656C69766572792D646174653A", "eml"); // 邮件

mFileTypesput("D0CF11E0", "doc");

mFileTypesput("5374616E64617264204A", "mdb");

mFileTypesput("252150532D41646F6265", "ps");

mFileTypesput("255044462D312E", "pdf");

mFileTypesput("504B0304", "docx");

mFileTypesput("52617221", "rar");

mFileTypesput("57415645", "wav");

mFileTypesput("41564920", "avi");

mFileTypesput("2E524D46", "rm");

mFileTypesput("000001BA", "mpg");

mFileTypesput("000001B3", "mpg");

mFileTypesput("6D6F6F76", "mov");

mFileTypesput("3026B2758E66CF11", "asf");

mFileTypesput("4D546864", "mid");

mFileTypesput("1F8B08", "gz");

}

/

 根据文件路径获取文件头信息

 @param filePath

 文件路径

 @return 文件头信息

/

public static String getFileType(String filePath){

Systemoutprintln(getFileHeader(filePath));

Systemoutprintln(mFileTypesget(getFileHeader(filePath)));

return mFileTypesget(getFileHeader(filePath));

}

/

 根据文件路径获取文件头信息

 @param filePath

 文件路径

 @return 文件头信息

/

public static String getFileHeader(String filePath){

FileInputStream is = null;

String value = null;

try {

is = new FileInputStream(filePath);

byte[] b = new byte[4];

/int read() 从此输入流中读取一个数据字节。 

int read(byte[] b) 从此输入流中将最多 blength 个字节的数据读入一个 byte 数组中。 

 int read(byte[] b, int off, int len) 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。 

/

isread(b, 0, blength);

value = bytesToHexString(b);

} catch (Exception e){

} finally {

if (null != is){

try {

isclose();

} catch (IOException e){

}

}

}

return value;

}

/

 将要读取文件头信息的文件的byte数组转换成string类型表示

 @param src

 要读取文件头信息的文件的byte数组

 @return 文件头信息

/

private static String bytesToHexString(byte[] src){

StringBuilder builder = new StringBuilder();

if (src == null || srclength <= 0){

return null;

}

String hv;

for (int i = 0; i < srclength; i++){

// 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写

hv = IntegertoHexString(src[i] & 0xFF)toUpperCase();

if (hvlength() < 2){

builderappend(0);

}

builderappend(hv);

}

Systemoutprintln(buildertoString());

return buildertoString();

}

public static void main(String[] args)throws Exception {

final String fileType = getFileType("E:/Java编程思想读书笔记docx");

Systemoutprintln(fileType);

}

}

首先在窗体中放置

Microsoft

Common

Dialog

Control,名称指定为

cdlg1。

然后放一个按钮,代码如下:

Private

Sub

Command1_Click()

Dim

fname

As

String

Dim

content

As

String

cdlg1ShowOpen

fname

=

cdlg1FileName

MsgBox

fname

Open

fname

For

Input

As

#1

Input

#1,

content

MsgBox

content

Close

#1

End

Sub

package comhmilyldexp;

import javaioFile;

public class ListFile {

private long[] count = new long[] ;

private File file;

private long[] listFile(String path) {

file = new File(path);

File[] f = filelistFiles();

for (int i = 0; i < flength; i++) {

if (f[i]isDirectory()) {

count[0]++;

thislistFile(f[i]getPath());

} else {

count[1]++;

}

}

return count;

}

/

得到指定路径下的文件和文件夹数量

@param path

要查看的路径

@return object[0]耗时(毫秒)<br>

object[1]文件夹数量<br>

object[2]文件数量

/

public Object[] getFileCount(String path) {

long t = SystemcurrentTimeMillis();

long[] count = thislistFile(path);

t = SystemcurrentTimeMillis() - t;

Object[] o = new Object[] { LongvalueOf(t), LongvalueOf(count[0]),

LongvalueOf(count[1])};

return o;

}

public static void main(String[] args) {

ListFile l = new ListFile();

Object[] count = lgetFileCount("d:\\");

Systemoutprintln(count[0]);

Systemoutprintln(count[1]);

Systemoutprintln(count[2]);

}

}

以前写的一个获取目录下有多少文件和多少文件夹的代码,

可以参考下:)

以上就是关于java 传输 获取文件类型全部的内容,包括:java 传输 获取文件类型、vb 选择文件 获取文件路径、java我想要写某个文件,怎样获取文件的相对路径。文件位置:等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9315281.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存