jsp upload test

It is possible to create a jsp that can upload a file to a server, knowing the path of that file ? The file isn't selected by the user (is a default file).

For example:

Supposing:

I have test.jpg located on root of disk C ( C:\test.jpg )

I have a form that only contains a <input type="text" name="A"> and a submit button.

The file that contains the form isin.jsp .

The file that retrieves the informations isout.jsp

I want out.jsp to retrieve the value of the textbox and the file to be uploaded to the server.

I hope I was clear enough. Thanks in advance!!

[638 byte] By [brandooa] at [2007-11-26 18:55:52]
# 1
Have you started doing something already? Show what have you done already...It is possible to make uploads using JSP, so I see no problem to get what you need.MeTitus
Me_Titusa at 2007-7-9 20:34:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I know is it possible doing uploads using jsp, but i want to know if is possible to do it without a <input type="file".....> (i'm not interested in the users capability to select a file).

in.jsp looks like this:

<html>

<head>

</head>

<body>

<form action="out.jsp">

Enter ur name:

<input type="text" name="A"/>

<input type="submit" value="SUBMIT"/>

</form>

</body>

</html>

out.jsp isn't written yet but i use for upload Uploadbean from javazoom.

brandooa at 2007-7-9 20:34:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
You could do it javascrip, dont' forget tha jsp scriptlets run on the server, but then using javascript you could run into permission issues. The best way to do it, would be to have an hidden form, and submit it... that is simple and you wont have any problems...MeTitus
Me_Titusa at 2007-7-9 20:34:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Javascript is not an option.

This project (with in.jsp and out.jsp) is a simple version of this problem: http://forum.java.sun.com/thread.jspa?threadID=5131163&messageID=9471742

I have to figure a way to upload a file without using the multipart/form-data enctype in that form. I need those EL prameters. Writing all in JSP (without using JSTL) is not an option.

brandooa at 2007-7-9 20:34:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> multipart/form-data enctype in that form. I need

Then I have to say to you that what you want is not possible... If a client application will be a web browser then you can't do it. If you were using a J2SE aplication then you could use the URL object and get its output stream, but as far as I know there aren't to date any client language that runs upon a browser, which lets you read a file and write its bytes.

MeTitus

Me_Titusa at 2007-7-9 20:34:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Like i have previously said I think javascrio can do it, at least I once heard something about it, but then again you say javascript is not an option, and even if you could do it with javascript, permission issues would stop you from going any further.MeTitus
Me_Titusa at 2007-7-9 20:34:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Yes it is possible to create a JSP that can upload a file with a known file name and known file path to the server.

There's already a package that allows this:

http://jakarta.apache.org/commons/fileupload/

On this thread I've written out the code to do almost something similar to that:

http://forum.java.sun.com/thread.jspa?forumID=45&threadID=5135302

It is possible to write out all of the above code in just a JSP file, but I would recommend that you make use of Servlets and back end java classes otherwise the code in the JSP will be unmanageable.

I don't know if that's what you're looking for.

I doubt that it will be possible to upload a file, without the user having to initiate the post, otherwise it would be a big security threat, anyone could try to read a file from my machine without my permission if I simply load a webpage on some site.

Message was edited by:

appy77

appy77a at 2007-7-9 20:34:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
I've thought about the security issue, that's why I asked here about the possibility of creating that jsp. My interest is finding a upload code that works with JSTL.
brandooa at 2007-7-9 20:34:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
It works with JSTL also.
appy77a at 2007-7-9 20:34:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
But when I turn the form into a multipart form, the JSTL request object can be used to retrieve any parameters? I've tryed with multipart form (using UploadBean) but all retrieved parameters come with NULL value. Only the file was uploaded sucessfuly.
brandooa at 2007-7-9 20:34:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

The problem you are facing with multipart form data has nothing to do with JSTL.

You would be read the form fields that is the File Upload field and other form fields such as text fields, text area, radio buttons etc inside a Java Class first - instead of directly in a JSP using JSTL.

Read this User Guide here:

http://jakarta.apache.org/commons/fileupload/using.html

if you scroll down in the above user guide to this section under

Processing the uploaded items:

you will notice an IF statement as follows:

if (item.isFormField()) {

processFormField(item);

} else {

processUploadedFile(item);

}

The above IF statement is checking whether the form field is a "file" or if it is a regular HTML field like text, check box etc.

You would implement such an IF statement , while you are iterating over the list of form fields in the form (that includes both text and file upload fields).

appy77a at 2007-7-9 20:34:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...