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

6 Years
Discussion Span
Last Post by yuvi2288


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.*" %>
                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);
                    //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) {
                        value = contents.toString();
                        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);

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.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.