做一个下载的功能,但始终乱码,怎么调也好不了,返回ResponseEntity前打印内容正常,并且mock的测试返回的也正常
RequestBuilder request = MockMvcRequestBuilders.get("/export");
String res = mockMvc.perform(request).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
后来发现原来是springboot在解析string类型的时候不对,解决方法如下:
增加对byte和string的解析器
@Configuration public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter { private static final Logger logger = LoggerFactory.getLogger(MyWebMvcConfigurerAdapter.class); @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); logger.info("add string and byte array converter"); StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(); stringConverter.setDefaultCharset(Charset.forName("UTF-8")); converters.add(stringConverter); converters.add(new ByteArrayHttpMessageConverter()); } }
还要注意的是,在做下载的时候
文件名乱码解决方法
new String(fileName.getBytes("UTF-8"), "iso-8859-1")
CSV类型下载需要添加头信息
HttpHeaders headers = new HttpHeaders(); headers.set("Content-disposition", "attachment; filename=" + fileName); headers.set("Content-type", "text/plain;charset=UTF-8"); response = new ResponseEntity<String>("\uFEFF" + file, headers, HttpStatus.OK);
其他编码注意事项:见内容及结果串
//windows自身采用的编码格式是gbk(而gbk和gb2312基本上是一样的编码方式),而IDE中Encode不修改的话,默认是utf-8的编码 // File file = new File("中文文件名.csv"); // //流编码测试,文件中文内容 // OutputStreamWriter bis = new OutputStreamWriter(new FileOutputStream(file), "gbk"); // BufferedWriter bw = new BufferedWriter(bis); // bw.write("文件中文内容"); // bw.flush(); //------------------------------------------------------------------------------------- // File file1 = new File("中文文件名.csv"); // //流编码测试,鏂囦欢涓枃鍐呭 // OutputStreamWriter bis1 = new OutputStreamWriter(new FileOutputStream(file1), "utf-8"); // BufferedWriter bw1 = new BufferedWriter(bis1); // bw1.write("文件中文内容"); // bw1.flush(); //------------------------------------------------------------------------------------- // File file1 = new File("中文文件名.csv"); // //流编码测试,?????? // OutputStreamWriter bis1 = new OutputStreamWriter(new FileOutputStream(file1), "iso-8859-1"); // BufferedWriter bw1 = new BufferedWriter(bis1); // bw1.write("文件中文内容"); // bw1.flush(); //------------------------------------------------------------------------------------- // File file1 = new File("中文文件名.csv"); // //流编码测试,文件中文内容 // OutputStreamWriter bis1 = new OutputStreamWriter(new FileOutputStream(file1), "gb2312"); // BufferedWriter bw1 = new BufferedWriter(bis1); // bw1.write("文件中文内容"); // bw1.flush();
日期问题:
Long类型,分为秒的和毫秒的,注意区分
String类型,分为UTC类型和一般日期类型
UTC类型举例:https://zhidao.baidu.com/question/585516457.html 这个牵扯到时区,要注意可能这里不对
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); Date date = sdf.parse(begin);
一般日期类型举例:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(dateString);
日期转String
String dateStr = df.format(new Date());
long longTime = new Date().getTime(); System.out.println("字符串类型的Long日期转换成日期:"); String str = "1498457677473"; Long dateLong = Long.valueOf(str); System.out.println("longToDate:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(dateLong))); System.out.println("Date和Long互转:"); System.out.println("日期转换成Long:"+longTime); System.out.println("Long转换成日期:"+new Date(longTime));