0

I'm using file uplaoding concept using jsp,servlet.My jsp code is

<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>

and servlet code is

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

and I'm getting output like

 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

and my DB table structure like

+-------+-------------+------+-----+---------+-------+
| 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)

and table values

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

Here file is saving into specified folder with rename and values are coming from JSP page but values are not saving into database table. I want to save the form values into database table row wise, so please give some suggestion to solve this problem. I'm spending more time on this plse help me.

Hareesh
  • 125
  • 3
  • 3
  • 13

0 Answers0