How to get and store uploaded file on server in JSP

S

snehapshinde

I have created a html page for uploading of file..
<body>
<form method="post" enctype="multipart/form-data" action="http://
localhost:8080/LabMgmt1/upload.jsp">
<input type="file" size=20 name="fname">
<input type="Submit" value="Upload"> </form>
</body>
Now I want to get the uploaded file in upload.jsp and save it in a
folder.
How can I do it?
Plz help !
 
R

Roland de Ruiter

I have created a html page for uploading of file..
<body>
<form method="post" enctype="multipart/form-data" action="http://
localhost:8080/LabMgmt1/upload.jsp">
<input type="file" size=20 name="fname">
<input type="Submit" value="Upload"> </form>
</body>
Now I want to get the uploaded file in upload.jsp and save it in a
folder.
How can I do it?
Plz help !
Probably the easiest is to use the Jakarta Commons Fileupload library
(rather than writing it yourself).

<http://commons.apache.org/fileupload/>
<http://commons.apache.org/fileupload/using.html>
 
A

Arne Vajhøj

I have created a html page for uploading of file..
<body>
<form method="post" enctype="multipart/form-data" action="http://
localhost:8080/LabMgmt1/upload.jsp">
<input type="file" size=20 name="fname">
<input type="Submit" value="Upload"> </form>
</body>
Now I want to get the uploaded file in upload.jsp and save it in a
folder.

Jakarta FileUpload or COS.

I will recommend Jakarta FileUpload unless you have
special requirements.

Code snippet that processes multiple files:

<%@page import="org.apache.commons.fileupload.*,java.util.*,java.io.*"%>
<%
DiskFileUpload upload = new DiskFileUpload();
List files = upload.parseRequest(request);
for(int i = 0; i < files.size(); i++) {
FileItem file = (FileItem)files.get(i);
if(file.getSize() > 0) {
String filename = "C:\\" + file.getFieldName() + ".upl";
file.write(new File(filename));
}
}
%>

Arne
 
S

snehapshinde

Jakarta FileUpload or COS.

I will recommend Jakarta FileUpload unless you have
special requirements.

Code snippet that processes multiple files:

<%@page import="org.apache.commons.fileupload.*,java.util.*,java.io.*"%>
<%
DiskFileUpload upload = new DiskFileUpload();
List files = upload.parseRequest(request);
for(int i = 0; i < files.size(); i++) {
    FileItem file = (FileItem)files.get(i);
    if(file.getSize() > 0) {
       String filename = "C:\\" + file.getFieldName() + ".upl";
       file.write(new File(filename));
    }}

%>

Arne

I tried with this piece of code, but it is throwing following
exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 2 in the jsp file: /upload.jsp
Generated servlet error:
DiskFileUpload cannot be resolved or is not a type

An error occurred at line: 2 in the jsp file: /upload.jsp
Generated servlet error:
DiskFileUpload cannot be resolved or is not a type

An error occurred at line: 2 in the jsp file: /upload.jsp
Generated servlet error:
FileItem cannot be resolved or is not a type

An error occurred at line: 2 in the jsp file: /upload.jsp
Generated servlet error:
FileItem cannot be resolved or is not a type

I don't understand why is it not able to recognize DiskFileUpload and
FileItem, even though org.apache.commons.fileupload.* has been
imported! Could you please tell me what would be the probable
problem?
 
A

Arne Vajhøj

I tried with this piece of code, but it is throwing following
exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 2 in the jsp file: /upload.jsp
Generated servlet error:
DiskFileUpload cannot be resolved or is not a type
I don't understand why is it not able to recognize DiskFileUpload and
FileItem, even though org.apache.commons.fileupload.* has been
imported! Could you please tell me what would be the probable
problem?

import="org.apache.commons.fileupload.*

just imports names - it means that DiskFileUpload can be used as
abbreviation for org.apache.commons.fileupload.DiskFileUpload - it does
not import any code.

The error is with almost certainty that you have not put the
Jakarta FileUpload jar files in classpath. They should be in
WEB-INF/lib !

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top