0

I'm using springmvc, hibernate and mysql. 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. 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;
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
  • 1
    How is the above code relevant to saving text in a DB? – Scary Wombat Jan 13 '17 at 06:01
  • Its just a method which controller calls during saving the data. It may not help , but any suggestion or any idea would help me out. Sorry for any mistakes. – Nainesh patel Jan 13 '17 at 06:04
  • So did you check whether this method is corrupting your String? – Scary Wombat Jan 13 '17 at 06:08
  • If you want to save the file your receive exactly as you receive it, don't *parse* it into something else first. Just retrieve the bytes and save them. Or convert the bytes to text using the correct character set and save that text, – Andreas Jan 13 '17 at 06:12
  • What is the purpose of `Math.max(0, contentText.length())`? When do you expect a `String` to return a `length()` value less than `0`? `contentText.substring(0, Math.max(0, contentText.length()))` is the same as `contentText.substring(0, contentText.length())` which is the same as `contentText`. – Andreas Jan 13 '17 at 06:13
  • `contentCategoryId==3 && contentCategoryId==4` some Schrödinger int? –  Jan 13 '17 at 06:38
  • thanks for replying @Andreas math.max and contentCategoryId are project requirement.For particular upload in some categories full content will be display and for some only specific content will be displayed. In mysql i am using meduim-text as type of it. – Nainesh patel Jan 13 '17 at 06:48
  • @ScaryWombat no it is not corrupting my string but it doesn't save in HTML format. it saves in string format with some symbols. – Nainesh patel Jan 13 '17 at 06:50
  • @RC. yes it takes long as type variable – Nainesh patel Jan 13 '17 at 06:53
  • still a Schrödinger number, usually a number can **not** be `==` to 3 **and** `==` to 4 –  Jan 13 '17 at 08:53

2 Answers2

1

You are using Tika to parse the HTML. BodyContentHandler will only return the HTML found within the tags and not include anything else. 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;
}
Tea Curran
  • 2,923
  • 2
  • 18
  • 22
0

This issue should be analysed from the Application endpoint.

  1. The request that you are sending to services should be UTF-8.

  2. The data should be handled as UTF-8 until you persist the data to

    DB.

  3. Check your connection string that support Unicode and Character Encoding

property name="javax.persistence.jdbc.url"
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.

Use the blow reference to config the JPA for the same. JPA utf-8 characters not persisted

Spring configurations Spring MVC UTF-8 Encoding

Spring multi part file upload : http://javainsimpleway.com/spring-mvc-file-upload-single-and-multiple-files-upload/

Community
  • 1
  • 1
Abin Manathoor Devasia
  • 1,945
  • 2
  • 21
  • 47