为什么浏览器对有些图片是直接预览打开,对有些图片是直接下载?—— 原由就在
Content-Type
响应头上;
Content-Type响应头的作用:
Content-Type
用于向接收方说明传输资源的媒体类型,从而让浏览器用指定码表去解码。
以下举例说明MinIO上传时如何设置Content-Type
MinIO
上传时参数设置的代码片段
// 上传参数中指定
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucket)
.object(objectName)
// 上传时指定对应对ContentType
.contentType(ViewContentType.getContentType(objectName))
.stream(multipartFile.getInputStream(), multipartFile.getSize(), 1024*1024*5+1)
.build();
列举部分图片的Content-Type
public enum ViewContentType {
DEFAULT("default","application/octet-stream"),
JPG("jpg", "image/jpeg"),
TIFF("tiff", "image/tiff"),
GIF("gif", "image/gif"),
JFIF("jfif", "image/jpeg"),
PNG("png", "image/png"),
TIF("tif", "image/tiff"),
ICO("ico", "image/x-icon"),
JPEG("jpeg", "image/jpeg"),
WBMP("wbmp", "image/vnd.wap.wbmp"),
FAX("fax", "image/fax"),
NET("net", "image/pnetvue"),
JPE("jpe", "image/jpeg"),
RP("rp", "image/vnd.rn-realpix");
private String prefix;
private String type;
public static String getContentType(String prefix){
if(StrUtil.isEmpty(prefix)){
return DEFAULT.getType();
}
prefix = prefix.substring(prefix.lastIndexOf(".") + 1);
for (ViewContentType value : ViewContentType.values()) {
if(prefix.equalsIgnoreCase(value.getPrefix())){
return value.getType();
}
}
return DEFAULT.getType();
}
ViewContentType(String prefix, String type) {
this.prefix = prefix;
this.type = type;
}
public String getPrefix() {
return prefix;
}
public String getType() {
return type;
}
}