Hi,

I have write the code to upload a file to database. I checked it by uploading a text document which is in my desktop and it is working and successfully uploaded to database.

File tomcat.txt has been uploaded and inserted into Database.

But when I tried to upload an another text document in the desktop, it is not working and throws exception as :

java.io.FileNotFoundException: reminder.txt (The system cannot find the file specified)

I think it is not a compiler error. It may be runtime error. how to overcome this?

Any help will be appreciated....

Difficult to answer without seeing code

Hi,

Thankh you for your interest to help me. here is my full code for uploading the file to mysql database.

<%@ page import="java.io.*,java.sql.*" %>
<html>
    <%
                String contentType = request.getContentType();

                if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {

                    DataInputStream in = new DataInputStream(request.getInputStream());
                    int formDataLength = request.getContentLength();
                    byte dataBytes[] = new byte[formDataLength];
                    int byteRead = 0;
                    int totalBytesRead = 0;
                    //this loop converting the uploaded file into byte code
                    while (totalBytesRead < formDataLength) {
                        byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                        totalBytesRead += byteRead;
                    }

                    String file = new String(dataBytes);
                    out.println(file);
                    //for saving the file name
                    String saveFile = file.substring(file.indexOf("filename=\"") + 10);

                    saveFile = saveFile.substring(0, saveFile.indexOf("\n"));

                    saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
                    
                    Connection con = null;
                    Statement pst = null;
                    String line = null;
                    String value = null;
                    try {
                        StringBuilder contents = new StringBuilder();
                        BufferedReader input = new BufferedReader(new FileReader(saveFile));
                        while ((line = input.readLine()) != null) {
                            contents.append(line);
                        }
                        value = contents.toString();
                        out.println(contents);
                        Class.forName("com.mysql.jdbc.Driver");
                        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
                        pst = con.createStatement();
                        pst.executeUpdate("insert into file values('" + value + "')");
    %><b>File     <% out.println(saveFile);%> has been uploaded and inserted into Database.</b>
    <%
                    } catch (Exception e) {

                        out.println("Exception : "+e);
                    }
                }
    %>
</html>

If I upload the tomcat.txt from my desktop, it will inserted to database. But for any other file except that particular text document, it will not added. It shows the error message like FILE NOT FOUND EXCEPTION.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.