Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I cannot obtain the enitre path of image

 
shrutishrma sharma
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
"http://www.w3.org/TR/html4/loose.dtd">

<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 type="file" name="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>

------------------
n this my servlet upload.java
-----------------------------------

package controller;

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;

public class upload extends HttpServlet {


@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);
//String ab = image.getCanonicalPath();
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);
}
}
}

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic