response.setContentType()的作用及参数

x33g5p2x  于2021-12-06 转载在 其他  
字(5.8k)|赞(0)|评价(0)|浏览(472)

response.setContentType作用

response.setContentType(MIME)的作用是使客户端浏览器区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块处理相应的数据

MIME映射策略就是在网页中使用哪个应用程序(即插件),打开哪种文件。

例如web浏览器就是通过MIME类型来判断文件是PNG图片。通过MIME类型来处理json字符串。

请看如下代码案例:

@GetMapping("getImage")
public void getPngImage(HttpServletResponse response) throws IOException {
    // 利用 输入流从磁盘中读取一个jpg的图片
    String imagePath = "E:\\壁纸\\8b82b9014a90f6030add233a3b12b31bb051ed5a.jpg";
    FileInputStream in = new FileInputStream(imagePath);
    // 设置响应给客户端的文件格式
    response.setContentType("image/jpeg");
    // 获取响应体的输出流
    ServletOutputStream os = response.getOutputStream();
    // 用 BufferedOutputStream(处理流/包装流)来包装上面那个输出流
    BufferedOutputStream bos = new BufferedOutputStream(os);
    byte[] buff = new byte[1024];
    int len = 0;
    // 输入流读入字节码内容
    while ((len = in.read(buff, 0, 1024)) != -1) {
        // 输出流写出字节码内容
        bos.write(buff, 0, len);
    }
    // 关闭输出流
    if (bos != null)
        bos.close();
    // 关闭输入流
    if (in != null)
        bos.close();
}

这是一个controller层的一个接口
该接口首先从本地磁盘读取一个jpg格式的图片,最后以流的形式返回给客户端

重点看这个代码:

// 设置响应给客户端的文件格式
response.setContentType("image/jpeg");

此时我设置的response响应格式为image/jpeg,咱们请求一下接口看看浏览器解析的数据

可以看出浏览器解析出来的就是一个图片,很正确

接下来我们修改response.setContentType 为 audio/mp3(音频)

// 设置响应给客户端的文件格式
response.setContentType("audio/mp3");

可以看出,此时浏览器是将文件以音频的格式解析的,但是该文件本身是一个图片,因此什么声音也没有

改成pdf格式

// 设置响应给客户端的文件格式
response.setContentType("application/pdf");

原理和上面那个一样,我就不解释了

相信根据上面的案例大家应该明白response.setContentType怎么玩的了,接下来我为大家总结一些 文件格式与content-type的对应数据

content-type对照表

