Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions file-base64-converter/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# 文件 Base64 转换工具 (File Base64 Converter)

## 一、背景说明

当前 CodeWave 平台不支持 `byte` 字节类型,也不支持二进制字节流的直接处理。为解决此限制,本依赖库提供“通过 URL 下载网络文件并转换为 Base64 字符串”的能力,便于在平台中进行后续传输或存储。

该库对应 Maven 制品为 `file-base64-converter`(`version`:`1.0.0`),定位为通用的文件 Base64 转换工具库,支持将文件 URL 批量转换为 Base64 编码字符串,并内置网络下载与编码转换逻辑。

## 二、核心功能

1. **网络文件下载**:基于 `HttpURLConnection` 以 `GET` 方式拉取远程文件内容。
2. **Base64 编码转换**:将下载得到的二进制内容编码为 Base64 字符串(使用 `Base64.getEncoder()`)。
3. **批量转换**:支持传入 URL 列表,按输入顺序逐个转换并返回结果列表。
4. **超时控制**:内置连接超时 `10` 秒、读取超时 `30` 秒,避免长时间阻塞。
5. **异常处理**:下载失败或转换异常时记录日志并返回 `null`(批量场景返回列表,失败项为 `null`),保证流程不崩溃。

## 三、配置说明

本依赖库为无状态、无全局配置设计:

- 不需要 `application.yml` 等全局配置;连接信息由入参 `fileUrl` / `fileUrls` 动态指定。
- 依赖库通过 Spring 环境自动扫描注入(无需额外手动装配)。

## 四、接口说明

### 1. 批量转换 (convertUrlListToBase64)

批量将网络文件 URL 列表转换为 Base64 字符串列表,保持输入顺序返回。

**入参**

| 字段名称 | 描述 | 类型 | 说明 |
| :--- | :--- | :--- | :--- |
| fileUrls | 文件 URL 地址列表 | List&lt;String&gt; | 可选:允许为 `null`,此时直接返回空列表<br>建议每个元素为完整 `http://` 或 `https://` 地址,例如:`https://example.com/a.png` |

**出参**

| 字段名称 | 描述 | 类型 | 说明 |
| :--- | :--- | :--- | :--- |
| result | Base64 字符串列表 | List&lt;String&gt; | 与输入列表顺序一致<br>若某个 URL 转换失败,对应位置返回 `null` |

### 2. 单文件转换 (convertUrlToBase64)

输入一个文件 URL,下载并返回该文件内容的 Base64 编码字符串。

**入参**

| 字段名称 | 描述 | 类型 | 说明 |
| :--- | :--- | :--- | :--- |
| fileUrl | 文件 URL 地址 | String | 必填:不能为空;若为 `null` 或空串直接返回 `null`<br>需为 `http://` 或 `https://` 开头的完整地址,例如:`https://example.com/a.png` |

**出参**

| 字段名称 | 描述 | 类型 | 说明 |
| :--- | :--- | :--- | :--- |
| result | Base64 字符串 | String | 转换成功返回 Base64 字符串(不包含 `data:` 前缀)<br>失败返回 `null` |

## 五、注意事项

1. 请确保传入的 URL 为可访问的 `http://` 或 `https://` 地址;本库基于 `HttpURLConnection` 实现,非 HTTP(S) 协议将导致转换失败并返回 `null`。
2. 仅当响应码为 `200`(`HttpURLConnection.HTTP_OK`)时才认为下载成功;否则会记录错误日志并返回 `null`。
3. URL 中如包含空格、中文等特殊字符,请先按 URL 规范进行编码(例如按 UTF-8 进行 URL 编码),避免 `new URL(fileUrl)` 解析失败。
4. 单文件转换在入参为 `null` 或空串时直接返回 `null`;批量转换在入参列表为 `null` 时返回空列表,列表内某个元素为空或下载失败时对应结果为 `null`。
5. 默认连接超时时间为 `10` 秒,读取超时时间为 `30` 秒;超时或其他异常会被捕获,记录日志后返回 `null`。
6. 转换过程会将文件完整读入内存后再进行 Base64 编码;大文件会带来较高内存消耗,建议用于图片、PDF 等中小型文件。
7. 本库不涉及 FTP 场景,因此不包含被动模式、目录切换、目录不存在处理、`FTP.BINARY_FILE_TYPE` 等相关配置;文件内容按 HTTP 输入流字节读取后直接进行 Base64 编码。
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.netease.lowcode</groupId>
<artifactId>nasl-metadata-maven-plugin</artifactId>
<version>1.7.1</version>
<packaging>maven-plugin</packaging>

