Skip to content

Commit 17b68ca

Browse files
authored
Merge pull request #318 from netease-lcap/freemarker-tool-1.3
Freemarker tool 1.3
2 parents d1cf930 + 33a13fc commit 17b68ca

6 files changed

Lines changed: 1181 additions & 25 deletions

File tree

freemarker-tool/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ freemarker工具
3232
| jsonData | String | 模板数据 |
3333
| imageMap | Map<String,String> | k=图片名称,v=图片信息 |
3434
| base64 | Boolean | 图片是否编码<br/><ul><li>false:传入图片url</li><li>true:传入图片base64编码</li></ul> |
35+
| processImgTag | Boolean | 是否将富文本中的`&lt;img&gt;`标签转换为 Word 图片,默认 false |
3536

3637

3738
# 如何制作docx模板

freemarker-tool/pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>com.netease.lowcode</groupId>
88
<artifactId>freemarker-tool</artifactId>
99
<!-- We currently do not support snapshots. Please use a testing version number like 0.x.x, e.g. 0.1.12 -->
10-
<version>1.2.5</version>
10+
<version>1.3.7</version>
1111

1212
<properties>
1313
<maven.compiler.source>8</maven.compiler.source>
@@ -99,6 +99,11 @@
9999
<artifactId>commons-lang3</artifactId>
100100
<version>3.13.0</version>
101101
</dependency>
102+
<dependency>
103+
<groupId>com.twelvemonkeys.imageio</groupId>
104+
<artifactId>imageio-webp</artifactId>
105+
<version>3.10.1</version>
106+
</dependency>
102107
<dependency>
103108
<groupId>org.springframework.boot</groupId>
104109
<artifactId>spring-boot-starter-test</artifactId>
@@ -152,4 +157,4 @@
152157
</plugins>
153158
</build>
154159

155-
</project>
160+
</project>

freemarker-tool/src/main/java/com/netease/lowcode/freemarker/dto/CreateDocxRequest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ public class CreateDocxRequest {
3434
* 如果为true,表示图片传入的是base64编码
3535
*/
3636
public Boolean base64 = false;
37+
38+
/**
39+
* 是否将富文本中的img标签转换为图片
40+
*/
41+
public Boolean processImgTag = false;
3742
}

freemarker-tool/src/main/java/com/netease/lowcode/freemarker/util/FileUtil.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,46 @@
1818
import java.net.URL;
1919
import java.util.Arrays;
2020
import java.util.Objects;
21+
import java.util.concurrent.TimeUnit;
2122

2223
public class FileUtil {
2324

2425
private static final Logger logger = LoggerFactory.getLogger("LCAP_CUSTOMIZE_LOGGER");
2526

27+
private static final OkHttpClient client = new OkHttpClient.Builder()
28+
.connectTimeout(5, TimeUnit.SECONDS)
29+
.readTimeout(30, TimeUnit.SECONDS)
30+
.followRedirects(true)
31+
.followSslRedirects(true)
32+
.build();
33+
2634
public static InputStream getFileInputStream(String urlStr) throws IOException {
27-
URL url = new URL(getTrueUrl(urlStr));
28-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
29-
connection.setConnectTimeout(3 * 1000);
30-
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36");
31-
return url.openStream();
35+
logger.info("urlStr using OkHttp: {}", urlStr);
36+
37+
// 还是保留你原本的 URL 处理逻辑
38+
String finalUrl = getTrueUrl(urlStr);
39+
40+
Request request = new Request.Builder()
41+
.url(finalUrl)
42+
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36")
43+
.build();
44+
45+
// 执行请求
46+
Response response = client.newCall(request).execute();
47+
48+
if (!response.isSuccessful()) {
49+
// 如果失败(比如 404 或 500),必须关闭响应体并抛出异常
50+
response.close();
51+
throw new IOException("Unexpected code " + response);
52+
}
53+
54+
ResponseBody body = response.body();
55+
if (body == null) {
56+
response.close();
57+
throw new IOException("Response body is null");
58+
}
59+
60+
return body.byteStream();
3261
}
3362

3463
// 新增方法:检测 URL 是否已经编码

0 commit comments

Comments
 (0)