Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
poc-api
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
poc
poc-api
Commits
2c98d9d0
Commit
2c98d9d0
authored
May 12, 2025
by
alex yao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:doc 乱码
parent
70f6f19f
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
8 deletions
+79
-8
BosConfigServiceImpl.java
.../cn/com/poc/common/service/impl/BosConfigServiceImpl.java
+2
-1
ContentReportRestImpl.java
...va/cn/com/poc/expose/rest/impl/ContentReportRestImpl.java
+25
-7
ContentReportTest.java
src/test/java/cn/com/poc/expose/ContentReportTest.java
+52
-0
No files found.
src/main/java/cn/com/poc/common/service/impl/BosConfigServiceImpl.java
View file @
2c98d9d0
...
...
@@ -23,6 +23,7 @@ import java.io.IOException;
import
java.io.InputStream
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Base64
;
import
java.util.Date
;
import
java.util.Optional
;
...
...
@@ -91,7 +92,7 @@ public class BosConfigServiceImpl implements BosConfigService {
meta
.
setContentDisposition
(
"attachment; filename="
+
FILE_NAME
);
}
// 设置内容被下载时的编码格式。
meta
.
setContentEncoding
(
"utf-8"
);
meta
.
setContentEncoding
(
StandardCharsets
.
UTF_8
.
displayName
()
);
meta
.
setContentLength
(
inputStream
.
available
());
// 设置上传目录
...
...
src/main/java/cn/com/poc/expose/rest/impl/ContentReportRestImpl.java
View file @
2c98d9d0
...
...
@@ -6,6 +6,8 @@ import cn.com.poc.common.utils.UUIDTool;
import
cn.com.poc.expose.dto.ContentReportDto
;
import
cn.com.poc.expose.rest.ContentReportRest
;
import
cn.com.yict.framemax.core.exception.BusinessException
;
import
cn.hutool.Hutool
;
import
cn.hutool.poi.word.Word07Writer
;
import
com.itextpdf.text.pdf.PdfWriter
;
import
com.vladsch.flexmark.html.HtmlRenderer
;
import
com.vladsch.flexmark.parser.Parser
;
...
...
@@ -20,6 +22,7 @@ import org.apache.poi.xwpf.usermodel.*;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.awt.*
;
import
java.io.*
;
import
java.nio.charset.StandardCharsets
;
import
java.util.regex.Matcher
;
...
...
@@ -71,15 +74,30 @@ public class ContentReportRestImpl implements ContentReportRest {
return
renderer
.
render
(
document
);
}
// private String convertHtmlToWord(String html) throws IOException {
// File file = File.createTempFile(UUIDTool.getUUID(), ".doc");
// FileOutputStream outputStream = new FileOutputStream(file);
// ByteArrayInputStream bais = new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8));//将字节数组包装到流中
// POIFSFileSystem poifs = new POIFSFileSystem();
// DirectoryEntry directory = poifs.getRoot();
// directory.createDocument("WordDocument", bais);
// poifs.writeFilesystem(outputStream);
// FileInputStream fileInputStream = new FileInputStream(file);
// bais.close();
// poifs.close();
// file.deleteOnExit();
// return bosConfigService.upload(fileInputStream, "doc", "application/msword");
// }
private
String
convertHtmlToWord
(
String
html
)
throws
IOException
{
File
file
=
File
.
createTempFile
(
UUIDTool
.
getUUID
(),
".docx"
);
FileOutputStream
outputStream
=
new
FileOutputStream
(
file
);
ByteArrayInputStream
bais
=
new
ByteArrayInputStream
(
html
.
getBytes
());
//将字节数组包装到流中
POIFSFileSystem
poifs
=
new
POIFSFileSystem
();
DirectoryEntry
directory
=
poifs
.
getRoot
();
directory
.
createDocument
(
"WordDocument"
,
bais
);
poifs
.
writeFilesystem
(
outputStream
);
Word07Writer
writer
=
new
Word07Writer
();
writer
.
addText
(
new
Font
(
"宋体"
,
Font
.
PLAIN
,
10
),
html
);
writer
.
flush
(
file
);
FileInputStream
fileInputStream
=
new
FileInputStream
(
file
);
return
bosConfigService
.
upload
(
fileInputStream
,
"docx"
,
"application/msword"
);
String
upload
=
bosConfigService
.
upload
(
fileInputStream
,
"docx"
,
"application/msword"
);
file
.
deleteOnExit
();
fileInputStream
.
close
();
return
upload
;
}
}
src/test/java/cn/com/poc/expose/ContentReportTest.java
0 → 100644
View file @
2c98d9d0
package
cn
.
com
.
poc
.
expose
;
import
cn.com.poc.expose.dto.ContentReportDto
;
import
cn.com.poc.expose.rest.ContentReportRest
;
import
cn.com.yict.framemax.core.spring.SingleContextInitializer
;
import
org.junit.runner.RunWith
;
import
org.junit.Test
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
javax.annotation.Resource
;
import
java.io.IOException
;
/**
* @author alex.yao
* @date 2025/5/12
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@ContextConfiguration
(
initializers
=
SingleContextInitializer
.
class
)
@WebAppConfiguration
public
class
ContentReportTest
{
@Resource
private
ContentReportRest
contentReportRest
;
@Test
public
void
test_report
()
throws
IOException
{
String
content
=
"<pre> </pre><div class=\"code-render-container\"><div class=\"code-operation-bar-container\"><span class=\"language\">markdown</span></div><div class=\"code-render-wrapper\"><pre class=\"code-render-inner\"><code><span class=\"hljs-section\"># 图片内容总结</span>\n"
+
"\n"
+
"这是一幅书法作品,内容如下:\n"
+
"\n"
+
"<span class=\"hljs-bullet\">-</span> <span class=\"hljs-strong\">**正文**</span>:工资那么低,工作上出差错是应该的,毕竟便宜没好货!\n"
+
"<span class=\"hljs-bullet\">-</span> <span class=\"hljs-strong\">**落款**</span>:一石\n"
+
"\n"
+
"<span class=\"hljs-section\">## 作品主题</span>\n"
+
"作品表达了对工资低和工作质量之间关系的一种调侃和无奈的态度。作者认为因为工资低,所以工作出差错是可以接受的,并用“便宜没好货”这一俗语来类比,暗示低工资可能导致低质量的工作表现。\n"
+
"\n"
+
"<span class=\"hljs-section\">## 艺术风格</span>\n"
+
"<span class=\"hljs-bullet\">-</span> <span class=\"hljs-strong\">**书法风格**</span>:作品采用传统的书法形式,字体流畅,具有一定的艺术美感。\n"
+
"<span class=\"hljs-bullet\">-</span> <span class=\"hljs-strong\">**情感表达**</span>:通过幽默和讽刺的方式,传达出对现实工作环境的不满和对生活无奈的感慨。\n"
+
"</code></pre></div></div><pre><code class=\"hljs code-container-wrapper language-markdown\">\n"
+
"</code></pre>"
;
String
reportType
=
"doc"
;
ContentReportDto
dto
=
new
ContentReportDto
();
dto
.
setContent
(
content
);
dto
.
setReportType
(
reportType
);
contentReportRest
.
report
(
dto
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment