Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Upload files from client

 
Amod Mulay
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at - http://jakarta.apache.org/commons/fileupload/
 
Amod Mulay
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Amod Mulay
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yeah that worked thankx!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you seem to be working in JSP and not servlets, I'll move this thread to the JSP forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic