add ServletUtil 测试违反法 和 注释;方便调用这进行调用;

目前暂时发现:safafi浏览器出现乱码
This commit is contained in:
duandazhi 2021-03-24 15:21:23 +08:00
parent 65a43f5318
commit 0d0f70bf15
2 changed files with 84 additions and 43 deletions

View File

@ -549,6 +549,17 @@ public class ServletUtil {
* @param response 响应对象{@link HttpServletResponse} * @param response 响应对象{@link HttpServletResponse}
* @param in 需要返回客户端的内容 * @param in 需要返回客户端的内容
* @param contentType 返回的类型 * @param contentType 返回的类型
*
* 1application/pdf
* 2application/vnd.ms-excel
* 3application/msword
* 4application/vnd.ms-powerpoint
* docxxlsx 这种 office 2007 格式 设置 MIME;网页里面docx 文件是没问题但是下载下来了之后就变成doc格式了
* https://blog.csdn.net/cyh2260629/article/details/73824760
* 5MIME_EXCELX_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
* 6MIME_PPTX_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
* 7MIME_WORDX_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
* 8MIME_STREAM_TYPE = "application/octet-stream;charset=utf-8"; #原始字节流
* @param fileName 文件名 * @param fileName 文件名
* @since 4.1.15 * @since 4.1.15
*/ */

View File

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