forked from JoyChou93/java-sec-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebUtils.java
More file actions
51 lines (37 loc) · 1.72 KB
/
WebUtils.java
File metadata and controls
51 lines (37 loc) · 1.72 KB
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
44
45
46
47
48
49
50
51
package org.joychou.util;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import com.google.common.base.Preconditions;
import org.springframework.web.util.HtmlUtils;
public class WebUtils {
// Get request body.
public static String getRequestBody(HttpServletRequest request) throws IOException {
InputStream in = request.getInputStream();
return convertStreamToString(in);
}
// https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java
public static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
public static String getCookieValueByName(HttpServletRequest request, String cookieName) {
Cookie cookie = org.springframework.web.util.WebUtils.getCookie(request, cookieName);
return cookie == null ? null : cookie.getValue();
}
public static String json2Jsonp(String callback, String jsonStr) {
return HtmlUtils.htmlEscape(callback) + "(" + jsonStr + ")";
}
public static String getFileExtension(String fullName) {
Preconditions.checkNotNull(fullName);
String fileName = (new File(fullName)).getName();
int dotIndex = fileName.lastIndexOf('.');
return dotIndex == -1 ? "" : fileName.substring(dotIndex + 1);
}
public static String getNameWithoutExtension(String file) {
Preconditions.checkNotNull(file);
String fileName = (new File(file)).getName();
int dotIndex = fileName.lastIndexOf('.');
return dotIndex == -1 ? fileName : fileName.substring(0, dotIndex);
}
}