Problem with picture

Problem with picture

View Answers

July 6, 2009 at 12:09 PM

Hi Friend,

To upload and insert the image into database, try the following code:

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="2" >
<tr><center><td colspan="2"><p align="center"><B>UPLOAD THE FILE</B><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">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

2)upload.jsp:
<%@ page import="java.io.*" %>
<%@ page import="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;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
out.println(saveFile);
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;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

%><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</b>
<% out.println(saveFile);%></td></tr></table>
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://192.168.10.112:3306/file";;
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("unsucessfull to upload file.");
}
}
catch(Exception e){e.printStackTrace();}
}
%>

Thanks









Related Tutorials/Questions & Answers:
Problem with picture - Java Beginners
Problem with picture   Hi, I Develope a School Automated System... (MSSQL), i make the registrar to be able to upload the student picture from the System picture Folder and display it on the User Interface. Now, i want the registrar
Problem with picture - Java Beginners
Problem with picture   Hi, I Develope a School Automated System... (MSSQL), i make the registrar to be able to upload the student picture from the System picture Folder and display it on the User Interface. Now, i want the registrar
Advertisements
Problem with picture - Java Beginners
Problem with picture   Hi, I Develope a School Automated System... (MSSQL), i make the registrar to be able to upload the student picture from the System picture Folder and display it on the User Interface. Now, i want the registrar
picture in background in a HTML
picture in background in a HTML  How is picture displayed in background in a HTML
Upload the picture - JSP-Servlet
Upload the picture  Dear Sir, How to upload the picture in the database a webpage and i want to show that picture another page. what i...; Hi Friend, Please visit the following link to solve your problem. Here
How to make a glossy picture effect, make a glossy picture effect, glossy picture effect
How to make a enhance picture       If you have any problem to make glossy picture effect, you should follow my this example because I have made it with some easy steps. Picture
make a picture a link
make a picture a link  hello, What is the way to make a picture a link?   hii,ADS_TO_REPLACE_1 This is the tab for image linking <A HREF="http://www.roseindia.net"><IMG SRC="image.gif"></A>
How to convert a picture into a shape, convert a picture into a shape, picture shape
How to convert a picture into a shape       If you want to use a picture shape but you are not able for that, don't worry. You will learn easily to convert a picture into shape. Take
How to convert a picture to drawing, convert a picture to drawing, picture to drawing
How to convert a picture to drawing       Learn here a simple processing to make a color picture.... Picture: Take a picture to modify.ADS_TO_REPLACE_1 Filter effect: Go to Filter
How to make a enhance picture, make a enhance picture, enhance picture
How to make a enhance picture       In this example you will learn a easy way to make a enhance picture effect. Picture: Take a picture to modify.ADS_TO_REPLACE_1 Bright
How to make frame of the picture, make frame of the picture, frame of the picture
How to make frame of the picture   ..., I have mention here all the steps to make this effect. Take picture: First take a picture to framed.ADS_TO_REPLACE_1 Selection: Make selection
Call Picture into java
Call Picture into java  Morning sir, I want to view my picture(longblob) from My DB Mysql, I have a code like this. try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc
inserting picture in ms access
inserting picture in ms access  hi i am sanatan, how to insert picture in ms access by jsp.   <%@page import="java.sql.*,java.io.*"%> <% try{ InputStream sImage; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver
Call Picture into java
Call Picture into java  Morning sir, I want to view my picture(longblob) from My DB Mysql, I have a code like this. try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc
ModuleNotFoundError: No module named 'mdx_picture'
ModuleNotFoundError: No module named 'mdx_picture'  Hi, My Python... 'mdx_picture' How to remove the ModuleNotFoundError: No module named 'mdx_picture' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'picture-cutter'
ModuleNotFoundError: No module named 'picture-cutter'  Hi, My... named 'picture-cutter' How to remove the ModuleNotFoundError: No module named 'picture-cutter' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'The-Big-Picture'
ModuleNotFoundError: No module named 'The-Big-Picture'  Hi, My... named 'The-Big-Picture' How to remove the ModuleNotFoundError: No module named 'The-Big-Picture' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'djangocms-picture'
ModuleNotFoundError: No module named 'djangocms-picture'  Hi, My... named 'djangocms-picture' How to remove the ModuleNotFoundError: No module named 'djangocms-picture' error? Thanks   Hi, In your
How to make a high contrast picture, make a high contrast picture, high contrast picture
How to make a high contrast picture       Make a fade picture to high contrast by this example, it has a new way to make it so follow now. Picture: Take a picture to modify, I have
How to make a curl corner of the picture, make a curl corner of the picture, curl corner of the picture
How to make a curl corner of the picture   ... of the picture, it is very easy by the help of this example. Picture: Take a picture to curl the corner of the picture.ADS_TO_REPLACE_1 Selection: Make
How to make a bright and contrast picture, make a bright and contrast picture, bright and contrast picture
How to make a bright and contrast picture       If any picture has darkness and you... a technique to reduce the darkness so let's try. Take a Picture: Take
How to make a golden effect on the picture, make a golden effect on the picture, golden effect on the picture
How to make a golden effect on the picture   ... to make a golden effect on the picture. New file: Insert a picture to modify, I am using here a monkey picture to modify.ADS_TO_REPLACE_1 Blur effect: Make
How to design a frame of the picture.
How to design a frame of the picture.       This is a picture frame example, it will help you to make a picture frame easily because it has some simple steps. If you want
How to make grass effect on the picture, make grass effect on the picture, grass effect on the picture
How to draw grass effect on the picture       If you want to draw grass on the land... in this example. Take a picture: Capture any picture as you like.ADS_TO_REPLACE_1
How to make colorful a black&white picture, make colorful a black&white picture, colorful a black&white picture
: Capture any black&white picture to colored.ADS_TO_REPLACE_1 Selection: Press (Q... How to make colorful a black&white picture       If you have a black&white picture, make
How to insert a car in the picture, insert a car in the picture, insert a car
How to insert a car in the picture   ... how to insert an object on the another place. Picture: Take a picture of the road... the car picture and paste on the road picture as here. Decrease size: Press Ctrl
An Applet program for Drawing picture & graph - Applet
An Applet program for Drawing picture & graph  How can I write an Applet program for drawing picture and graph having various colours and tools provided
we make a picture as a background on my web pages
we make a picture as a background on my web pages  How do we make a picture as a background on my web pages
I need add my java program with a picture.
I need add my java program with a picture.  Good evng Frnds Friends i created 1 jar file in my desktop. I need add this program with 1 picture. Whenever the user double clicking on that picture The program must start instead
ModuleNotFoundError: No module named 'picture_db_scripts'
ModuleNotFoundError: No module named 'picture_db_scripts'  Hi, My... named 'picture_db_scripts' How to remove the ModuleNotFoundError: No module named 'picture_db_scripts' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'picture-lake-weibo'
ModuleNotFoundError: No module named 'picture-lake-weibo'  Hi, My... named 'picture-lake-weibo' How to remove the ModuleNotFoundError: No module named 'picture-lake-weibo' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'git-big-picture'
ModuleNotFoundError: No module named 'git-big-picture'  Hi, My... named 'git-big-picture' How to remove the ModuleNotFoundError: No module named 'git-big-picture' error? Thanks   Hi, In your python
Applet program for drawing picture and graph - Applet
Applet program for drawing picture and graph  Please send me the program(code) of drawing picture and graph in Applet.  Hi Friend, Please visit the following links: http://www.roseindia.net/java/example/java
NetBeans change the picture on one single label
NetBeans change the picture on one single label  I wantted to insert Images using if condition on a button that is performed to change the picture on one single label. how shuold i do this ? and also give me an example
How to capture picture from webcam using java
How to capture picture from webcam using java  i want catch the image from web-cam using jsp, If anyone knows please answer me
to get picture from ms access database by jsp-servlet....
to get picture from ms access database by jsp-servlet....  I have inserted a picture in ms access data base,,,how we can retrieve that picture by using jsp
Change Background Picture of Slide Using Java
Change Background Picture of Slide Using Java  ...; In this example we are going to create a slide then change background picture of the slide. In this example we are creating a slide. In this slide we are inserting a picture
problem in onlinetest...
problem in onlinetest...  hi, my code getting the problem is ,when user leave a question then the server puts error... pls help me
Java to insert picture to database - JSP-Servlet
Java to insert picture to database  Hi Guys, Please assist me on this. Below is the code I wanted to use to insert picture into Ms Sql server 2000 but after the insertion, I got this in the database (back-end
How save,get picture from database in my jsp page?
How save,get picture from database in my jsp page?  How i save picture in db after browsing it,and also how i get it on my other jsp page
javascript problem
javascript problem  hi guys My problem is that I am using radio button for yes or no but i want to do that if I press yes button then enter... enter please help me this problem
problem in validation
problem in validation  sir/madam, i m using struts-1.3.10. i m getting a problem my properties file is not found.... while i hav configuired it in struts-config.xml file, likeADS_TO_REPLACE_1 thanks n regards himanshu
Photoshop Tutorial : How to remove an object from a picture
How to remove an object from a picture   ... from a picture. If you want to remove an object, which you don't want to like in your picture then there is no need to worry. Just follow these easy steps
Problem with loginbean.jsp
Problem with loginbean.jsp  http://www.roseindia.net/jsp/loginbean.shtml - I am getting an error in loginbean.jsp.There is some error regarding .What is hello in this? Also in this example how
problem on strings
problem on strings  in string1 "123456 ABC DEF...",IN string2 "raveen". iwant to add string2 after 123456 in the first string based on index number.i need logic for this with out using StringBuffer concept. Thanks in advanace
compilation problem
compilation problem  struts 2.0 : i have included all 5 jars in lib folder,but its giving compilation problem that package open symphony doesnot... havenot set the build path.if this is the problem then tell me how to set
compilation problem
compilation problem  struts 2.0 : i have included all 5 jars in lib folder,but its giving compilation problem that package open symphony doesnot... havenot set the build path.if this is the problem then tell me how to set
Problem with cookies
Problem with cookies  Hello All, i need jsp code for RememberMe module of login. i am facing problem with cookies. so please if any one could guide me please help and provide mme the exact code.   Please visit
problem with package
problem with package  Dear sir, i have created one java file with package com.net; and i compiled the program.it showing the .class file in " net" folder.The problem is i want to use that .class file in some other
Design a picture of the sea water.
Design a picture of the sea water.       This example is about a sea water in which we are going to teach you to make a sea water. You all have seen sea water, but here we

Ads