Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get Image from request

 
Syam Veerakumar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to get an image from a form's file type tag(which browses and get the image from client machine) and submitting it to a servlet, in which type i have to get it from request in servlet.i.e
if its a text we'll get it like
String string = request.getParameter("name");
But I have to get an image so can any one please help me out in this.
Thanks,
Syam.
 
Yogen Vadnere
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shyamrao,
I think visit to the following link , this may help u
http://www.javaranch.com/ubb/Forum7/HTML/006063.html
 
Sujitha Raj
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it like some web pages asking for photographs to save it in database,if so i am also need help what syam had asked.
regards,
Suji.
 
Yogen Vadnere
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code to save the gif in database, but first u have to upload the file from clients machine by using multipart request.
{
...
File file = new File ("Logo.gif");
InputStream inStream = new FileInputStream ("Logo.gif");
PreparedStatement psmt = conn.prepareStatement
("INSERT INTO myTable (pk_field,img_field) VALUES
(?, ?)");
//mytable with two columns Varchar and Long raw
psmt.setString (1, "AveryE Logo");
psmt.setBinaryStream (3, inStream, (int)file.length ());
psmt.execute ();
...
}
Hope this will help u, if something is wrong pl correct me.
 
Syam Veerakumar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yogen,
I'll try it out and let u know,thank u very much.
Syam.
 
Syam Veerakumar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yogen,
I gave it like this,but still its not worikng.
File file = new File("file://"+request.getParameter("image"));
InputStream inStream = new FileInputStream(request.getParameter("image"));
// where image is the name of input type.
PreparedStatement pstmt = null;
pstmt = conn.prepareStatement("INSERT INTO TEST VALUES(?,?)");
pstmt.setInt(1,no);
pstmt.setBinaryStream(2,inStream,(int)file.length());
pstmt.execute();
 
Yogen Vadnere
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O man,
Not like this, i said first upload the file (image) in some temporary folder by using multipart request (for more information search for "uploading file" or "uploading" in this section) and then use the new path to create file object and to save into the specified table.
Hope this will help u.

 
Syam Veerakumar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yogen,
my form is like this,
<form name= "imageForm" enctype="multipart/form-data" method = post action = "http://10.0.0.24:800/trng/TestImage" >
<input type = "file" name = "image"><p>
<input type = "submit" >
</form>
so can i get the image from the client machine like this using servlet,
InputStream inStream = new FileInputStream(request.getParameter("image"));
Its working fine in local system but if i try to get image from a remote system its not working so can u please help me out in this.
Thanks,
Syam.
 
v velus
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Its difficult to pass as fine stream.Set that content type to jpg/gif.
Read it in bytes, try to pass it in http stream and save it in server.
Try this code:
FileInputStream fis=new FileInputStream(yourfilename.ext);
index=s.lastIndexOf("/");
String s1="";
for(int i=index+1;i<s.length();i++)
{
s1=s1+(s.charAt(i));
}
FileOutputStream fos=new FileOutputStream(s1);
while((c=fis.read())!=-1)
{
fos.write(c);
}
 
Syam Veerakumar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Velu,
I tried that its working fine in server but i have to get the image file from client can u give me some suggestion how to get that image from client machine and save it in server.
Thanks,
Syam.
 
GUBAL
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Syam
U FIRST upload the image(uploading refer book),check for .gif/jpg(using javascript) and then store the image in some folder(in server) and then store the path in database.

kumar
 
Syam Veerakumar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumar,
I am checking the file extn like this in javascript,but the problem is while submiting the servlet is not getting that image,so can u please let me know how to get it.
<script language ="javascript">
function verify_photo() {
if( (document.imageForm.username.value == "") | | (document.imageForm.username.value == null) ) {
alert("Please Enter value for UserName");
return false;
}
var t = document.imageForm.image.value;
var z=t.substring(t.length,t.length -4);
if (z == ".jpg" | | z == ".gif" | | z == "jpeg" | | z == ".JPG" | | z == ".GIF" | | z == "JPEG")
{ return true; }
else
{ alert("Only gif's , jpg's , jpeg's are allowed\n");
return false; }
}
Thanks,
Syam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic