How to get and store uploaded file on server in JSP

This is a discussion on How to get and store uploaded file on server in JSP within the Java forums in Programming Languages category; 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 !...

Go Back   Application Development Forum > Programming Languages > Java

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 06:37 AM
snehapshinde@gmail.com
Guest
 
Default How to get and store uploaded file on server in JSP

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 !
Reply With Quote
  #2  
Old 08-27-2008, 08:10 AM
Roland de Ruiter
Guest
 
Default Re: How to get and store uploaded file on server in JSP

On 27-8-2008 12:37, snehapshinde@gmail.com wrote:
> 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>
--
Regards,

Roland
Reply With Quote
  #3  
Old 08-27-2008, 08:14 AM
Roland de Ruiter
Guest
 
Default Re: How to get and store uploaded file on server in JSP

On 27-8-2008 14:10, Roland de Ruiter wrote:
> On 27-8-2008 12:37, snehapshinde@gmail.com wrote:
>> 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>

The Commons Fileupload library also needs Commons IO library (at least
at runtime; I don't think it's needed at compile time).
<http://commons.apache.org/io/>
--
Regards,

Roland
Reply With Quote
  #4  
Old 08-27-2008, 08:55 PM
Arne Vajhøj
Guest
 
Default Re: How to get and store uploaded file on server in JSP

snehapshinde@gmail.com wrote:
> 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
Reply With Quote
  #5  
Old 08-28-2008, 04:58 AM
snehapshinde@gmail.com
Guest
 
Default Re: How to get and store uploaded file on server in JSP

On Aug 28, 5:55*am, Arne Vajhøj <a...@vajhoej.dk> wrote:
> snehapshi...@gmail.com wrote:
> > 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


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?
Reply With Quote
  #6  
Old 08-28-2008, 05:54 PM
Arne Vajhøj
Guest
 
Default Re: How to get and store uploaded file on server in JSP

snehapshinde@gmail.com wrote:
> On Aug 28, 5:55 am, Arne Vajhøj <a...@vajhoej.dk> wrote:
>> 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));
>> }}
>>
>> %>


> 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
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:02 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.