當前位置:首頁 » 文件管理 » java判斷上傳文件類型

java判斷上傳文件類型

發布時間: 2023-06-26 14:39:27

java圖像上傳中如何判斷是否是jpg格式

  • 通過文件的二進制流來判斷一張圖片是否為jpg格式的圖片。

  • 原理很簡單:所有JPG/JPEG格式的圖片都是以十六進制的'FF D8'作為文件開始,'FF
    D9'作為文件結束標志。FF等為16進制數據,占文件的1 byte。如果沒有帶相機信息的jpg圖片,第7, 8, 9,
    10這4位上分別是字元'JFIF'的16進製表示,用UltraEdit打開可以直接看到;如果帶有相機信息,有可能與不帶信息的不同,也可能相同,不
    同的話在那4位上為'Exif'字元的16進製表示,但是他們都是以FF D8開始,FF D9結束。

⑵ Java web文件上傳怎麼限制文件類型

  1. 前端限制,用 正則匹配文件名後綴 /.([jJ][pP][gG]){1}$|.([jJ][pP][eE][gG]){1}$|.([gG][iI][fF]){1}$|.([pP][nN][gG]){1}$|.([bB][mM][pP]){1}$/ 這個是圖片正則匹配


  2. 服務端獲取文件,用正則匹配文件名後綴


  3. =(MultipartHttpServletRequest)request;
    MultipartFilefile=mrequest.getFile("file");
    if(file!=null&&!file.isEmpty()){
    //獲得文件類型(可以判斷如果不是指定類型,禁止上傳)
    StringcontentType=file.getContentType();

⑶ java中判斷上傳文件類型

我一般用SmartUpload這個jar文件做上傳和下載的模塊,它裡面有個getContentType()方法可以獲取文件的上傳類型。這個組件功能挺全的,建議你去看看。

⑷ java web開發,上傳圖片並讀取

java web開發中,使用文件操作類來上傳圖片並讀取,如下代碼:

*@desc:圖片處理工具
*@author:bingye
*@createTime:2015-3-17下午04:25:32
*@version:v1.0
*/
publicclassImageUtil{

/**
*將圖片寫到客戶端
*@author:bingye
*@createTime:2015-3-17下午04:36:04
*@history:
*@paramimage
*@paramresponsevoid
*/
publicstaticvoidwriteImage(byte[]image,HttpServletResponseresponse){
if(image==null){
return;
}
byte[]buffer=newbyte[1024];
InputStreamis=null;
OutputStreamos=null;
try{
is=newByteArrayInputStream(image);
os=response.getOutputStream();
while(is.read(buffer)!=-1){
os.write(buffer);
os.flush();
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(is!=null){is.close();}
if(os!=null){os.close();}
}catch(IOExceptione){
e.printStackTrace();
}
}
}

/**
*獲取指定路勁圖片
*@author:bingye
*@createTime:2015-3-21上午10:50:44
*@paramfilePath
*@paramresponsevoid
*/
publicstaticvoidwriteImage(StringfilePath,HttpServletResponseresponse){
FileimageFile=newFile(filePath);
if(imageFile!=null&&imageFile.exists()){
byte[]buffer=newbyte[1024];
InputStreamis=null;
OutputStreamos=null;
try{
is=newFileInputStream(imageFile);
os=response.getOutputStream();
while(is.read(buffer)!=-1){
os.write(buffer);
os.flush();
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(is!=null){is.close();}
if(os!=null){os.close();}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}

/**
*圖片上傳到文件夾
*@author:bingye
*@createTime:2015-3-20下午08:07:25
*@paramfile
*@paramsavePath
*@returnboolean
*/
(CommonsMultipartFilefile,StringsavePath){
if(file!=null&&!file.isEmpty()){
//獲取文件名稱
StringfileName=file.getOriginalFilename();
//獲取後綴名
StringsuffixName=fileName.substring(fileName.indexOf(".")+1);
//新名稱
StringnewFileName=System.currentTimeMillis()+"."+suffixName;
//新文件路勁
StringfilePath=savePath+newFileName;
//獲取存儲文件路徑
FilefileDir=newFile(savePath);
if(!fileDir.exists()){
//如果文件夾沒有:新建
fileDir.mkdirs();
}
FileOutputStreamfos=null;
try{
fos=newFileOutputStream(filePath);
fos.write(file.getBytes());
fos.flush();
returnResultUtil.success("UPLOAD_SUCCESS",URLEncoder.encode(newFileName,"utf-8"));
}catch(Exceptione){
e.printStackTrace();
returnResultUtil.fail("UPLOAD_ERROR");
}finally{
try{
if(fos!=null){
fos.close();
}
}catch(IOExceptione){
e.printStackTrace();
returnResultUtil.fail("UPLOAD_ERROR");
}
}
}
returnResultUtil.fail("UPLOAD_ERROR");
}}

⑸ java 傳輸 獲取文件類型

獲取文件類型,一般的是列出目前所有的文件類型,根據表頭進行相應判斷,示例如下:

/**
*件頭是位於文件開頭的一段承擔一定任務的數據,一般都在開頭的部分。
*頭文件作為一種包含功能函數、數據介面聲明的載體文件,用於保存程序的聲明(declaration),而定義文件用於保存程序的實現(implementation)。
*為了解決在用戶上傳文件的時候在伺服器端判斷文件類型的問題,故用獲取文件頭的方式,直接讀取文件的前幾個位元組,來判斷上傳文件是否符合格式。具體代碼如下:
*Java代碼:
*
*/
packagecom.yonyou.sud.file;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.util.HashMap;
/**
*獲取和判斷文件頭信息
*
*@authorSud
*
*/
publicclassGetTypeByHead{
//緩存文件頭信息-文件頭信息
publicstaticfinalHashMap<String,String>mFileTypes=newHashMap<String,String>();
static{
//images
mFileTypes.put("FFD8FF","jpg");
mFileTypes.put("89504E47","png");
mFileTypes.put("47494638","gif");
mFileTypes.put("49492A00","tif");
mFileTypes.put("424D","bmp");
//
mFileTypes.put("41433130","dwg");//CAD
mFileTypes.put("38425053","psd");
mFileTypes.put("7B5C727466","rtf");//日記本
mFileTypes.put("3C3F786D6C","xml");
mFileTypes.put("68746D6C3E","html");
mFileTypes.put("44656C69766572792D646174653A","eml");//郵件
mFileTypes.put("D0CF11E0","doc");
mFileTypes.put("5374616E64617264204A","mdb");
mFileTypes.put("252150532D41646F6265","ps");
mFileTypes.put("255044462D312E","pdf");
mFileTypes.put("504B0304","docx");
mFileTypes.put("7221","rar");
mFileTypes.put("57415645","wav");
mFileTypes.put("41564920","avi");
mFileTypes.put("2E524D46","rm");
mFileTypes.put("000001BA","mpg");
mFileTypes.put("000001B3","mpg");
mFileTypes.put("6D6F6F76","mov");
mFileTypes.put("3026B2758E66CF11","asf");
mFileTypes.put("4D546864","mid");
mFileTypes.put("1F8B08","gz");
}
/**
*根據文件路徑獲取文件頭信息
*
*@paramfilePath
*文件路徑
*@return文件頭信息
*/
publicstaticStringgetFileType(StringfilePath){
System.out.println(getFileHeader(filePath));
System.out.println(mFileTypes.get(getFileHeader(filePath)));
returnmFileTypes.get(getFileHeader(filePath));
}
/**
*根據文件路徑獲取文件頭信息
*
*@paramfilePath
*文件路徑
*@return文件頭信息
*/
(StringfilePath){
FileInputStreamis=null;
Stringvalue=null;
try{
is=newFileInputStream(filePath);
byte[]b=newbyte[4];
/*intread()從此輸入流中讀取一個數據位元組。
*intread(byte[]b)從此輸入流中將最多b.length個位元組的數據讀入一個byte數組中。
*intread(byte[]b,intoff,intlen)從此輸入流中將最多len個位元組的數據讀入一個byte數組中。
*/
is.read(b,0,b.length);
value=bytesToHexString(b);
}catch(Exceptione){
}finally{
if(null!=is){
try{
is.close();
}catch(IOExceptione){
}
}
}
returnvalue;
}
/**
*將要讀取文件頭信息的文件的byte數組轉換成string類型表示
*
*@paramsrc
*要讀取文件頭信息的文件的byte數組
*@return文件頭信息
*/
(byte[]src){
StringBuilderbuilder=newStringBuilder();
if(src==null||src.length<=0){
returnnull;
}
Stringhv;
for(inti=0;i<src.length;i++){
//以十六進制(基數16)無符號整數形式返回一個整數參數的字元串表示形式,並轉換為大寫
hv=Integer.toHexString(src[i]&0xFF).toUpperCase();
if(hv.length()<2){
builder.append(0);
}
builder.append(hv);
}
System.out.println(builder.toString());
returnbuilder.toString();
}
publicstaticvoidmain(String[]args)throwsException{
finalStringfileType=getFileType("E:/Java編程思想讀書筆記.docx");
System.out.println(fileType);
}
}

⑹ java後台怎樣規定上傳的圖片的大小格式,

用 BufferedImage sourceImg = javax.imageio.ImageIO.read(in);可得到寬高 sourceImg.getWidth()
sourceImg.getHeight()
File file=new File(path);
ImageIO.write(sourceImg, 後綴, file);
file.length();
將圖片的文件轉為File類型,有length方法可得到文件大小,格式就看文件名的後綴了

熱點內容
編程里的等於 發布:2025-03-20 16:22:02 瀏覽:915
ise綜合與編譯 發布:2025-03-20 16:13:22 瀏覽:878
linux文件句柄 發布:2025-03-20 16:11:55 瀏覽:635
如來神掌原始登錄密碼多少 發布:2025-03-20 16:06:32 瀏覽:319
保電訪問 發布:2025-03-20 16:06:23 瀏覽:144
08年雅閣都有什麼配置 發布:2025-03-20 15:55:48 瀏覽:907
塗鴉解壓法 發布:2025-03-20 15:54:54 瀏覽:676
三國伺服器什麼時候開放 發布:2025-03-20 15:50:43 瀏覽:655
英詩派2022款大改款選哪個配置好 發布:2025-03-20 15:36:50 瀏覽:908
廈門如何找回中招報名的密碼 發布:2025-03-20 15:30:56 瀏覽:959