JSP / Servlets:如何上传zip文件,解压缩并解压缩CSV文件 [英] JSP/Servlets: How do I Upload a zip file, unzip it and extract the CSV file

查看:251
本文介绍了JSP / Servlets:如何上传zip文件,解压缩并解压缩CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道如何在JSP / Servlets中执行以下操作:


  1. 上传zip文件(包含多个CSV文件)


  2. 解压缩文件到obsian的CSV文件

  3. 将记录导入到mySQL数据库中


注意:mySQL表已设置好, >

提前感谢。

解决方案


上传zip文件(包含多个CSV文件)


使用 multipart / form-data 在HTML / JSP中使用输入类型=文件形式,以便能够选择文件并将其上传。在Servlet中使用 Apache Commons FileUpload ,以便能够解析请求正文并获取上传的文件。另请参阅:如何在JSP / Servlet中上传文件?


2:将文件解压缩为obtian的CSV文件


使用 java.util.ZipInputStream 以读取zip文件并解压缩zip条目。另请参阅:在Java中压缩和解压缩文件


3:读取CSV文件并将记录泵入mySQL数据库


两种方法:


  1. 将CSV放在MySQL可访问的本地磁盘文件系统并指示它使用 LOAD DATA INFILE 查询。


  2. 使用现有的 CSV解析器创建一个以将CSV解析为可用的Java对象集合,例如列表< List< String>> 。然后学习JDBC ,并使用 PreparedStatement 创建,填充和批量执行 INSERT 查询。另请参见此 MySQL和JDBC的迷你教程



Wondering how can I do the following in JSP/Servlets:

  1. Upload a zip file (containing multiple CSV files)

  2. Unzip the file to obtian the CSV files

  3. Read the CSV files and pump the records into a mySQL database

Note: mySQL table is set up and ready for CSV files inputs.

Thanks in advance.

解决方案

1: Upload a zip file (containing multiple CSV files)

Use a multipart/form-data form with input type="file" in HTML/JSP to be able to select a file and upload it. Use Apache Commons FileUpload in the Servlet to be able to parse the request body and obtain the uploaded files. See also: How to upload files in JSP/Servlet?

2: Unzip the file to obtian the CSV files

Use java.util.ZipInputStream to read a zip file and extract the zip entries. See also: Compressing and Decompressing files in Java.

3: Read the CSV files and pump the records into a mySQL database

Two ways:

  1. Put the CSV somewhere on the local disk file system where the MySQL has access to and instruct it to import it using a LOAD DATA INFILE query.

  2. Use an existing CSV parser or create one to parse a CSV into a useable collection of Java objects, e.g. List<List<String>>. Then learn JDBC and use PreparedStatement to create, populate and execute an INSERT query in batches. See also this mini tutorial on MySQL and JDBC.

这篇关于JSP / Servlets:如何上传zip文件,解压缩并解压缩CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