0

I have worked on saving a single file into database(postgresql) and it works fine. But I am stuck when i try to upload multiple files.

My jsp fileupload :

<input type="file" id="reqFile" name="attachments.myFile" multiple/>

This is my action method :

    public String SaveAttachments() throws SQLException, IOException {
    int case_id = (Integer) session.get("case_id");
    System.out.println("in save attachments()");
    int uploaded_by = 1;
    try {
        File destFile = new File(attachments.getMyFileFileName());
        FileUtils.copyFile(attachments.getMyFile(), destFile);
        fis = new FileInputStream(destFile);
        currentCon = ConnectionManager.getConnection();
        pstmt = (PreparedStatement) currentCon.prepareStatement("insert into case_attachments(case_id, attachment, attachment_title, upload_date, uploaded_by) values(?,?,?,current_date,?)");
        pstmt.setInt(1, case_id);
        pstmt.setBinaryStream(2, fis, (int) destFile.length());
        pstmt.setString(3, attachments.getMyFileFileName());
        pstmt.setInt(4, uploaded_by);
        pstmt.executeUpdate();
    } catch (Exception e) {
        System.out.println("Error From SaveAttachments :" + e);
    }
    return SUCCESS;
}

When I attach multiple files it goes into the same column in database in comma seperated format. How can i save files in different rows when the user attaches multiple files? Thanks In advance.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
Pradnya
  • 649
  • 2
  • 6
  • 17

2 Answers2

1

You can upload multiple files like described here, then you simply need to loop them and insert them in the database one at time.

You just need to decide if using a business layer or doing the insert job directly from the Action (not the best practice...), and if putting all the files in a single transaction or each one in its own.

Remember that HTML5 multiple attribute should be multiple="multiple", not just multiple (although the majority of the browsers handles it right even when "quirk").

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 1
    Thank u very much finally I am able to upload multiple files. I added my original code with a little bit of changes in the execute method itself. I hope there is nothing wrong in doing that. Thank u again. – Pradnya Sep 15 '14 at 13:38
  • @Pradnya Nothing "wrong", except that it is highly coupled... actions should be small, and they should delegate the business to another layer (the business layer). Suppose tomorrow you want to do this operation from a lot of different actions... how would you proceed, copy and paste of all the logic ? [This answer may be worth reading](http://stackoverflow.com/a/13837913/1654265), hope that helps :) – Andrea Ligios Sep 15 '14 at 14:17
0

use array for multiple uploading.

<td><input type="file" name="file[]" class="multi" id="reqFile"></td>
Mohit
  • 1,185
  • 2
  • 11
  • 25
  • and then what changes in action method? – Pradnya Sep 15 '14 at 07:01
  • please check this thread you will get some idea.. [link](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet) – Mohit Sep 15 '14 at 07:11