answersLogoWhite

0

File Upload into Database in JSP?

Updated: 10/26/2022
User Avatar

Wiki User

11y ago

Best Answer

<%@page language="java" session="true"

import="java.io.*,java.util.*,java.io.*,java.sql.*,javax.servlet.*"%>

<%

//to get the content type information from JSP Request Header

String contentType = request.getContentType();

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

{

DataInputStream in = new DataInputStream(request.getInputStream());

//we are taking the length of Content type data

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

int lastIndex = contentType.lastIndexOf("=");

String boundary = contentType.substring(lastIndex + 1);

int pos;

//extracting the index of file

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;

// creating a new file with the same name and writing the content in new file

//FileOutputStream fileOut = new FileOutputStream(saveFile);

FileOutputStream fileOut = new FileOutputStream("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\incidentreportform_main\\upload"+saveFile+"");

fileOut.write(dataBytes, startPos, (endPos - startPos));

fileOut.flush();

fileOut.close();

//out.println(saveFile);

Connection con=null,con1=null;

Statement stmt=null,stmt1=null;

PreparedStatement ps=null,ps1=null;

ResultSet rs=null,rs1=null;

String sql="",sql1="",a="";

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:inficert","inficert","inficert");

stmt=con.createStatement();

String sno=request.getParameter("parameter");

int j1=0;

try

{

sql="UPDATE department set crftitle =('"+saveFile+"') where sno='"+sno+"'";

j1=stmt.executeUpdate(sql);

if(j1!=0)

{

%>

<script type="text/javascript">

alert('Successfully Uploaded');

top.location.href = "selectlocation.jsp";

</script>

<%

}

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

%>

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: File Upload into Database in JSP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How validate and retrieve data from database in jsp?

We can do validation by using JavaScript. Here we are using function Validate(). Iam creating one JSP name index.jsp &amp; give a link to another JSP name basic.jsp. In that Jsp iam using Type 1 Jdbc Driver &amp; giving a database connection


How can you take data from a Java page to a HTML page simultaneously to the database and print that HTML file?

Your order is incorrect. The database exists and is populated with the data wanted on the page, Java extracts the data needed from the database and renders the page in the necessary HTML for the browser to view. You could see a JSP reference in the address for Jave Server Page or not.


How do you Write a java program to get and display all the information?

That depends a lot on what information you want to get, and where you want to get it from: from user input; from a text file; from some other kind of file; from a database; etc.That depends a lot on what information you want to get, and where you want to get it from: from user input; from a text file; from some other kind of file; from a database; etc.That depends a lot on what information you want to get, and where you want to get it from: from user input; from a text file; from some other kind of file; from a database; etc.That depends a lot on what information you want to get, and where you want to get it from: from user input; from a text file; from some other kind of file; from a database; etc.


What is JSP?

In the context of Computer Programming, JSP stands for Java Server Page. A JSP is basically an HTML file with Java code inserted to make a dynamic page.AnswerJacketed soft point in the context of bullets. AnswerJSP, Java Server Pages, is a language for mixing HTML/XML and Java in the same page. Basic JSP can look like this:...Welcome, ...where tells the server to execute the contents of the tag as Java and print the result to the page.See related link.


How do you convert HTML to JSP?

You cannot. A JSP Page contains a lot more contents than just HTML. It contains JavaScript, Java Scriptlets etc. So a HTML cannot be directly converted to a JSP unless someone sits and adds the other contents to the JSP that makes it a JSP

Related questions

How do you get the image file from the database with JSP?

Follow the steps of this article: codejava.net/coding/upload-files-to-database-servlet-jsp-mysql.


How to upload excel sheet data into a MySQL database using JSP and Servlets?

It is very easy to upload excel spreadsheet data into a MySQL database using JSP and servlets. Within Java there is a tool where you can designate the name and location of the file you would like to import. Once you furnish the file name Java server pages will do the work for you.


How do you upload database to Google docs?

You can upload a database to Google Docs as a .TXT file. You will preserve the data, but you will lose the database structure.


How to Update data from jsp into database?

can i get update coding in jsp??


What is a database in access?

An Access database is a relational database contained in a single file that you can upload to a directory on your Web server. People typically create an Access database file using Microsoft Access or FrontPage.


What is database in access?

An Access database is a relational database contained in a single file that you can upload to a directory on your Web server. People typically create an Access database file using Microsoft Access or FrontPage.


What is bulk loading in a database?

Usually, that is using a "load" file to import data into a database. That can be a flat, delimited file or other formats. The type of format used depends on the database. My database has data upload batch jobs scheduled daily.


How do you import jsp file in servlet?

I think the name of the jsp file is included in the web.xml of the servlet


Jsp file is text or xml based document?

A JSP file is a text based document with a .jsp extension. The file can contain HTML content, Java code and other text.


How can you compile the jsp file?

JSP file would be automatically compiled by an engine called Jasper in the servelt containter.


Can you create a database with only a form?

Yes , you can create a database using a form . But you have to do two things First make a form in html or jsp whatever you like. Then Connect the form to database using java database connectivity.Yes, we can create database with a form. First create a form using html or jsp then we can connect it to database using JDBC.


How validate and retrieve data from database in jsp?

We can do validation by using JavaScript. Here we are using function Validate(). Iam creating one JSP name index.jsp &amp; give a link to another JSP name basic.jsp. In that Jsp iam using Type 1 Jdbc Driver &amp; giving a database connection