简体   繁体   中英

Import csv file to mysql through jsp servlet

I am writing a web app that I would like to be able to import a csv file into a database from a jsp. Previously I have used the following code to insert the csv into the database.

LOAD DATA LOCAL INFILE "/myFileLocation.csv"
INTO TABLE myTable
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

Which works great when I have the file locally.

My question, when I upload the csv file in my jsp as a multipart. Is it possible for me to pass that PartItem file as a variable and replace the "/myFileLocation.csv" with the PartItem files temp location?

I can see the temp location when I debug and view the PartItem file which resides in repository/path under the variables table. Is this at all possible to access or do i need to parse the csv and insert it into the database that way?

I ended up finding a way to make this work. Not sure if it's the best solution but its working as I envisioned. Basically I create a string pointing to an assets folder I created in the web/resources like this.

final String mainPath = this.getServletContext().getRealPath("resources/assets");

Then I read the uploaded file, check to see if the file already exists in my assets folder, if it does I delete it.

Part filePart = request.getPart("csvFile");
String path = mainPath + "/" + filePart.getSubmittedFileName();
File fileTemp = new File(path);
if(fileTemp.exists()){
    fileTemp.delete();
}

Lastly I read the uploaded file and write a new file in the location I directed it to which in this case is the assets folder I created like this.

    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();
           }
       }

After that I just passed a string containing the path to the file with the file name to the DAO I created where I was able to utilize the sql statement I had posted above.

Like I stated before, not sure if this is the best way to do this but it seems to be working fine for me and none of my java code is contained within my jsp. If anyone has a better way of doing this or sees something wrong with what I did here let me know, I'd be very interested to hear about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM