使用springmvc創建下載文件時發現只有IE不能成功下載
/**
* 獲取中圖片以下載使用 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