通过 jsp servlet 将 csv 文件导入到 mysql
Import csv file to mysql through jsp servlet
我正在编写一个 Web 应用程序,我希望能够将 csv 文件从 jsp 导入数据库。以前我使用下面的代码将 csv 插入数据库。
LOAD DATA LOCAL INFILE "/myFileLocation.csv"
INTO TABLE myTable
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
当我在本地拥有该文件时效果很好。
我的问题是,当我将 csv 文件作为多部分上传到 jsp 时。我是否可以将该 PartItem 文件作为变量传递并将“/myFileLocation.csv”替换为 PartItem 文件的临时位置?
我在调试和查看驻留在变量 table 下的 repository/path 中的 PartItem 文件时可以看到临时位置。这完全可以访问还是我需要解析 csv 并将其插入数据库?
我最终找到了一种方法来完成这项工作。不确定它是否是最好的解决方案,但它按我的设想工作。基本上,我创建了一个字符串,指向我在 web/resources 中创建的资产文件夹,就像这样。
final String mainPath = this.getServletContext().getRealPath("resources/assets");
然后我读取上传的文件,检查我的资产文件夹中是否已经存在该文件,如果存在我将其删除。
Part filePart = request.getPart("csvFile");
String path = mainPath + "/" + filePart.getSubmittedFileName();
File fileTemp = new File(path);
if(fileTemp.exists()){
fileTemp.delete();
}
最后我读取了上传的文件并在我指定的位置写入了一个新文件,在本例中是我这样创建的资产文件夹。
final String fileName = filePart.getSubmittedFileName();
File convFile = new File(filePart.getSubmittedFileName());
FileOutputStream fos = new FileOutputStream(convFile);
OutputStream out = null;
InputStream filecontent= null;
try{
out = new FileOutputStream(new File(mainPath + File.separator + fileName));
filecontent = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while((read = filecontent.read(bytes)) != -1){
out.write(bytes, 0, read);
}
} catch (FileNotFoundException fne) {
} finally {
if (out != null) {
out.close();
}
if (filecontent != null) {
filecontent.close();
}
}
之后,我将包含文件路径的字符串和文件名传递给我创建的 DAO,我可以在其中使用上面发布的 sql 语句。
就像我之前说的,不确定这是否是最好的方法,但它似乎对我来说工作正常,我的 java 代码中的 none 包含在我的 jsp。如果有人有更好的方法或发现我在这里所做的有问题,请告诉我,我很想听听。
我正在编写一个 Web 应用程序,我希望能够将 csv 文件从 jsp 导入数据库。以前我使用下面的代码将 csv 插入数据库。
LOAD DATA LOCAL INFILE "/myFileLocation.csv"
INTO TABLE myTable
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
当我在本地拥有该文件时效果很好。
我的问题是,当我将 csv 文件作为多部分上传到 jsp 时。我是否可以将该 PartItem 文件作为变量传递并将“/myFileLocation.csv”替换为 PartItem 文件的临时位置?
我在调试和查看驻留在变量 table 下的 repository/path 中的 PartItem 文件时可以看到临时位置。这完全可以访问还是我需要解析 csv 并将其插入数据库?
我最终找到了一种方法来完成这项工作。不确定它是否是最好的解决方案,但它按我的设想工作。基本上,我创建了一个字符串,指向我在 web/resources 中创建的资产文件夹,就像这样。
final String mainPath = this.getServletContext().getRealPath("resources/assets");
然后我读取上传的文件,检查我的资产文件夹中是否已经存在该文件,如果存在我将其删除。
Part filePart = request.getPart("csvFile");
String path = mainPath + "/" + filePart.getSubmittedFileName();
File fileTemp = new File(path);
if(fileTemp.exists()){
fileTemp.delete();
}
最后我读取了上传的文件并在我指定的位置写入了一个新文件,在本例中是我这样创建的资产文件夹。
final String fileName = filePart.getSubmittedFileName();
File convFile = new File(filePart.getSubmittedFileName());
FileOutputStream fos = new FileOutputStream(convFile);
OutputStream out = null;
InputStream filecontent= null;
try{
out = new FileOutputStream(new File(mainPath + File.separator + fileName));
filecontent = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while((read = filecontent.read(bytes)) != -1){
out.write(bytes, 0, read);
}
} catch (FileNotFoundException fne) {
} finally {
if (out != null) {
out.close();
}
if (filecontent != null) {
filecontent.close();
}
}
之后,我将包含文件路径的字符串和文件名传递给我创建的 DAO,我可以在其中使用上面发布的 sql 语句。
就像我之前说的,不确定这是否是最好的方法,但它似乎对我来说工作正常,我的 java 代码中的 none 包含在我的 jsp。如果有人有更好的方法或发现我在这里所做的有问题,请告诉我,我很想听听。