Mime Type-the server verifies the type of the uploaded file

Source: Internet
Author: User
Tags gtar

The role of mime: Enable the client software to distinguish different types of data. For example, a Web browser uses the MIME type to determine whether a file is a GIF image or a printable postscript file.

The Web server uses mime to describe the type of data sent. The Web client uses mime to describe the type of data to be received.

The tomcat installation directory/CONF/Web. xml defines a large number of MIME types. You can also take a look.

 

I recently used a form to upload files. To verify the file type on the server, I only allowed to upload GIF, JPG, and zip files. We have two methods,

First, check the file extension. Second, check the MIME type of the file.

The method to check the file extension is simple and quick, but you can bypass the check and upload by renaming a. jsp to a.jpg.

The method for checking the MIME type of a file is a little different in IE7 and Firefox (see the table below), and the upload performance of different browsers is inconsistent. The MIME types of zip and exe files in Firefox are the same as application/octet-stream.

 

Therefore, I decided to use the two methods in combination, first check the extension, and then check the mime. Unqualified files are not allowed to be uploaded.

 

The following table lists the MIME types obtained when the server (tomcat5.5) receives files uploaded by different browsers.

 

  Upload with IE7 Upload with firefox3.0
GIF

Image/GIF

Image/GIF

JPG

Image/pjpeg

Image/JPEG

Zip Application/X-compressed Application/octet-stream
JSP

Text/html

Text/html

EXE Application/octet-stream Application/octet-stream

 

 

  Upload with IE7 Upload with firefox3.0
GIF Image/GIF Image/GIF
JPG Image/pjpeg Image/JPEG
Zip Application/X-compressed Application/octet-stream
JSP Text/html Text/html
EXE Application/octet-stream Application/octet-stream

 

 

 

Common MIME type example tables:

 

Serial number

Content type

File Extension

Description

1

Application/MSWord

Doc

Microsoft Word

2

Application/octet-stream Bin

Dms lha lzh EXE class

Executable program

3

Application/PDF

PDF

Adobe Acrobat

4

Application/postscript

AI EPS PS

Postscript

5

APPICATION/PowerPoint

PPT

Microsoft PowerPoint

6

APPICATION/RTF

RTF

RTF Format

7

APPICATION/X-compress

Z

UNIX compressed file

8

Application/X-Gzip

GZ

Gzip

9

Application/X-gtar

Gtar

Tar document (GNU format)

10

Application/X-Shockwave-flash

SWF

Macromedia Flash

11

Application/X-Tar

Tar

Tar (4.3bsd)

12

Application/zip

Zip

WinZip

13

Audio/basic

AU SND

Sun/next sound file

14

Audio/MPEG

MPEG MP2

MPEG sound file

15

Audio/X-AIFF

Mid MIDI RMF

MIDI format

16

Audio/X-PN-RealAudio

Ram Ra

Real Audio voice

17

Audio/X-PN-RealAudio-plugin

Rpm

Real Audio plugin

18

Audio/X-WAV

Wav

Microsoft Windows sound

19

Image/CGM

CGM

Computer Graphics Metafile

20

Image/GIF

GIF

CompuServe GIF Image

21

Image/JPEG

Jpeg jpg jpe

JPEG Image

22

Image/PNG

PNG

PNG Image

 

Upload with IE7 Upload with firefox3.0
GIF

Image/GIF

Image/GIF

JPG

Image/pjpeg

Image/JPEG

Zip Application/X-compressed Application/octet-stream
JSP

Text/html

Text/html

EXE Application/octet-stream Application/octet-stream

 

Serial number Content type File Extension Description
1 Application/MSWord Doc Microsoft Word
2 Application/octet-stream Bin Dms lha lzh EXE class Executable program
3 Application/PDF PDF Adobe Acrobat
4 Application/postscript AI EPS PS Postscript
5 APPICATION/PowerPoint PPT Microsoft PowerPoint
6 APPICATION/RTF RTF RTF Format
7 APPICATION/X-compress Z UNIX compressed file
8 Application/X-Gzip GZ Gzip
9 Application/X-gtar Gtar Tar document (GNU format)
10 Application/X-Shockwave-flash SWF Macromedia Flash
11 Application/X-Tar Tar Tar (4.3bsd)
12 Application/zip Zip WinZip
13 Audio/basic AU SND Sun/next sound file
14 Audio/MPEG MPEG MP2 MPEG sound file
15 Audio/X-AIFF Mid MIDI RMF MIDI format
16 Audio/X-PN-RealAudio Ram Ra Real Audio voice
17 Audio/X-PN-RealAudio-plugin Rpm Real Audio plugin
18 Audio/X-WAV Wav Microsoft Windows sound
19 Image/CGM CGM Computer Graphics Metafile
20 Image/GIF GIF CompuServe GIF Image
21 Image/JPEG Jpeg jpg jpe JPEG Image
22 Image/PNG PNG PNG Image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.