Browse Source

添加 text/event-stream,支持 chat gpt 打字输出体验

James 2 years ago
parent
commit
8403052129
1 changed files with 16 additions and 13 deletions
  1. 16 13
      src/main/java/com/jfinal/render/ContentType.java

+ 16 - 13
src/main/java/com/jfinal/render/ContentType.java

@@ -27,31 +27,32 @@ import java.util.Map;
  * http://tool.oschina.net/commons
  */
 public enum ContentType {
-	
+
 	TEXT("text/plain"),
 	HTML("text/html"),
 	XML("text/xml"),
 	JSON("application/json"),
-	JAVASCRIPT("application/javascript");
-	
+	JAVASCRIPT("application/javascript"),
+	EVENTSTREAM("text/event-stream");
+
 	private final String value;
-	
+
 	private ContentType(String value) {
 		this.value = value;
 	}
-	
+
 	public String value() {
 		return value;
 	}
-	
+
 	public String toString() {
 		return value;
 	}
-	
+
 	// ---------
-	
+
 	private static final Map<String, ContentType> mapping = initMapping();
-	
+
 	/**
 	 * 将简写的文本射到 context type,方便在 Controller.renderText(String, String)
 	 * 之中取用,例如:
@@ -61,7 +62,7 @@ public enum ContentType {
 	 */
 	private static Map<String, ContentType> initMapping() {
 		Map<String, ContentType> ret = new HashMap<>();
-		
+
 		ret.put("text", TEXT);
 		ret.put("plain", TEXT);
 		ret.put("html", HTML);
@@ -69,7 +70,8 @@ public enum ContentType {
 		ret.put("json", JSON);
 		ret.put("javascript", JAVASCRIPT);
 		ret.put("js", JAVASCRIPT);
-		
+		ret.put("eventStream", EVENTSTREAM);
+
 		ret.put("TEXT", TEXT);
 		ret.put("PLAIN", TEXT);
 		ret.put("HTML", HTML);
@@ -77,10 +79,11 @@ public enum ContentType {
 		ret.put("JSON", JSON);
 		ret.put("JAVASCRIPT", JAVASCRIPT);
 		ret.put("JS", JAVASCRIPT);
-		
+		ret.put("EVENTSTREAM", EVENTSTREAM);
+
 		return ret;
 	}
-	
+
 	public static ContentType parse(String str) {
 		return mapping.get(str);
 	}