aspose file tools*
The moose likes Servlets and the fly likes How to get Image from request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get Image from request" Watch "How to get Image from request" New topic
Author

How to get Image from request

Syam Veerakumar
Ranch Hand

Joined: Sep 20, 2001
Posts: 49
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.


A Quitter Never Wins<br />A Winner Never Quits
Yogen Vadnere
Ranch Hand

Joined: Sep 20, 2001
Posts: 58
Shyamrao,
I think visit to the following link , this may help u
http://www.javaranch.com/ubb/Forum7/HTML/006063.html


Yogen Vadnere
Sujitha Raj
Greenhorn

Joined: Sep 25, 2001
Posts: 1
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

Joined: Sep 20, 2001
Posts: 58
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

Joined: Sep 20, 2001
Posts: 49
Hi Yogen,
I'll try it out and let u know,thank u very much.
Syam.
Syam Veerakumar
Ranch Hand

Joined: Sep 20, 2001
Posts: 49
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

Joined: Sep 20, 2001
Posts: 58
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

Joined: Sep 20, 2001
Posts: 49
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

Joined: Sep 07, 2001
Posts: 14
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

Joined: Sep 20, 2001
Posts: 49
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

Joined: Sep 28, 2001
Posts: 2
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

KUMAR H.D
Syam Veerakumar
Ranch Hand

Joined: Sep 20, 2001
Posts: 49
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get Image from request
 
Similar Threads
Time bounded Image Loading
jsp to display image
How to restrict access to image folders
Moving the Cursor in ResultSets.....
I cannot obtain the enitre path of image