1. 程式人生 > >response.getWriter().write(0) 前端取到的數字亂碼

response.getWriter().write(0) 前端取到的數字亂碼

response.getWriter().write(0);

原始碼

/**
     * Writes a single character.  The character to be written is contained in
     * the 16 low-order bits of the given integer value; the 16 high-order bits
     * are ignored.
     *
     * <p> Subclasses that intend to support efficient single-character output
     * should override this method.
     *
     * @param  c
     *         int specifying a character to be written
     *
     * @throws  IOException
     *          If an I/O error occurs
     */
    public void write(int c) throws IOException {
        synchronized (lock) {
            if (writeBuffer == null){
                writeBuffer = new char[WRITE_BUFFER_SIZE];
            }
            writeBuffer[0] = (char) c;
            write(writeBuffer, 0, 1);
        }
    }

最終,會將int轉換成char,所以可能會造成亂碼。

解決:

1、使用 response.getWriter().println(0);

2、使用response.getWriter().write("0");