在 java 中将 base64 转换为 excel 文件
Convert base64 to excel file in java
我有一个 Web 应用程序,我正在从那里上传 excel 文件并将其转换为 base64 并将其作为字符串发送到我的休息 api,从我的休息 API 我需要读取那个 excel 文件,然后我需要将该文件上传到我的数据库 (MYSQL),我不想将文件保存在磁盘上的任何位置,我想在内存中读取文件。
我在互联网上搜索了很多关于如何在 java 中将 base64 转换为 excel,但不幸的是我没有得到任何解决方案。
如有任何帮助,我们将不胜感激。
如果您有图像或 excel 文件,这对 Java 没有影响。
下面是一段我认为应该可以帮助您入门的示例代码。
它基于 Write Base64-encoded image to file and the code provided there for decoding the base 64 encoded file and based on the instructions from https://www.codejava.net/java-se/jdbc/insert-file-data-into-mysql-database-using-jdbc。它应该适用于许多数据库,因为它仅使用 JDBC,但未针对 MySQL.
进行测试
package net.codejava.jdbc;
import org.apache.commons.codec.binary.Base64;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
class JdbcInsertFileTwo {
public static void main(String[] args) {
String base64encoded = "asdf123";
byte[] excelBytes = Base64.decodeBase64(base64encoded);
String url = "jdbc:mysql://localhost:3306/contactdb";
String user = "root";
String password = "secret";
try (Connection conn = DriverManager.getConnection(url, user, password)) {
PreparedStatement statement = conn.prepareStatement("insert into table(name, data) " + "values(?,?)");
statement.setString(1, "My file name.xls");
statement.setBinaryStream(2, new ByteArrayInputStream(excelBytes), excelBytes.length);
int row = statement.executeUpdate();
if (row > 0) {
System.out.println("The excel file was uploaded.");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
Apache POI is a good library to process excel files, here is a good tutorial 首先,基本上您必须将 base base 64 文件转换为 InputStream,然后使用 Apache POI 提供的 WorkBook 实现之一来处理数据。
我有一个 Web 应用程序,我正在从那里上传 excel 文件并将其转换为 base64 并将其作为字符串发送到我的休息 api,从我的休息 API 我需要读取那个 excel 文件,然后我需要将该文件上传到我的数据库 (MYSQL),我不想将文件保存在磁盘上的任何位置,我想在内存中读取文件。 我在互联网上搜索了很多关于如何在 java 中将 base64 转换为 excel,但不幸的是我没有得到任何解决方案。
如有任何帮助,我们将不胜感激。
如果您有图像或 excel 文件,这对 Java 没有影响。 下面是一段我认为应该可以帮助您入门的示例代码。 它基于 Write Base64-encoded image to file and the code provided there for decoding the base 64 encoded file and based on the instructions from https://www.codejava.net/java-se/jdbc/insert-file-data-into-mysql-database-using-jdbc。它应该适用于许多数据库,因为它仅使用 JDBC,但未针对 MySQL.
进行测试package net.codejava.jdbc;
import org.apache.commons.codec.binary.Base64;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
class JdbcInsertFileTwo {
public static void main(String[] args) {
String base64encoded = "asdf123";
byte[] excelBytes = Base64.decodeBase64(base64encoded);
String url = "jdbc:mysql://localhost:3306/contactdb";
String user = "root";
String password = "secret";
try (Connection conn = DriverManager.getConnection(url, user, password)) {
PreparedStatement statement = conn.prepareStatement("insert into table(name, data) " + "values(?,?)");
statement.setString(1, "My file name.xls");
statement.setBinaryStream(2, new ByteArrayInputStream(excelBytes), excelBytes.length);
int row = statement.executeUpdate();
if (row > 0) {
System.out.println("The excel file was uploaded.");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
Apache POI is a good library to process excel files, here is a good tutorial 首先,基本上您必须将 base base 64 文件转换为 InputStream,然后使用 Apache POI 提供的 WorkBook 实现之一来处理数据。