為什么瀏覽器對有些圖片是直接預(yù)覽打開拇勃,對有些圖片是直接下載?—— 原由就在
Content-Type
響應(yīng)頭上缀踪;
Content-Type響應(yīng)頭的作用:
Content-Type
用于向接收方說明傳輸資源的媒體類型虐译,從而讓瀏覽器用指定碼表去解碼。
以下舉例說明MinIO上傳時如何設(shè)置Content-Type
MinIO
上傳時參數(shù)設(shè)置的代碼片段
// 上傳參數(shù)中指定
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucket)
.object(objectName)
// 上傳時指定對應(yīng)對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;
}
}