如何使用JavaServlet3.1将多个文件插入oracle数据库

q1qsirdb  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(94)

我再一次需要你的帮助。我正在尝试将多个文件插入mysql db表。我设法在db表中插入了1个文件-1行,但是我不知道如何在一个请求中插入多个文件-多行。
这是我的servlet的一部分,在这里我从html输入获取文件:

InputStream otherFileInputStream = null;            
Part otherFilesPart = request.getPart("other_files");

if (otherFilesPart != null && otherFilesPart.getSize() != 0) {
    otherFileInputStream = otherFilesPart.getInputStream();                
}

这是我用来将文件插入数据库的方法:

private int addOtherFiles(long personId, int userId) throws SQLException {
    int res = 0;
    con.setAutoCommit(true);
    String sql = "insert into person_other_files(person_fk, other_files, user_fk) values (?, ?, ?)";
    PreparedStatement ps = con.prepareStatement(sql);

    if (otherFileInputStream != null) {
        ps.setLong(1, personId);        
        ps.setBlob(2, otherFileInputStream);
        ps.setInt(3, userId);
        ps.executeUpdate();
        ps.close();
    }        
    return res;
}

有人能告诉我,当用户上传2或3个文件时,上传的文件应该有2-3行吗?请记住,我不能使用ApacheCommons上传文件。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题