0

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.....

3
Contributors
11
Replies
13
Views
6 Years
Discussion Span
Last Post by javaAddict
0

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.

Edited by javaAddict: n/a

0

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();
%>

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

0

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.

0

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.

0

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?

0

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.

0

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.

0

I need to use this in scriplet.

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

Edited by masijade: n/a

0

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();
%>

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

0

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.