nikita anand
code required
1 Answer(s)      a year ago
Posted in : Java Beginners

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 Pages:
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
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
Help Required - Swing AWT
the password by searching this example's\n" + "source code
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
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
HTML5 input autofocus, html5 input required.
HTML5 input autofocus, html5 input required. Introduction:Here we will introduce you to about the autofocus and required attribute of input tag. Type... required autofocus required Automatically focus when page load
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
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
Urgently required SEO
Freelance SEO       Position Vacant: Urgently required SEO Job Description ...; Website: http://www.roseindia.net  Reference ID: Urgently required
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
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
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
need code
is the required code: import java.util.*; class Vehicle { int
need code
is the required code: import java.util.*; class Vehicle { int
need code
is the required code: import java.util.*; class Vehicle { int
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
jQuery required validation
jQuery required Validation: jQuery "required" is the method...;/option>. Getting Started with jQuery required : In this example there are the following steps which are used to make jQuery required. This example  Makes
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
Javascript validation code
..it should display enter the field required Kindly please send me the code...Javascript validation code  Hi, I need a javascript validation code...)submit button All should display enter the field required(like name required
Editor help required. - Java Server Faces Questions
Editor help required.  I am having problem with editor used to design JSP page in JSF applications. I want to open JSP page with Web Page Editor... are they? Please Help me.... Thanks In advance
Javascript validation code
)submit button All should display enter the field required(like name required,email required etc) Kindly please send me the code as soon as possible...Javascript validation code  Hi, I need a javascript validation code
html5 required attribute, Example of required attribute of &lt;textarea&gt; tag in html5
html5 required attribute, Example of required attribute of <textarea> tag in html5 In this tutorial, We will introduce you about the required attribute...; tag: <textarea required="value">Text Here<
sample JSP&Servlet application required? - JSP-Servlet
sample JSP&Servlet application required?  hey all iam new to jsp&servlets i did read some tutorials about the basics and i want any simple tutorial or a project that integrate both jsp&servlets any help?  Hi Friend
Required help about the concept of JSP page reloading
Required help about the concept of JSP page reloading  Hi, We have one application with Websphere portlet Factory generated JSP as front end, Java in the business layer and finally DB2 as back-end. In certain cases
Required help about the concept of JSP page reloading
Required help about the concept of JSP page reloading  Hi, We have one application with Websphere portlet Factory generated JSP as front end, Java in the business layer and finally DB2 as back-end. In certain cases
jsp code - JSP-Servlet
jsp code  I need code for bar charts using jsp. I searched some code but they are contain some of their own packages. Please give me asimple code for bar charts and what are the topics required for that charts.  Hi
'else' Not Required
Java Notes'else' Not Required 'else' is not required In this section we will learn about if else statement in more detail with example. It is not necessary to have the else part of an if statement. Maybe only 50
J2ME code hint - MobileApplications
J2ME code hint  I am required to make a mobile application for encryption and decryption of sms messages using RSA. The last day for submission is 9th of March, 2009... If anyone can plz help me in this regard plz let me
Java code - Development process
Java code  in GWT ,which classes would be required to implement a screen showing a Grid with its cells derived from a JDBC select to a database table... of: SELECT name, phone_number FROM contact; Can i get the code
Java Code - Java Beginners
Java Code  Code a for loop that adds the integers that are multiples of 5 starting with 50 and ending with 5. Each time the sum is increased, print it on a line at the console. Be sure to declare and initialize any required
Java Code - Java Beginners
Java Code  Code nested for loops that calculate the area... at the console followed by a space. Be sure to declare and initialize any required variables.  Hi friend, Code to Area of Rectangle : class
source code - JSP-Servlet
source code  I want source code for Online shopping Cart...-down list, enter the required quantity and add the selected products to the cart... Rakesh, I am sending a link where u will found complete source code
The required JDBC drivers to establish connectivity to my database part of the JDK.
The required JDBC drivers to establish connectivity to my database part of the JDK.  Are all the required JDBC drivers to establish connectivity to my database part of the JDK
Sample Code - Development process
Sample Code   Hi Friend, Give sample code DTO... the performance of this user request is to package all the required data... a new class for each DTO. 2.Write or generate the code for the data transfer
Simple ATM Java Code...
Simple ATM Java Code...  You are required to write a Graphical User Interface that simulates an ATM by building on the program you wrote.... It's a college assignment and needs to be written with the least amount of 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
How to install Hibernate-3 and jars required for eclipse environment. And is jboss jars are necessary..?
How to install Hibernate-3 and jars required for eclipse environment... required? I am searching in Google so may jars there some are related jboss... structure and jars required mentioned
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: import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import java.io.

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.