Authored by Amos_sdy

添加敏感词批量插入接口

  1 +package com.yoho.yhmessage.rest;
  2 +
  3 +import java.io.BufferedReader;
  4 +import java.io.File;
  5 +import java.io.FileInputStream;
  6 +import java.io.IOException;
  7 +import java.io.InputStreamReader;
  8 +import java.util.ArrayList;
  9 +import java.util.List;
  10 +
  11 +import org.slf4j.Logger;
  12 +import org.slf4j.LoggerFactory;
  13 +import org.springframework.beans.factory.annotation.Autowired;
  14 +import org.springframework.stereotype.Controller;
  15 +import org.springframework.web.bind.annotation.RequestMapping;
  16 +import org.springframework.web.bind.annotation.ResponseBody;
  17 +
  18 +import com.yoho.message.dal.MessageSensitiveWordsMapper;
  19 +import com.yoho.message.dal.model.MessageSensitiveWords;
  20 +import com.yoho.service.model.sms.response.CommonRspBO;
  21 +
  22 +@Controller
  23 +@RequestMapping("/FilterInfoRest")
  24 +public class FilterInfoRest {
  25 + private static final Logger logger = LoggerFactory.getLogger(FilterInfoRest.class);
  26 +
  27 + @Autowired
  28 + private MessageSensitiveWordsMapper messageSensitiveWordsMapper;
  29 +
  30 + /**
  31 + *
  32 + * Description: 添加敏感词<br>
  33 + *
  34 + * @author amos.shan<br>
  35 + * @taskId <br>
  36 + * @return <br>
  37 + * @throws IOException
  38 + */
  39 + @SuppressWarnings("resource")
  40 + @RequestMapping(value = "/addSensitiveWords")
  41 + @ResponseBody
  42 + public CommonRspBO addSensitiveWords() throws IOException {
  43 + CommonRspBO commonRspBO = new CommonRspBO(200, "OK");
  44 + List<MessageSensitiveWords> wordList = null;
  45 + File file = new File("D:\\SensitiveWord.txt"); // 读取文件
  46 + InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
  47 + try {
  48 + if (file.isFile() && file.exists()) { // 文件流是否存在
  49 + wordList = new ArrayList<MessageSensitiveWords>();
  50 + BufferedReader bufferedReader = new BufferedReader(read);
  51 + String txt = null;
  52 + while ((txt = bufferedReader.readLine()) != null) {
  53 + MessageSensitiveWords word = new MessageSensitiveWords();
  54 + word.setSensitiveWord(txt);
  55 + wordList.add(word);
  56 + }
  57 + messageSensitiveWordsMapper.insertSensitiveWordsBatch(wordList);
  58 + } else {
  59 + commonRspBO.setCode(-1);
  60 + commonRspBO.setMessage("敏感词库不存在");
  61 + }
  62 + } catch (Exception e) {
  63 + commonRspBO.setCode(-1);
  64 + commonRspBO.setMessage(e.getMessage());
  65 + } finally {
  66 + read.close(); // 关闭文件流
  67 + }
  68 + return commonRspBO;
  69 + }
  70 +}