简体   繁体   English

数据库无法以html格式存储

[英]Database unable to store in html format

I'm using springmvc , hibernate and mysql . 我正在使用springmvchibernatemysql Whenever I upload a file in my project the database doesn't save in HTML format, I want that user who uploads the file, the database should maintain the format. 每当我在项目中上载文件时,数据库就不会以HTML格式保存,我希望上载该文件的用户保持数据库的格式。 What should I do? 我该怎么办?

Uploading a method which controller calls during upload. 上载控制器在上载期间调用的方法。 Apart from code, any general idea would be appreciated. 除了代码之外,任何一般性想法都将受到赞赏。

private String getContentDescription(MultipartFile file, Long contentCategoryId) {
  StringBuffer contentDescription = new StringBuffer();
  ContentHandler textHandler = new BodyContentHandler(-1);
  InputStream input = null;
  try {
    input = file.getInputStream();
    Metadata metadata = new Metadata();
    this.parser.parse(input, textHandler, metadata, new ParseContext());
    input.close();
  } catch (IOException | SAXException | TikaException e) {
    LOGGER.debug("Unable to read uploaded document", e);
  }
  String returnString = "";
  if (null != textHandler) {
    if (contentCategoryId==3 && contentCategoryId==4) {
      String contentText = textHandler.toString();
      returnString = contentText.substring(0, Math.max(0, contentText.length()));
    } else {
      String contentText = textHandler.toString();
      returnString = contentText.substring(0, Math.min(1200, contentText.length()));
    }
  }
  return returnString;
}

You are using Tika to parse the HTML. 您正在使用Tika解析HTML。 BodyContentHandler will only return the HTML found within the tags and not include anything else. BodyContentHandler将仅返回在标记中找到的HTML,而不包含其他任何内容。 What you want to do is read the entire file. 您要做的就是读取整个文件。 Try something like this: 尝试这样的事情:

private String getContentDescription(MultipartFile file, Long contentCategoryId) {
    try (InputStream inputStream = file.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
        StringBuilder sb = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append('\n');
        }
        return sb.toString();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return null;
}

This issue should be analysed from the Application endpoint. 应该从应用程序端点分析此问题。

  1. The request that you are sending to services should be UTF-8 . 您要发送到服务的请求应为UTF-8

  2. The data should be handled as UTF-8 until you persist the data to 数据应以UTF-8格式处理,直到将数据持久保存到

    DB. D B。

  3. Check your connection string that support Unicode and Character Encoding 检查您的连接字符串是否支持UnicodeCharacter Encoding

property name="javax.persistence.jdbc.url" 属性名称=“ javax.persistence.jdbc.url”
value="jdbc:mysql://localhost:3306/blogdatabase?useUnicode=yes&characterEncoding=UTF-8" value =“ jdbc:mysql:// localhost:3306 / blogdatabase?useUnicode = yes&characterEncoding = UTF-8”

  1. To save the data to the database the column should be capable of storing stat particular data.To Store the UTF-8 data convert the Db column to lob or blob type. 为了将数据保存到数据库中,该列应该能够存储特定于统计的数据。要存储UTF-8数据,请将Db列转换为lob or blob类型。

Use the blow reference to config the JPA for the same. 使用打击参考将JPA配置为相同。 JPA utf-8 characters not persisted JPA utf-8字符未保留

Spring configurations Spring MVC UTF-8 Encoding Spring配置Spring MVC UTF-8编码

Spring multi part file upload : http://javainsimpleway.com/spring-mvc-file-upload-single-and-multiple-files-upload/ Spring多部分文件上传: http : //javainsimpleway.com/spring-mvc-file-upload-single-and-multiple-files-upload/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM