File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes I cannot obtain the enitre path of image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "I cannot obtain the enitre path of image" Watch "I cannot obtain the enitre path of image" New topic

I cannot obtain the enitre path of image

shrutishrma sharma

Joined: Oct 29, 2010
Posts: 1
hey i have a problem in obtaining the path of the image which i have to upload using jsp servlet...
actually the problem is ...i have used getParameter to get the image path and then using the image i store the image in database ...but getParameter is not returning me the complete path of the image instead it returns only the image name...
here is my code please check it as soon as possible ....eagerly wating for reply:
my index.jsp page
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<center><table border="2" >
<td colspan="2"><p align="center">
<b>Choose the file To Upload:</b>
<input type="file" name="file" value="" />
<td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p>

n this my servlet

package controller;

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;

public class upload extends HttpServlet {

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://";
ResultSet rs = null;
PreparedStatement psmnt = null;

// declare FileInputStream object to store binary stream of given image.

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);
//String ab = image.getCanonicalPath();
FileInputStream fis = new FileInputStream(image);
psmnt = connection.prepareStatement("insert into pic values(?,?)");
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);

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Welcome to the Ranch.

A few things to help you make the most of the Ranch as resource:

First, please use real words when posting to the forums. Abbreviations such as "d" in place of "the" only serve to make your posts more difficult to read and less likely to generate useful responses. Please click this link ⇒ UseRealWords for more information.

I have fixed the subject for you so it is understandable.

Secondly, please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

[Asking smart questions] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

shrutishrma sharma wrote:getParameter is not returning me the complete path of the image instead it returns only the image name

That's the way it's supposed to work.

Some browsers are broken and return the entire path.
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 199
1.) Your form does not define correct enctype (enctype="multipart/form-data") so the file is never even sent to server.
2.) You seem to think that server stores the file locally (i.e. writes it to disk). How do you know that? What server are you using?
I agree. Here's the link:
subject: I cannot obtain the enitre path of image
jQuery in Action, 3rd edition