[英]Database unable to store in html format
I'm using springmvc
, hibernate
and mysql
. 我正在使用
springmvc
, hibernate
和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. 每当我在项目中上载文件时,数据库就不会以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. 应该从应用程序端点分析此问题。
The request that you are sending to services should be UTF-8
. 您要发送到服务的请求应为
UTF-8
。
The data should be handled as UTF-8
until you persist the data to 数据应以
UTF-8
格式处理,直到将数据持久保存到
DB. D B。
Check your connection string that support Unicode
and Character Encoding
检查您的连接字符串是否支持
Unicode
和Character 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”
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.