I am new to java, I need to save a image file from my system to my web application. How to do that in jsp or java? Need help urgently.....

Recommended Answers

All 11 Replies

If you are new in java then this task is not for you. If you had previous experience with jsp then that experience would have been acquired by studying, and you would know what you needed to do:

You need to search the net for examples on how to load files. You will use the input tag type="file" in your jsp/html code.

There are plenty of examples that you can find by searching.

After you are done loading the file, how do you want to save it? The easiest way would be to create a folder at your server and copy the files there.

Thanks for the reply. I used manage with the code below, I have included this code in jsp. Its working fine if i use the static path. I need to use the dynamic path of the image to be uploaded from the Input tag file. I dont know how to get that inside this java code. Help me regarding that.....

<%@ page import="java.io.*" %>

<%
String source = "/blue.jpg";
String destination = "/images/blue.jpg";

File sourceFile=new File(source);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile), 4096);
String fileName=getServletContext().getRealPath(destination);
File targetFile = new File(fileName);
targetFile.createNewFile();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile), 4096);
int theChar;
while ((theChar = bis.read()) != -1) {
        bos.write(theChar);
}
bos.close();
bis.close();
%>

Have you used what I suggested:

<input type="file" />

In order to load the image? I don't see it since you hard coded the file source name. Search the web for examples on how to load file with that attribute and what other arguments it takes.

Yes I have used tag <input type="file" name="imageFile" /> also to get dynamic file path. But I dont know how to use that file in java.

Yes I have used tag <input type="file" name="imageFile" /> also to get dynamic file path. But I dont know how to use that file in java.

That is what you need to search. There are examples and libraries specifically for that. What server are you using? I think that Apache already has classes and methods for getting that file, in the same way you get the request.

A very simple search at the net got me this result:
http://www.developershome.com/wap/wapUpload/wap_upload.asp?page=jsp

Check out that page (page 8) and the next.

Was it so difficult?

Hello Friend! I am getting File through <input type="file" name="imageFile" />, I need to use this in scriplet. I have been searching this for the last 1 hour, can u tell me how to do this? I am trying a lot, getting only for String input. I need it for File input.

Hello Friend! I am getting File through <input type="file" name="imageFile" />, I need to use this in scriplet. I have been searching this for the last 1 hour, can u tell me how to do this? I am trying a lot, getting only for String input. I need it for File input.

And what about the link I provided? If you can't understand it then post code and ask questions or do some search on your own.

But that link has an example to help you started, of you read it carefully of course.

I need to use this in scriplet.

No you don't, you shouldn't be using scriptlets in the first place.

This is what the part of code I am having in the jsp file. I need to use that imageFile in scriptlets.
masijade, Am I shouldn't use like this?

<table>
 <tr><td>Browse Image to Upload:</td><td><input type="file" name="imageFile">td></tr>
</table>
 
<%@ page import="java.io.*" %>

<%
String source = "/blue.jpg";
String destination = "/images/blue.jpg";

File sourceFile=new File(source);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile), 4096);
String fileName=getServletContext().getRealPath(destination);
File targetFile = new File(fileName);
targetFile.createNewFile();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile), 4096);
int theChar;
while ((theChar = bis.read()) != -1) {
bos.write(theChar);
}
bos.close();
bis.close();
%>

JavaAddict, I have gone through the link that you sent. I think that is not for scriptlets. I mean, that is the code, when we using java and jsp separately. Am I right?

You need to put the tag into a form and submit that form. Then use my example or another that you like and get the request.

If you don't know that input tags go inside forms that are submitted then you shouldn't be doing any of this.

You should be studying the basics of HTML and JSP. If the above is true then giving you code with that amount of knowledge will not help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.