aspose file tools*
The moose likes JSP and the fly likes Upload files from client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Upload files from client" Watch "Upload files from client" New topic
Author

Upload files from client

Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
Can some one please tell me how to upload files from a client machine to a particular drive on server using jsp - servlets
Mandar Max
Ranch Hand

Joined: Mar 14, 2006
Posts: 38
Take a look at - http://jakarta.apache.org/commons/fileupload/


"The trouble with doing something right the first time is that nobody appreciates how difficult it was!"
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
hi mandar
i downloader the jar .... can youu give me some code to look at coz i am not able to work it out properly
Mandar Max
Ranch Hand

Joined: Mar 14, 2006
Posts: 38
I presume that you already know how to write/deploy/test servlet applications.

Step1 - Create a servlet application. Implement the doPost() method of servlet and add code as per the usage guide on apache file upload.

Step2 - Create an HTML form like this:



Provide URL of the servlet in place of action tag.

Step3 - Run the server and test the file upload form

You can always find a lot of examples using Google.

Cheers
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
hi mandar
i have actually done that and implemented in my application but for some reason i am getting a n error
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:179)
org.apache.commons.fileupload.FileUploadBase.createItem(FileUploadBase.java:500)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:367)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)
org.apache.jsp.jsp.ManualAttendance.upload_jsp._jspService(upload_jsp.java:69)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


i dont know whats wrong...my code is like this

<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Process File Upload</title>
</head>
<%
System.out.println("Content Type ="+request.getContentType());

DiskFileUpload fu = new DiskFileUpload();
// If file size exceeds, a FileUploadException will be thrown
fu.setSizeMax(1000000);

List fileItems = fu.parseRequest(request);
Iterator itr = fileItems.iterator();

while(itr.hasNext()) {
FileItem fi = (FileItem)itr.next();

//Check if not form field so as to only handle the file inputs
//else condition handles the submit button input
if(!fi.isFormField()) {
System.out.println("\nNAME: "+fi.getName());
System.out.println("SIZE: "+fi.getSize());
//System.out.println(fi.getOutputStream().toString());
File fNew= new File(application.getRealPath("/"), fi.getName());

System.out.println(fNew.getAbsolutePath());
fi.write(fNew);
}
else {
System.out.println("Field ="+fi.getFieldName());
}
}
%>
<body>
Upload Successful!!
</body>
</html>

and html

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252"/>
<TITLE>File Upload Page</TITLE>
</HEAD>
<BODY>Upload Files
<FORM name="filesForm" action="upload.java"
method="post" enctype="multipart/form-data">
File 1:<input type="file" name="file1"/><br/>

<input type="submit" name="Submit" value="Upload Files"/>
</FORM>
</BODY>
</HTML>
Mandar Max
Ranch Hand

Joined: Mar 14, 2006
Posts: 38
The reason you are getting this error is a dependent class org.apache.commons.io.output.DeferredFileOutputStream is not on the classpath. You need to check the relevent documentation on dependencies. The DeferredFileOutputStream class is part of Jakarta commons IO library.
Bernard Sigmund Gustav
Ranch Hand

Joined: Dec 20, 2005
Posts: 170
I have tried using the file upload.
try javazoom
its easy to deploy and understand.

I do have a question though.
How do i delete a file upon uploading?
Or maybe just move the file instead of copying it when uploading?
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

i have actually done that and implemented in my application but for some reason i am getting a n error
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream


You need to include commons-io-1.1.jar along with commons-fileupload-1.1.jar


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Amod Mulay
Ranch Hand

Joined: Apr 06, 2006
Posts: 33
oh yeah that worked thankx!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Since you seem to be working in JSP and not servlets, I'll move this thread to the JSP forum.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Upload files from client