文件扩展名Content-Type(Mime-Type)
.*( 二进制流,不知道下载文件类型)application/octet-stream
.tifimage/tiff
.001application/x-001
.301application/x-301
.323text/h323
.906application/x-906
.907drawing/907
.a11application/x-a11
.acpaudio/x-mei-aac
.aiapplication/postscript
.aifaudio/aiff
.aifcaudio/aiff
.aiffaudio/aiff
.anvapplication/x-anv
.asatext/asa
.asfvideo/x-ms-asf
.asptext/asp
.asxvideo/x-ms-asf
.auaudio/basic
.avivideo/avi
.awfapplication/vnd.adobe.workflow
.biztext/xml
.bmpapplication/x-bmp
.botapplication/x-bot
.c4tapplication/x-c4t
.c90application/x-c90
.calapplication/x-cals
.catapplication/vnd.ms-pki.seccat
.cdfapplication/x-netcdf
.cdrapplication/x-cdr
.celapplication/x-cel
.cerapplication/x-x509-ca-cert
.cg4application/x-g4
.cgmapplication/x-cgm
.citapplication/x-cit
.classjava/*
.cmltext/xml
.cmpapplication/x-cmp
.cmxapplication/x-cmx
.cotapplication/x-cot
.crlapplication/pkix-crl
.crtapplication/x-x509-ca-cert
.csiapplication/x-csi
.csstext/css
.cutapplication/x-cut
.dbfapplication/x-dbf
.dbmapplication/x-dbm
.dbxapplication/x-dbx
.dcdtext/xml
.dcxapplication/x-dcx
.derapplication/x-x509-ca-cert
.dgnapplication/x-dgn
.dibapplication/x-dib
.dllapplication/x-msdownload
.docapplication/msword
.dotapplication/msword
.drwapplication/x-drw
.dtdapplication/xml-dtd
.dwfModel/vnd.dwf
.dwfapplication/x-dwf
.dwgapplication/x-dwg
.dxbapplication/x-dxb
.dxfapplication/x-dxf
.ednapplication/vnd.adobe.edn
.emfapplication/x-emf
.emlmessage/rfc822
.enttext/xml
.epiapplication/x-epi
.epsapplication/x-ps
.epsapplication/postscript
.etdapplication/x-ebx
.exeapplication/x-msdownload
.faximage/fax
.fdfapplication/vnd.fdf
.fifapplication/fractals
.fotext/xml
.frmapplication/x-frm
.g4application/x-g4
.gbrapplication/x-gbr
.gifimage/gif
.gl2application/x-gl2
.gp4application/x-gp4
.hglapplication/x-hgl
.hmrapplication/x-hmr
.hpgapplication/x-hpgl
.hplapplication/x-hpl
.hqxapplication/mac-binhex40
.hrfapplication/x-hrf
.htaapplication/hta
.htctext/x-component
.htmtext/html
.htmltext/html
.htttext/webviewhtml
.htxtext/html
.icbapplication/x-icb
.icoimage/x-icon
.icoapplication/x-ico
.iffapplication/x-iff
.ig4application/x-g4
.igsapplication/x-igs
.iiiapplication/x-iphone
.imgapplication/x-img
.insapplication/x-internet-signup
.ispapplication/x-internet-signup
.IVFvideo/x-ivf
.javajava/*
.jfifimage/jpeg
.jpeimage/jpeg
.jpeapplication/x-jpe
.jpegimage/jpeg
.jpgimage/jpeg
.jpgapplication/x-jpg
.jsapplication/x-javascript
.jsptext/html
.la1audio/x-liquid-file
.larapplication/x-laplayer-reg
.latexapplication/x-latex
.lavsaudio/x-liquid-secure
.lbmapplication/x-lbm
.lmsffaudio/x-la-lms
.lsapplication/x-javascript
.ltrapplication/x-ltr
.m1vvideo/x-mpeg
.m2vvideo/x-mpeg
.m3uaudio/mpegurl
.m4evideo/mpeg4
.macapplication/x-mac
.manapplication/x-troff-man
.mathtext/xml
.mdbapplication/msaccess
.mdbapplication/x-mdb
.mfpapplication/x-shockwave-flash
.mhtmessage/rfc822
.mhtmlmessage/rfc822
.miapplication/x-mi
.midaudio/mid
.midiaudio/mid
.milapplication/x-mil
.mmltext/xml
.mndaudio/x-musicnet-download
.mnsaudio/x-musicnet-stream
.mochaapplication/x-javascript
.movievideo/x-sgi-movie
.mp1audio/mp1
.mp2audio/mp2
.mp2vvideo/mpeg
.mp3audio/mp3
.mp4video/mpeg4
.mpavideo/x-mpg
.mpdapplication/vnd.ms-project
.mpevideo/x-mpeg
.mpegvideo/mpg
.mpgvideo/mpg
.mpgaaudio/rn-mpeg
.mppapplication/vnd.ms-project
.mpsvideo/x-mpeg
.mptapplication/vnd.ms-project
.mpvvideo/mpg
.mpv2video/mpeg
.mpwapplication/vnd.ms-project
.mpxapplication/vnd.ms-project
.mtxtext/xml
.mxpapplication/x-mmxp
.nrfapplication/x-nrf
.nwsmessage/rfc822
.odctext/x-ms-odc
.outapplication/x-out
.p10application/pkcs10
.p12application/x-pkcs12
.pciapplication/x-pci
.pdfapplication/pdf
.pdxapplication/vnd.adobe.pdx
.pngimage/png
.pngapplication/x-png
.potapplication/vnd.ms-powerpoint
.pptapplication/vnd.ms-powerpoint
.c4tapplication/x-c4t
.pptapplication/x-ppt
.prapplication/x-pr
.prfapplication/pics-rules
.psapplication/x-ps
.psapplication/postscript
.ratapplication/rat-file
.rdftext/xml
.rgbapplication/x-rgb
.rmapplication/vnd.rn-realmedia
.rmfapplication/vnd.adobe.rmf
.smilapplication/smil
.stlapplication/vnd.ms-pki.stl
.svgtext/xml
.svgimage/svg+xml
.tsdtext/xml
.txttext/plain
.vssapplication/vnd.visio
.vstapplication/vnd.visio
.wavaudio/wav
.wb1application/x-wb1
.wsapplication/x-ws
.wsdltext/xml
.xhtmltext/html
.xlsapplication/vnd.ms-excel
.xlsapplication/x-xls
.xlwapplication/x-xlw
.xsltext/xml
.ipaapplication/vnd.iphone
.apkapplication/vnd.android.package-archive
.xapapplication/x-silverlight-app
.zapplication/x-compress
.zabwapplication/x-abiword
.zipapplication/zip
.zooapplication/x-zoo

参考优秀文章

【 人工智能 content-type对照表 】
【一文彻底读懂response.setContentType()的作用及参数】

相关文章