擷取遠程html插入到本地的網頁裡面(java+react實現)__html

來源:互聯網
上載者:User

需求: 通過編輯器產生的html被上傳到阿里雲的伺服器,但是這個html 需要嵌入到另外的一個網頁裡面。 環境 前端: react 後端: Java Java代碼

@RequestMapping(value = "/getHtml", method = RequestMethod.POST)@UnNeedAdminId@Unauthorizepublic Object getHtml(@RequestBody Map<String, String> uri) throws Exception {    URL url = new URL(uri.get("url"));    URLConnection URLconnection = url.openConnection();    HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;    int responseCode = httpConnection.getResponseCode();    String str = "";    if (responseCode == HttpURLConnection.HTTP_OK) {        InputStream in = httpConnection.getInputStream();        InputStreamReader isr = new InputStreamReader(in);        BufferedReader bufr = new BufferedReader(isr);        String lStr;        while ((lStr = bufr.readLine()) != null) {            str = str + lStr;        }        bufr.close();    } else {        str = "沒有內容";    }    return str;}
react 代碼
export default class PHomeLog extends React.Component {  static defaultProps = {};  constructor(props) {    super(props);    this.state = {      siteContent: ""    }  }  componentWillMount() {    let id = this.props.id;    let url = `http://XXXXXXX/${id}/XXX.html`;    Fetch({      host: "XXX",      url: 'api/getHtml',      body: {url},      method: "POST",    }).then((res)=>{      this.setState({        siteContent: res.data      })    })  }  createMarkup = (html) => {    return {__html: html};  };    // 替換返回的html中被轉義的字元  escapeChars=(str)=> {    str = str.replace(/&amp;/g, '&');    str = str.replace(/&lt;/g, '<');    str = str.replace(/&gt;/g, '>');    str = str.replace(/&acute;/g, '\'');    str = str.replace(/&quot;/g, '"');    str = str.replace(/&brvbar;/g, '\\|');    str = str.replace(/&#x27;/g, '\'');    return str;  }  render() {    let {siteContent} = this.state;    siteContent = this.escapeChars(siteContent)    return (      <div>        {          siteContent?            <div dangerouslySetInnerHTML={this.createMarkup(siteContent)}></div>:            <div className="pusht">暫無日誌……</div>        }      </div>    );  }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.