前端读取zip,并将zip转File上传到服务端.

23 篇文章 0 订阅

一. 下载包

npm i jszip -S

二. 引入包

 <script src="jszip/jszip.min.js"></script>

三. 解析zip文件

httpRequest({ file }) {
  // 解压文件
  this.loading = true;
  const _this = this;
  // ********************************* start *********************************
  // 读取zip内文件
  JSZip.loadAsync(file, {
    decodeFileName: function (bytes) {
      return iconv.decode(bytes, "gbk");
    },
  }).then(function (zip) {
    _this.loading = false;
    zip.forEach((val) => {
      // 将文件转成FIle格式
      zip.files[val].async("blob").then(function (con) {
        const file = new window.File([con], val);
        const id_number = val.split("_")[0];
        const user_name = val.split("_")[1].split(".")[0];
        _this.userList.push({
          user_name,
          id_number,
          file,
          name: val,
        });
        _this.total++;
      });
    });
  });
},
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Java的ZipInputStream类来读取zip压缩文件,然后将数据写入数据库。以下是一个简单的示例代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipFileReader { public static void main(String[] args) { String zipFilePath = "path/to/your/zip/file.zip"; String dbUrl = "jdbc:mysql://localhost:3306/mydatabase"; String dbUsername = "your-username"; String dbPassword = "your-password"; try { // 打开数据库连接 Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword); // 创建插入数据的SQL语句 String insertQuery = "INSERT INTO your_table_name (file_name, file_data) VALUES (?, ?)"; // 创建PreparedStatement对象 PreparedStatement pstmt = conn.prepareStatement(insertQuery); // 创建ZipInputStream对象 ZipInputStream zipInput = new ZipInputStream(new FileInputStream(zipFilePath)); // 读取zip文件中的每个条目 ZipEntry entry = zipInput.getNextEntry(); while (entry != null) { // 读取条目的文件名和数据 String fileName = entry.getName(); byte[] fileData = new byte[(int) entry.getSize()]; zipInput.read(fileData); // 设置参数并执行插入语句 pstmt.setString(1, fileName); pstmt.setBytes(2, fileData); pstmt.executeUpdate(); // 关闭当前条目,准备读取下一个条目 zipInput.closeEntry(); entry = zipInput.getNextEntry(); } // 关闭ZipInputStream、PreparedStatement和数据库连接 zipInput.close(); pstmt.close(); conn.close(); System.out.println("数据成功写入数据库!"); } catch (IOException | SQLException e) { e.printStackTrace(); } } } ``` 请将代码中的以下内容替换为您自己的信息: - `zipFilePath`:您的zip文件的路径。 - `dbUrl`:您的数据库URL。 - `dbUsername`:您的数据库用户名。 - `dbPassword`:您的数据库密码。 - `your_table_name`:您要插入数据的表名。 这段代码将逐个读取zip文件中的条目,并将每个条目的文件名和数据插入到数据库中。您需要根据自己的需求修改代码以适应您的数据库结构和数据处理逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值