将值保存到Iterator的数据库行中

时间:2014-03-07 12:52:57

标签: java jsp servlets

我正在使用jsp,servlet的文件uplaoding概念。我的jsp代码是

<form name="form1" method="post" enctype="multipart/form-data" action="fileupload.sci"> 
<input type="text" name="name" id="name" />
<input type="text" name="enter" id="jam" />
<input type="file" name="ImageFile" id="ImageFile" />
<input type="submit" name="submit" value="submit" />
</form>

和servlet代码是

public class FileAction {
    public void newevent(HttpServletRequest request, HttpServletResponse response) {
        RandomGen ran = new RandomGen();

        try {
            Connection con = Singleton.getMySqlConnection();
            Statement st = con.createStatement();
            String updir = "D:\\don\\fileupload1\\WebRoot\\upload";
            File dir = new File(updir);
            dir.mkdir();

            String ImageFile = "";
            String itemName = "";
            String value = "";
            String dbpath = "";
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            if (!isMultipart) {
            } else {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List items = null;
                items = upload.parseRequest(request);
                Iterator itr = items.iterator();
                while (itr.hasNext()) {
                    FileItem item = (FileItem) itr.next();
                    if (item.isFormField()) {
                        String name = item.getFieldName();
                        value = item.getString();
                        System.out.println("Field value: " + item.getString());
                        if (name.equals("ImageFile")) {
                            ImageFile = value;
                        }

                    } else {
                        itemName = item.getName();
                        File savedFile = new File("D://don//fileupload1//WebRoot//upload//" + itemName);
                        String[] filetype = itemName.split("\\.");
                        System.out.println("Filetype :" + filetype[1]);
                        String ext = "." + filetype[1];
                        itemName = ran.generateRandomFileName().concat(ext);
                        savedFile.renameTo(new File("D://don//fileupload1//WebRoot//upload//" + itemName));
                        String path = "D://don//fileupload1//WebRoot//upload//" + itemName;
                        File Rsavedfile = new File(path);
                        dbpath = "upload//" + itemName;
                        System.out.println("path=" + path);
                        System.out.println("dbpath=" + dbpath);
                        item.write(Rsavedfile);
                        item.write(Rsavedfile);
                    }
                    st.executeUpdate("insert into test values ('" + dbpath + "','" + value + "','" + value + "')");
                }

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

我的输出就像

 this requst is coming from: /fileupload1/fileupload.sci
   Field value: POOSA Rajesh
value: POOSA Rajesh
Field value: POOSA Rajesh
Field value: hhhhhh
value: hhhhhh
Field value: hhhhhh
Filetype :docx
path=D://don//fileupload1//WebRoot//upload//98745386552_6_7_5_47.docx
dbpath=upload//98745386552_6_7_5_47.docx

和我的数据库表结构一样

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| path  | varchar(45) | NO   | PRI | NULL    |       |
| name  | varchar(45) | YES  |     | NULL    |       |
| enter | varchar(45) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

和表值

+-----------------------------------+------+-------+
| path                              | name | enter |
+-----------------------------------+------+-------+
| upload//98745386552_6_7_5_47.docx | NULL | NULL  |
+-----------------------------------+------+-------+
1 row in set (0.00 sec)

此处文件正在保存到具有重命名的指定文件夹中,并且值来自JSP页面,但值未保存到数据库表中。我想将表单值保存到数据库表中,所以请提出一些建议来解决这个问题。我花了更多时间在这个plse上帮助我。

0 个答案:

没有答案