Class ResponseHelper
-
- All Implemented Interfaces:
public class ResponseHelper- Since:
2021-03-10
trydofor
-
-
Constructor Summary
Constructors Constructor Description ResponseHelper()
-
Method Summary
Modifier and Type Method Description static StringgetDownloadContentType(String fileName)Get the correct ContentType for the download filename. static StringgetDownloadContentDisposition(@Nullable() String fileName)Get the correct ContentDisposition for the download filename. static voidsetDownloadContentType(@NotNull() HttpServletResponse response, @Nullable() String fileName)Set the correct ContentType to Response for the download filename. static voidsetDownloadContentDisposition(@NotNull() HttpServletResponse response, @Nullable() String fileName)Set the correct Content-Disposition to Response for the download filename. static voiddownloadFile(@NotNull() HttpServletResponse response, @Nullable() String fileName, @NotNull() @WillClose() InputStream stream)Directly download file with filename by HttpServletResponse, use stream as the content and close the stream. static OutputStreamdownloadFile(@NotNull() HttpServletResponse response, @Nullable() String fileName)Set the download filename to HttpServletResponse and return the output stream for user to write the content. static voiddownloadFile(@NotNull() HttpServletResponse response, @NotNull() File file)Directly download the file with its filename by HttpServletResponse static voiddownloadFileWithZip(@NotNull() HttpServletResponse response, @NotNull() @WillClose() Map<String, InputStream> files, @Nullable() String fileName)static voidpreviewPDF(@NotNull() HttpServletResponse response, @Nullable() String fileName, @NotNull() @WillClose() InputStream stream)Directly preview the PDF by HttpServletResponse static voidshowCaptcha(HttpServletResponse response, String code)Output the image/jpeg captcha static voidshowCaptcha(HttpServletResponse response, String code, String fmt)Output the base64 image captcha in text/plain. static voidbothHeadCookie(HttpServletResponse response, String key, String value, int second)static voidwriteBodyUtf8(HttpServletResponse response, String body)static InputStreamtryCachingOutputStream(ServletResponse response)static voidrenderModelAndView(ModelAndView mav, HttpServletResponse res, HttpServletRequest req)static ResponseEntity<String>flatResponse(Map<String, out Object> data, String accept, String uri)Execute in the following order and return 3 forms, the Accept header strictly matches, as the browser sends multiple headers. (1) 200 xml, accept=application/xml (2) 200 json, accept=application/json or uri=null or (3) if not match (3) 302 uri, uri != null-
-
Method Detail
-
getDownloadContentType
@NotNull() static String getDownloadContentType(String fileName)
Get the correct ContentType for the download filename.
- Parameters:
fileName- File name prompted during download- Returns:
ContentType, default APPLICATION_OCTET_STREAM_VALUE
-
getDownloadContentDisposition
@NotNull() static String getDownloadContentDisposition(@Nullable() String fileName)
Get the correct ContentDisposition for the download filename. see Content-Disposition
- Parameters:
fileName- File name prompted during download- Returns:
ContentDisposition
-
setDownloadContentType
static void setDownloadContentType(@NotNull() HttpServletResponse response, @Nullable() String fileName)
Set the correct ContentType to Response for the download filename.
- Parameters:
response- HttpServletResponsefileName- File name prompted during download
-
setDownloadContentDisposition
static void setDownloadContentDisposition(@NotNull() HttpServletResponse response, @Nullable() String fileName)
Set the correct Content-Disposition to Response for the download filename.
- Parameters:
response- HttpServletResponsefileName- File name prompted during download
-
downloadFile
static void downloadFile(@NotNull() HttpServletResponse response, @Nullable() String fileName, @NotNull() @WillClose() InputStream stream)
Directly download file with filename by HttpServletResponse, use stream as the content and close the stream.
- Parameters:
response- HttpServletResponsefileName- File name prompted during downloadstream- input stream
-
downloadFile
static OutputStream downloadFile(@NotNull() HttpServletResponse response, @Nullable() String fileName)
Set the download filename to HttpServletResponse and return the output stream for user to write the content.
- Parameters:
response- HttpServletResponsefileName- File name prompted during download
-
downloadFile
static void downloadFile(@NotNull() HttpServletResponse response, @NotNull() File file)
Directly download the file with its filename by HttpServletResponse
- Parameters:
response- HttpServletResponsefile- file and filename to download
-
downloadFileWithZip
static void downloadFileWithZip(@NotNull() HttpServletResponse response, @NotNull() @WillClose() Map<String, InputStream> files, @Nullable() String fileName)
-
previewPDF
static void previewPDF(@NotNull() HttpServletResponse response, @Nullable() String fileName, @NotNull() @WillClose() InputStream stream)
Directly preview the PDF by HttpServletResponse
- Parameters:
response- HttpServletResponsefileName- the PDF filename to previewstream- input stream
-
showCaptcha
static void showCaptcha(HttpServletResponse response, String code)
Output the image/jpeg captcha
- Parameters:
response- responsecode- captcha
-
showCaptcha
static void showCaptcha(HttpServletResponse response, String code, String fmt)
Output the base64 image captcha in text/plain.
- Parameters:
response- responsecode- captchafmt- format, `{base64}` is placeholder
-
bothHeadCookie
static void bothHeadCookie(HttpServletResponse response, String key, String value, int second)
-
writeBodyUtf8
static void writeBodyUtf8(HttpServletResponse response, String body)
-
tryCachingOutputStream
static InputStream tryCachingOutputStream(ServletResponse response)
-
renderModelAndView
static void renderModelAndView(ModelAndView mav, HttpServletResponse res, HttpServletRequest req)
-
flatResponse
@NotNull() static ResponseEntity<String> flatResponse(Map<String, out Object> data, String accept, String uri)
Execute in the following order and return 3 forms, the Accept header strictly matches, as the browser sends multiple headers. (1) 200 xml, accept=application/xml (2) 200 json, accept=application/json or uri=null or (3) if not match (3) 302 uri, uri != null
-
-
-
-