? ? ? 使用springmvc创建下载文件时发现只有IE不能成功下载
? ? ??
class="java">/**
* 获取中图片以下载使用 260*260
*
* @return
* @throws IOException
*/
@RequestMapping("/download")
public ResponseEntity<byte[]> download(Long id, HttpServletRequest request) throws Exception {
Order order = orderService.find(id);
String code = order.getOrderDimensional().getTwoDimensionalCode();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
MatrixToImageWriter.buildToOutputStream(code, 260, 260, out);
ByteArrayInputStream input = new ByteArrayInputStream(out.toByteArray());
Font font = new Font("微软雅黑", Font.BOLD, 20);
out = new ByteArrayOutputStream();
//260*260 图片允许最多数字字符串:21 font 20px
int x = 0;
if (code.length() <= 21 && code.length() >= 0) {
x = (21 - code.length()) / 2 * 12;
}
orderDimensionalService.addCodeView(input, out, code, 20, x, 260, font, 100);
HttpHeaders headers = new HttpHeaders();
String fileName=new String("订单二维码.png".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
byte[] bytes = out.toByteArray();
input.close();
out.close();
return new ResponseEntity<byte[]>(bytes,
headers, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
原因是IE对状态码:201不支持,可以修改为200.
?
? ? 在Java代码中表现为?HttpStatus.CREATED 修改为?HttpStatus.OK