<name>Nasl Metadata Maven Plugin</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mavenVersion>3.2.5</mavenVersion>
<jettyVersion>9.4.54.v20240208</jettyVersion>
<pluginTestingVersion>3.3.0</pluginTestingVersion>
<resolverVersion>1.0.2.v20150114</resolverVersion>
<javaVersion>8</javaVersion>
<slf4j.version>1.7.36</slf4j.version>
<plexus-archiver.version>4.9.2</plexus-archiver.version>
</properties>

<dependencies>
<!-- maven -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-repository-metadata</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>

<!-- reporting -->
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-sink-api</artifactId>
<version>1.12.0</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven.reporting</groupId>
<artifactId>maven-reporting-api</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.reporting</groupId>
<artifactId>maven-reporting-impl</artifactId>
<version>3.2.0</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- plexus -->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-archiver</artifactId>
<version>${plexus-archiver.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-xml</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-io</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-i18n</artifactId>
<version>1.0-beta-10</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- shared -->
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-analyzer</artifactId>
<version>1.13.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-tree</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-common-artifact-filters</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-artifact-transfer</artifactId>
<version>0.13.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-utils</artifactId>
<version>3.4.2</version>
</dependency>

<!-- dependencies to annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${resolverVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
<version>${resolverVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonatype.plexus</groupId>
<artifactId>plexus-build-api</artifactId>
<version>0.0.7</version>
<scope>compile</scope>
</dependency>

<!-- others -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.16.1</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.28.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5</version>
<configuration>
<goalPrefix>nasl-metadata-maven-plugin</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
78 changes: 78 additions & 0 deletions file-base64-converter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version><!--与当前制品应用默认版本统一-->
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.yourcompany.converter</groupId>
<artifactId>file-base64-converter</artifactId>
<version>1.0.0</version>
<name>文件下载并转换为Base64工具库</name>
<description>通用的文件Base64转换工具库,支持批量将文件URL转换为Base64编码字符串,提供网络文件下载和编码转换功能</description>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<nasl.ide.version>4.1</nasl.ide.version>
</properties>
<dependencies>
<!--本案例是本地系统引入nasl-metadata-collector-0.8.0.jar的方式。
若把nasl-metadata-collector-0.8.0.jar安装到自己的maven仓库,
注意修改artifactId和groupId的情况下,不要使用<scope>system</scope>,会在发布时造成依赖中断。
不修改artifactId和groupId的情况下,nasl-metadata-maven-plugin会做特殊处理-->
<dependency>
<artifactId>nasl-metadata-collector</artifactId>
<groupId>com.netease.lowcode</groupId>
<version>0.15.0</version>
<!-- <optional>true</optional>-->
<!-- <scope>system</scope>-->
<!-- <systemPath>${project.basedir}/jar/nasl-metadata-collector-0.15.0/nasl-metadata-collector-0.15.0.jar</systemPath>-->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<!--制品应用使用Springboot框架,父应用引用了的包,为了防止版本冲突,scope可设置为provided-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.netease.lowcode</groupId>
<artifactId>nasl-metadata-maven-plugin</artifactId>
<version>1.7.1</version>
<configuration>
<jarWithDependencies>false</jarWithDependencies>
</configuration>
<executions>
<execution>
<goals>
<goal>archive</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading
Loading