Java Enum

 

Enum

Enum is a special class that represents a group of constants.

1.define enum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.exp.enums;

import java.util.Arrays;
import java.util.List;

public enum ContentTypeEnum {

WORD_FILE_TYPE("word", new String[]{"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}),

EXCEL_FILE_TYPE("excel", new String[]{"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}),

PPT_FILE_TYPE("ppt", new String[]{"application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}),

IMAGE_FILE_TYPE("jpg", new String[]{"image/jpeg", "image/gif", "image/png", "image/bmp"}),

PDF_FILE_TYPE("pdf", new String[]{"application/pdf"});

private String fileType;

private List<String> mimeTypeList;

ContentTypeEnum(String fileType, String[] mimeTypeList) {
this.fileType = fileType;
this.mimeTypeList = Arrays.asList(mimeTypeList);
}

public String getFileType() {
return fileType;
}

public void setFileType(String fileType) {
this.fileType = fileType;
}

public List<String> getMimeTypeList() {
return mimeTypeList;
}

public void setMimeTypeList(List<String> mimeTypeList) {
this.mimeTypeList = mimeTypeList;
}

}
2.Case

Check the uploaded file type, the uploaded file must be a picture or PDF.

1
2
3
4
5
6
7
// MultipartFile multipartFile
String contentType = multipartFile.getContentType();
List<String> pdfFileType = ContentTypeEnum.PDF_FILE_TYPE.getMimeTypeList();
List<String> imageFileType = ContentTypeEnum.IMAGE_FILE_TYPE.getMimeTypeList();
if (!pdfFileType.contains(contentType) && !imageFileType.contains(contentType)) {
return "The uploaded file only can be a picture or PDF.";
}