Java Spring: 如何将十六进制格式的 HTML 字符代码从文本区域转换为纯文本?

Java Spring: How convert HTML character codes in hex format from textarea to plain text?

我有一个用 React JS 编写的 Web 应用程序和 Java Spring 启动。在我的 Board 组件中,我有带有文本区域和按钮的表单。当我单击按钮进行调试时,我将重定向到 UserController spring 项目中的 PostMapping。我的方法有一个参数。这是 @RequestBody 字符串查询。 我以十六进制代码的 HTML 字符代码从 textarea 获取文本。我需要从这个字符串中提取纯文本。

我得到了这样的东西:

CREATE+TABLE+users+%28%0A%09id+INT%2C%0A%09fullName+VARCHAR%28220%29+NOT+NULL%2C%0A%09city+VARCHAR%28120%29+ NOT+NULL%2C%0A%09country+VARCHAR%2860%29+NOT+NULL%2C%0A%09PRIMARY+KEY%28id%29%0A%29%3 ...

其中 + 表示 space

我正在尝试解决这个问题。 没有任何效果。

第一种方式:

byte[] s = DatatypeConverter.parseHexBinary(query);
System.out.println(new String(s, "UTF-8"));

第二种方式: Apache Commons 编解码器 - 十六进制

byte[] bytes = Hex.decodeHex(query.toCharArray());
System.out.println(new String(bytes, "UTF-8"));

这是我的代码

Spring 项目:

用户控制器class

@Controller
@RequestMapping("fiddle")
public class MainController {

@PostMapping
public ResponseEntity<?> processingQueries(@RequestBody String query) {
    System.out.println(query);

    return new ResponseEntity<String>("Query prcessed successfully.", HttpStatus.OK);
}

}

React JS 项目:

板组件

import React from 'react';
import TableButton from './TableButton';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { processQueries } from '../actions/queryActions';

class Board extends React.Component {
constructor() {
    super();

    this.state = {
        query: 'Write here your SQL query...'
    }
    this.onChange = this.onChange.bind(this);
    this.resetField = this.resetField.bind(this);
    this.onSubmitRun = this.onSubmitRun.bind(this);
}

onChange(e) {
    this.setState({ [e.target.name]: e.target.value });
}

resetField(e) {
    this.setState({ query: '' });
}

onSubmitRun(e) {
    e.preventDefault();
    console.log(this.state.query);
    this.props.processQueries(this.state.query, this.props.history);
}

render() {
    return (
        <div className="box flex-stretch">
            <div className="blue smallClass">
                <TableButton />
            </div>
            <div className="mediumClass">
                <form onSubmit={this.onSubmitRun}>
                    <textarea 
                        name="query" 
                        className="txtArea"
                        value={this.state.query}
                        onChange={this.onChange}
                        onClick={this.resetField}
                        rows="27"
                    >
                        Write here your SQL queries...
                    </textarea>
                    <input type="submit" value="Run" className="runButton"/>
                </form>
            </div>
            <div className="red largeClass">
                One of three columns
            </div>
        </div>
    );
}
}

Board.propTypes = {
query: PropTypes.string
}

const mapStateToProps = state => ({
query: state.query
})

export default connect(mapStateToProps, { processQueries })(Board);

queryReducer

import { PROCESS_QUERY } from '../actions/types';

const initialState = {
query: ''
}

export default function(state = initialState, action) {
switch(action.type) {
    case PROCESS_QUERY:
        return {
            ...state,
            query: action.payload
        }
    default:
        return state;
}
}

queryActions

import axios from 'axios';
import { GET_ERRORS, PROCESS_QUERY } from './types';

export const processQueries = (query, history) => async dispatch => 
{
try {
    console.log(query);
    await axios.post("/fiddle", query);
    history.push("/fiddle");

    dispatch({
        type: PROCESS_QUERY,
        payload: ''
    })
} catch(error) {
    dispatch({
        type: GET_ERRORS,
        payload: error.response.data
    })
}
}

我需要将此字符串从文本区域转换为纯文本。插入到 textarea 的数据是计划 SQL 查询。

所有你需要用UrlDecoder解码字符串。

String result = java.net.URLDecoder.decode(query, StandardCharsets.UTF_8.displayName());