to obtain image path
i have made a web application in which you can upload a file and i have used File image = new File(image); here String image = request.getParameter("file"); and file is the name of FILESELECT button or BROWSE button . and i am expecting to obtain the complete path of the image from FILE that i have browsed but instead i m gettng just the name of the image.
this is my index.jsp page
<p><%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"></p>
<p><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<FORM ACTION="upload" METHOD=POST>
<br><br><br>
<center><table border="2" >
<tr>
<center>
<td colspan="2"><p align="center">
<B>UPLOAD THE FILE</B>
</td>
</center>
</tr>
<tr>
<td>
<b>Choose the file To Upload:</b>
</td>
<td>
<INPUT NAME="file" TYPE="file" value="">
</td>
</tr>
<tr>
<td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p>
</td>
</tr>
</table>
</center>
</FORM>
</body>
</html></p>
<p>and this is my servlet:upload.java
package controller;</p>
<p>import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;</p>
<p>public class upload extends HttpServlet {</p>
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
String imageUrl = request.getParameter("file");
Connection connection = null;
String connectionURL = "jdbc:mysql://127.0.0.1:3306/skill_tracker";
ResultSet rs = null;
PreparedStatement psmnt = null;
// declare FileInputStream object to store binary stream of given image.
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
// create a file object for image by specifying full path of image as parameter.
File image = new File(imageUrl);
out.println(image);
FileInputStream fis = new FileInputStream(image);
psmnt = connection.prepareStatement("insert into pic values(?,?)");
psmnt.setInt(1,'1');
psmnt.setBinaryStream (2, (InputStream)fis, (int)(image.length()));
/* executeUpdate() method execute specified sql query. Here this query
insert data and image from specified address. */
int s = psmnt.executeUpdate();
if(s>0) {
out.println("Uploaded successfully !");
}
else {
out.println("unsucessfull to upload image.");
}
}
catch (Exception ex)
{
out.println("Found some error : "+ex);
}
}
<p>}</p>
View Answers
October 29, 2010 at 11:15 AM
Hi Friend,
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="../UploadServlet" 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)UploadServlet.java:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UploadServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
PrintWriter out = response.getWriter();
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("\""));
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 ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
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) {
out.println("Uploaded successfully !");
}
else{
out.println("unsucessfull to upload file.");
}
}
catch(Exception e){e.printStackTrace();}
}
}
}
Thanks
October 29, 2010 at 6:18 PM
dat is very difficulto understand wat i have done is very simple...my code works on some system perfectly but on my system it has dis problm..if u can plz tell me waT can i do to get the complete path??
September 8, 2012 at 11:07 AM
Is it possible to do the same jsp function with php??? just curious...:-)
Related Tutorials/Questions & Answers:
to obtain image path to
obtain image path i have made a web application in which you can... or BROWSE button . and i am expecting to
obtain the complete
path of the
image from...");
// create a file object for
image by specifying full
path of
image Advertisements
Full path of image to save it in a folderFull
path of
image to save it in a folder Sir ,I am trying to upload... to find that
image path &upload it as well.
I am just a beginner in jsp...(p2.getContentType());
String type=sc.next();
try
{
String
path inserting an path of an image in database - JDBCinserting an
path of an
image in database hello
kindly help related... to save it in folder..but can you plz tell me how an the full
path of
image can... an
image using web cam....
and when the
image is saved in a project at the same
how to store image upload path to mssql databasehow to store
image upload
path to mssql database hi there!!,
i need help in storing the
image upload
path into the database. basically i just use file select to upload the
image from jsp to database. however when i click submit
how to store image upload path to mssql databasehow to store
image upload
path to mssql database hi there!!,
i need help in storing the
image upload
path into the database. basically i just use file select to upload the
image from jsp to database. however when i click submit
how to store image upload path to mssql databasehow to store
image upload
path to mssql database hi there!!,
i need help in storing the
image upload
path into the database. basically i just use file select to upload the
image from jsp to database. however when i click submit
how to store image upload path to mssql databasehow to store
image upload
path to mssql database hi there!!,
i need help in storing the
image upload
path into the database. basically i just use file select to upload the
image from jsp to database. however when i click submit
image is display from path of mysql databaseimage is display from
path of mysql database <%@ page import="java.io.,java.sql.,java.util.zip.*" %>
<%
String saveFile="";
String..._
path) values(?)");
psmnt.setString(1, ff.getPath());
int s = psmnt.executeUpdate
pls provide common path to set image in flex - XMLpls provide common
path to set
image in flex hi,
pls provide common setpath to
image in flex.when i give ful
path like these
C:\eclipse\workspace... the coding in mxml to set common
path of
image in flex
Java get Absolute Path
Java get Absolute
Path
In this section, you will study how to
obtain the absolute
path...
file.getAbsolutePath() returns the absolute
path of the given file.
ADS
imageimage how to add the
image in servlet code
ImageImage how to insert
image in xsl without using xml. the
image was displayed in pdf..Please help me
ImageImage how to insert
image in xsl without using xml. the
image was displayed in pdf..Please help me
ImageImage how to insert
image in xsl without using xml. the
image was displayed in pdf..Please help me
image image Dear every body please help me how to add and retrive
image and video into oracle 11g using jsp
image retreivalimage retreival I ve stored the
path of
image and audio in mysql database.
how to retrive it and display...
Can u pls help me out
path classpathpath classpath EXPLAIN
PATH AND CLASSPATH ? DIFF
send me ans plz...,
Path is system wide variable that tells where to find your commands.
Lets... be in
path.
While Classpath is Enviroment Variable that tells JVM or Java Tools where
Path was not foundPath was not found The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path
image in jsp - JSP-Servletimage in jsp i m storing
path of
image in my database.. but when i m trying to display
image using that
path image is not getting... i m storing
path like c:\
image\a.jpg ... and i m using tag... how to get
Image retrieveImage retrieve HI..
store
image path/data Java Coding. ... It's supposed to take the
image, store it in a directory as well as pass the
image path to mysql database...
Now I want to retrieve the data from directory using
path Java IO Path in the above
image the absolute
path will be C:\user\user2\xyz.txt
Relative
Path... in the above
image the relative
path will be
\user2\xyz.txt. The relative
path...Java IO
Path
In this section we will discuss about the Java IO
Path.
Storage
Clip of image in an
image. To give the
path with straight line, we have used the class
GeneralPath... Clip of
image
In this section, you will studied how to show a clip of
image substitute image linksubstitute
image link How can i display a substitute
image if there is no
image found?
<
Image Source="{Binding
Path, FallbackValue... a default
image or link in the
image source file
display image on jspdisplay
image on jsp how to display
image from database which
path is coming in string.
i want to display
image and its discription and price.
image is like link. on jsp . i am trieving that
image path by which
image How to get path of a file in iOS?How to get
path of a file in iOS? Hi
iOS project I have added a file abg.jpg, Now I want to know the
path of the
image file when installed in iOS.
Pl let's know the code.
Thanks
Hi,
You can use following code
ModuleNotFoundError: No module named 'path'ModuleNotFoundError: No module named '
path' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
path'
How to remove the ModuleNotFoundError: No module named '
path' error
How to store url path?How to store url
path?
Image is stored in physical directory like...
path like this String file = "http://www.queen.com/website/screenshots/" + username + createTimeStampStr() + ".PNG";
this my program
public class
Image path - Java Beginnersmeaning of
path and classpath what is the meaning of
path and classpath. How it is set in environment variable.
Path and ClassPath in in JAVAJava ClassPath Resources:-http://www.roseindia.net/java/java-classpath.shtml
Fetching image from databaseFetching
image from database I have uploaded
image path and
image name in database so, now how can i display that
image using JSP or HTML page(is it possible to display using tag using concatination).
image path i have stored
path problem - Java Beginnerspath problem I dont know how to set the
path. What
path should we...-FINAL-20081019.jar in jdk's lib folder.
I entered
path as "C:\Program Files\Java\jdk1.6.0_07\lib" , is this correct?
Becoz even after this
path compilation
PHP hide file pathPHP hide file path PHP to read a
path and convert that to the virtual link
Fileupload from source path to destination pathFileupload from source
path to destination path first we will create... source
path &Destination
path fields and BOTH INPUT TYPES ARE "TEXT" we will give source
path as statically where the .doc or .rtf files
path will be their.and
storing images in directory,saving path in db2storing images in directory,saving
path in db2 i am working in a web... in a folder and its
path(relative/absolute) in my DB2 database. and when the user logins, i shall retrieve the
image and show it as the profile
image again
browse imagebrowse image how to browse the
image in
image box by browse button and save
image in database by save button by swing
import java.sql.... java.awt.image.*;
import java.awt.event.*;
public class UploadImage extends JFrame {
Image browse imagebrowse image how to browse the
image in
image box by browse button and save
image in database by save button by swing
import java.sql.... java.awt.image.*;
import java.awt.event.*;
public class UploadImage extends JFrame {
Image