add JakartaServletUtil

This commit is contained in:
Looly
2022-04-23 10:31:17 +08:00
parent 7ad9253250
commit fe0f5527f6
6 changed files with 676 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
package cn.hutool.extra.servlet;
import cn.hutool.core.util.StrUtil;
import org.junit.Ignore;
import org.junit.Test;
@@ -11,8 +12,8 @@ import java.nio.charset.StandardCharsets;
* ServletUtil工具类测试
*
* @author dazer
* @date 2021/3/24 15:02
* @see ServletUtil
* @see JakartaServletUtil
*/
public class ServletUtilTest {
@@ -20,7 +21,7 @@ public class ServletUtilTest {
@Ignore
public void writeTest() {
HttpServletResponse response = null;
byte[] bytes = "地球是我们共同的家园,需要大家珍惜.".getBytes(StandardCharsets.UTF_8);
byte[] bytes = StrUtil.utf8Bytes("地球是我们共同的家园,需要大家珍惜.");
//下载文件
// 这里没法直接测试,直接写到这里,方便调用;
@@ -32,4 +33,21 @@ public class ServletUtilTest {
ServletUtil.write(response, new ByteArrayInputStream(bytes), contentType, fileName);
}
}
@Test
@Ignore
public void jakartaWriteTest() {
jakarta.servlet.http.HttpServletResponse response = null;
byte[] bytes = StrUtil.utf8Bytes("地球是我们共同的家园,需要大家珍惜.");
//下载文件
// 这里没法直接测试,直接写到这里,方便调用;
//noinspection ConstantConditions
if (response != null) {
String fileName = "签名文件.pdf";
String contentType = "application/pdf";// application/octet-stream、image/jpeg、image/gif
response.setCharacterEncoding(StandardCharsets.UTF_8.name()); // 必须设置否则乱码; 但是 safari乱码
JakartaServletUtil.write(response, new ByteArrayInputStream(bytes), contentType, fileName);
}
}
}