title: How to store the file path and file name into the database(mysql) using jsp servlet code?
body: <p>I have uploaded the folders to the server folder so now i want to store the file path and file name into the Mysql database, and display stored files from database to the front end in a tree structured way.</p>
  
  <p>Here is code pls help</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>page.jsp
  
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;%@ page language="java" %&gt;
  &lt;%@page import="java.io.*,java.net.*"%&gt;
  &lt;HTML&gt;
  &lt;FORM ENCTYPE="multipart/form-data" ACTION="uploadandstore.jsp" METHOD=POST&gt;
  &lt;center&gt;
  &lt;table border="0" bgcolor=#ccFDDEE&gt;
  &lt;tr&gt;
  &lt;center&gt;&lt;td colspan="2" align="center"&gt;&lt;B&gt;UPLOAD THE FILE&lt;/B&gt;&lt;center&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;br&gt;&lt;br&gt;&lt;br&gt;
  &lt;tr&gt;&lt;td colspan="" align="center"&gt; &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;&lt;b&gt;Choose the file&lt;/b&gt;&lt;/td&gt;
  &lt;td&gt;&lt;INPUT NAME="file" TYPE="file"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;&lt;td colspan="2" align="center"&gt; &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td colspan="2" align="center"&gt;&lt;input type="submit" value="Submit"&gt; &lt;/td&gt;&lt;/tr&gt;
  
  
  &lt;table&gt;
  &lt;/center&gt;
  &lt;/FORM&gt;
  &lt;/HTML&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>uploadandstore.jsp
  
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;%@page import="java.io.*, java.sql.*"%&gt;
  &lt;%
  
  String saveFile="";
  String contentType = request.getContentType();
  if ((contentType != null) &amp;&amp; (contentType.indexOf("multipart/form-data") &gt;= 0)) {
  DataInputStream in = new DataInputStream(request.getInputStream());
  int formDataLength = request.getContentLength();
  byte dataBytes[] = new byte[formDataLength];
  int byteRead = 0;
  int totalBytesRead = 0;
  while (totalBytesRead &lt; formDataLength) {
  byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
  totalBytesRead += byteRead;
  }
  String file = new String(dataBytes);
  saveFile = file.substring(file.indexOf("filename=\"") + 10);
  saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
  saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
  int lastIndex = contentType.lastIndexOf("=");
  String boundary = contentType.substring(lastIndex + 1,contentType.length());
  int pos;
  pos = file.indexOf("filename=\"");
  pos = file.indexOf("\n", pos) + 1;
  pos = file.indexOf("\n", pos) + 1;
  pos = file.indexOf("\n", pos) + 1;
  int boundaryLocation = file.indexOf(boundary, pos) - 4;
  int startPos = ((file.substring(0, pos)).getBytes()).length;
  int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
  saveFile="C:/UploadedFiles/"+saveFile;
  File f = new File(saveFile);
  FileOutputStream fileOut = new FileOutputStream(f);
  fileOut.write(dataBytes, startPos, (endPos - startPos));
  fileOut.flush();
  fileOut.close();
  %&gt;
     
       &lt;%
   
     }
  %&gt;
      &lt;a href="viewFiles.jsp"&gt;View Files&lt;/a&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>viewFiles.jsp
  
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;%@ page import="java.io.*"%&gt;
  &lt;html&gt;
  &lt;table&gt;
  
  &lt;%
  File f = new File("C:/UploadedFiles/");
          File[] files = f.listFiles();
          for(int i=0;i&lt;files.length;i++){
              String name=files[i].getName();
              String path=files[i].getPath();
              
  %&gt;
  &lt;tr&gt;&lt;td&gt;&lt;%=name%&gt;&lt;/td&gt;&lt;td&gt;&lt;a href="download.jsp?f=&lt;%=path%&gt;"&gt;Download&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
       &lt;%
          }
  %&gt;
  &lt;/table&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>download.jsp
  
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;%@page import="java.io.*,java.net.*"%&gt;
  
  &lt;%!
  public static String getMimeType(String fileUrl)
      throws java.io.IOException, MalformedURLException 
    {
      String type = null;
      URL u = new URL(fileUrl);
      URLConnection uc = null;
      uc = u.openConnection();
      type = uc.getContentType();
      return type;
    }
  
  %&gt;
   &lt;%
      String file=request.getParameter("f");
      File f = new File (file);
      String filename=f.getName();
      String type=getMimeType("file:"+file);
  
      response.setContentType (type);
      response.setHeader ("Content-Disposition", "attachment; filename=\""+filename+"\"");
  
      String name = f.getName().substring(f.getName().lastIndexOf("/") ,f.getName().length());
      InputStream in = new FileInputStream(f);
          ServletOutputStream outs = response.getOutputStream();
  
          int bit = 256;
          int i = 0;
              try {
                      while ((bit) &gt;= 0) {
                          bit = in.read();
                          outs.write(bit);
                      }
                          } catch (IOException ioe) {
                          ioe.printStackTrace(System.out);
                      }
                          outs.flush();
                      outs.close();
                      in.close(); 
  
          %&gt;</code></pre>
  </div>
  </div>
  </p>