| 105 | |
| 106 | |
| 107 | public void write() { |
| 108 | //默认返回OK |
| 109 | if(this.headerAppender.toString().length() == 0){ |
| 110 | header(HTTPStatus.OK); |
| 111 | } |
| 112 | |
| 113 | //如果是多次使用print或println构建的响应体,而非一次性传入 |
| 114 | if(body == null){ |
| 115 | log.info("多次使用print或println构建的响应体"); |
| 116 | body(bodyAppender.toString().getBytes(CharsetProperties.UTF_8_CHARSET)); |
| 117 | } |
| 118 | |
| 119 | byte[] header = this.headerAppender.toString().getBytes(UTF_8_CHARSET); |
| 120 | |
| 121 | //生成响应报文 |
| 122 | byte[] response = new byte[header.length + body.length]; |
| 123 | System.arraycopy(header, 0, response, 0, header.length); |
| 124 | System.arraycopy(body, 0, response, header.length, body.length); |
| 125 | try { |
| 126 | os.write(response); |
| 127 | os.flush(); |
| 128 | } catch (IOException e) { |
| 129 | e.printStackTrace(); |
| 130 | } finally { |
| 131 | try { |
| 132 | os.close(); |
| 133 | } catch (IOException e) { |
| 134 | e.printStackTrace(); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public void sendRedirect(String url){ |
| 140 | log.info("重定向至{}",url); |