code required

code required

I am making a blog in java using netbeans and database connectivity with SQL Server 2008. I am using JSP pages for the same and not the struts framework I want to provide option for the users to upload a photo(only .jpg,,png etc files) But i am not getting how to provide this option on the JSP page Will you plz help me out by providing me with the code?

View Answers

May 2, 2012 at 10:19 AM

Jsp Upload Image

1)page.jsp:

<%@ page language="java" %>
<HTML>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

2)upload.jsp:

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
String saveFile="";
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;
while (totalBytesRead < 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;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<% out.println(saveFile);%></td></tr></table>
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/test";;
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
File f = new File(saveFile);
psmnt = connection.prepareStatement("insert into file(file_data) values(?)");
fis = new FileInputStream(f);
psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length()));
int s = psmnt.executeUpdate();
if(s>0) {
System.out.println("Uploaded successfully !");
}
else{
System.out.println("Error!");
}
}
catch(Exception e){e.printStackTrace();}
}
%>









Related Tutorials/Questions & Answers:
code required.!
Java Script phone number entered by person should be in numeric only. code required!  i am using javascript coding on jsp page. i want that the phone number entered by person should be in numeric only..can i get code
code required
code required  I am making a blog in java using netbeans and database connectivity with SQL Server 2008. I am using JSP pages for the same... page Will you plz help me out by providing me with the code
Advertisements
code required in servlet
code required in servlet  hello... you provided me the answer with javascript.. i want to check which button is clicked in the servlet.. i have only 1 servlet an in that separate codes for different button clicks.. can u plzzz
ModuleNotFoundError: No module named 'required'
ModuleNotFoundError: No module named 'required'  Hi, My Python... 'required' How to remove the ModuleNotFoundError: No module named 'required... to install padas library. You can install required python with following
E BOOK REQUIRED
E BOOK REQUIRED  Wicket in Action pdf required Urgently. Thnx
help required in coding
help required in coding  hi..... I want to display output in graphical format . Can u tell me how it can be done? using for project java swings. plzzzzzz helpp
maths required for data science
maths required for data science  Hi, I am beginner in Data Science... required for data science Try to provide me good examples or tutorials links so that I can learn the topic "maths required for data science". Also
statistics required for data science
statistics required for data science  Hi, I am beginner in Data...: statistics required for data science Try to provide me good examples or tutorials links so that I can learn the topic "statistics required for data
degree required for data scientist
degree required for data scientist  Hi, I am beginner in Data...: degree required for data scientist Try to provide me good examples or tutorials links so that I can learn the topic "degree required for data scientist"
data scientist required degree
data scientist required degree  Hi, I am beginner in Data Science... scientist required degree Try to provide me good examples or tutorials links so that I can learn the topic "data scientist required degree". Also tell
education required for data scientist
education required for data scientist  Hi, I am beginner in Data...: education required for data scientist Try to provide me good examples or tutorials links so that I can learn the topic "education required for data
math required for ai
math required for ai  Hi, I am beginner in Data Science and machine learning field. I am searching for the tutorials to learn: math required... the topic "math required for ai". Also tell me which is the good training
Is math required for data science?
Is math required for data science?  Hi, I am beginner in Data...: Is math required for data science? Try to provide me good examples or tutorials links so that I can learn the topic "Is math required for data science?"
property datasource required - Spring
property datasource required  Hi I am using java springs I am using mysql as backend I am getting one error when i compile the application.property datasource required. This is my dispatcher-servlet.xml
Help Required - Swing AWT
the password by searching this example's\n" + "source code
Code
Code  code for connecting c lang to database
code
code    how to write this in java
code
code  code for android sample program
code for this is
code for this is  a b c d c b a a b c c b a a b b a a a code
code
code  please provide code for custom tags.by using currdate tag we need to get current date?please give me code
code
code   hi I need help in creating a java code that reminds user on a particular date about their festival. i have no clue of how to do it.. am looking forward to seek help from you
code
the correct code for a program.The output of the program is listed below...: {Block 5} ADDRESS-3: {San Juan} POST CODE:{6745} ENTER CODE (XX TO stop) CODE:{FF1} QUANTITY:{2} CODE:{TR4} QUANTITY:{1} CODE:XX INVOICE FOR YOUR ORDER Harry
code
code to create the RMI client on the local machine: import java.rmi.*; public... : "+e); } } } However, when the preceding code is executed it results... the correct code
code
code  i have four textboxes.whenever i click on up,down,left or down arrows then the cursor move to another textbox based on the key pressed.i want code for this in javascript
code
line of code to print the amount he should be paid as allowance
code
code  write a program to encrypt and decrypt the cipher text "adfgvx"   Hi Friend, Try the following code:ADS_TO_REPLACE_1 import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import
Urgently required SEO
Freelance SEO       Position Vacant: Urgently required SEO Job Description ...  Reference ID: Urgently required SEO ADS_TO_REPLACE_4
Hibernate required jar files - Hibernate
Hibernate required jar files  Hi, What are the jar files are required for hibernate and Springs application. I want to run complete application...-spring/index.shtml Download the full code from the above link and extract
steps required to execute a query in JDBC
steps required to execute a query in JDBC  What are the steps required to execute a query in JDBC
What skills are required to be a Hadoop developer?
What skills are required to be a Hadoop developer?  Hi, How to become a good Hadoop Developer? What skills are required to be a Hadoop developer? Thanks
ModuleNotFoundError: No module named 'yxs_required'
ModuleNotFoundError: No module named 'yxs_required'  Hi, My Python... 'yxs_required' How to remove the ModuleNotFoundError: No module named 'yxs_required' error? Thanks   Hi, In your python
Date Format required to change in java
Date Format required to change in java  can we change the format of date like sql as per our requirements???If yes then please give the detail solution
the skills required to master data science is/are
the skills required to master data science is/are  Hi, I am beginner... to learn: the skills required to master data science is/are Try to provide me...;the skills required to master data science is/are". Also tell me which is the good
Spring @Required Annotation
Spring @required Annotation In this tutorial you will see about spring @required annotation with an example. The @Required when written on top of setStudent... that org.springframework.beans.factory.BeanInitializationException: Property 'student' is required for bean 'college
Tools required to build Simple Cart
Tools required to build Simple Cart       Shopping cart application is written in Java and so... tools are required--- Java Development Kit (JDK), 1.4 or later. Apache Ant
jQuery required validation
jQuery required Validation: jQuery "required" is the method...;/option>. Getting Started with jQuery required :ADS_TO_REPLACE_1 In this example there are the following steps which are used to make jQuery required
how to show required fields indicate as * in struts2
how to show required fields indicate as * in struts2  how to show required fields indicate as * in struts2
Why binding attribute is required in jsf frame work
Why binding attribute is required in jsf frame work  Why binding attribute is required in jsf frame work. And what use of binding attribute
ModuleNotFoundError: No module named 'django-login-required'
ModuleNotFoundError: No module named 'django-login-required'  Hi...: No module named 'django-login-required' How to remove the ModuleNotFoundError: No module named 'django-login-required' error? Thanks   Hi
ModuleNotFoundError: No module named 'django-login-required-middleware'
ModuleNotFoundError: No module named 'django-login-required-middleware' ...: ModuleNotFoundError: No module named 'django-login-required-middleware' How to remove the ModuleNotFoundError: No module named 'django-login-required-middleware'
ModuleNotFoundError: No module named 'django-login-required-middleware'
ModuleNotFoundError: No module named 'django-login-required-middleware' ...: ModuleNotFoundError: No module named 'django-login-required-middleware' How to remove the ModuleNotFoundError: No module named 'django-login-required-middleware'
ModuleNotFoundError: No module named 'flask-required-args'
ModuleNotFoundError: No module named 'flask-required-args'  Hi, My... named 'flask-required-args' How to remove the ModuleNotFoundError: No module named 'flask-required-args' error? Thanks   Hi
ModuleNotFoundError: No module named 'odoo10-addon-account-analytic-required'
ModuleNotFoundError: No module named 'odoo10-addon-account-analytic-required...: ModuleNotFoundError: No module named 'odoo10-addon-account-analytic-required' How...-analytic-required' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'odoo10-addon-account-partner-required'
ModuleNotFoundError: No module named 'odoo10-addon-account-partner-required...: ModuleNotFoundError: No module named 'odoo10-addon-account-partner-required' How...-required' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'odoo11-addon-account-analytic-required'
ModuleNotFoundError: No module named 'odoo11-addon-account-analytic-required...: ModuleNotFoundError: No module named 'odoo11-addon-account-analytic-required' How...-analytic-required' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'odoo11-addon-account-analytic-required'
ModuleNotFoundError: No module named 'odoo11-addon-account-analytic-required...: ModuleNotFoundError: No module named 'odoo11-addon-account-analytic-required' How...-analytic-required' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'odoo11-addon-account-partner-required'
ModuleNotFoundError: No module named 'odoo11-addon-account-partner-required...: ModuleNotFoundError: No module named 'odoo11-addon-account-partner-required' How...-required' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'odoo11-addon-account-partner-required'
ModuleNotFoundError: No module named 'odoo11-addon-account-partner-required...: ModuleNotFoundError: No module named 'odoo11-addon-account-partner-required' How...-required' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'odoo12-addon-account-analytic-required'
ModuleNotFoundError: No module named 'odoo12-addon-account-analytic-required...: ModuleNotFoundError: No module named 'odoo12-addon-account-analytic-required' How...-analytic-required' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'odoo12-addon-account-partner-required'
ModuleNotFoundError: No module named 'odoo12-addon-account-partner-required...: ModuleNotFoundError: No module named 'odoo12-addon-account-partner-required' How...-required' error? Thanks   Hi, In your python environment you

Ads