wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to upload a file using servlets to a webserver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How to upload a file using servlets to a webserver" Watch "How to upload a file using servlets to a webserver" New topic
Author

How to upload a file using servlets to a webserver

sai sridhar
Greenhorn

Joined: Aug 23, 2001
Posts: 7
Hi everyone,
I wanna upload a file to Javawebserver.I've downloaded
"com.oreille.servlet.*" package from www.servlets.com and has set the classpath.Everything is compiling fine and i wrote the following code in Html page
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form ACTION="http://localhost:8080/servlet/Test.class" METHOD=post ENCTYPE="multipart/form-data" >
what is your name ? <input type = text name=submitter> <br>
which file do you want to upload ? <input type=file name=file><br>
<input type=submit value=submit>
</form>
</body>
</html>
But,when i click on the submit button i get an error as "Http404" not found.
I wrote the following code as my servlet

public class Test extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
res.setContentType("text/html");PrintWriter out = res.getWriter();try{
MultipartRequest multi = new MultipartRequest
(req,"./public_html/temp",5*1024*1024);
out.println("<html>");
out.println("<head><title>UploadTest</head></title>");
out.println("<body>");
out.println("<h1>upLoadTest</h1>");
out.println("<pre>");
Enumeration params = multi.getParameterNames();
while (params.hasMoreElements()){
String name = (String)params.nextElement();
String value = multi.getParameter(name);
out.println( name + " = " + value);
}
out.println("</pre>");
out.println("<h3> files : </h3>");
out.println("<pre>");
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()){
String name = (String)files.nextElement();
String filename = multi.getFilesystemName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
out.println("name : " + name);
out.println("file name : "+ filename);
out.println("type : "+type);
if (f!=null){
out.println("length : " +f.length());
out.println();
}
out.println("</pre>");
}
}
catch(Exception e){
out.println("<pre>");
System.out.println(e);
e.printStackTrace(out);
out.println("</pre>");
}
out.println("</body> </html>");
}
}
pl revert back as this is urgent
Thanks
sridhar
arvind
Greenhorn

Joined: Aug 27, 2001
Posts: 11
hi! sridhar,
u can try out this code ,it works:
Ist part:
<html>
<head>
<title>File Upload</title>
</head>
<body bgcolor=bisque>
<form ACTION="http://localhost:8080/servlet/upload" METHOD=post ENCTYPE="multipart/form-data" >
which file do you want to upload ? <input type=file name=file><br>
<input type=submit value=submit>
</form>
</body>
</html>
IInd part:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.*;
import java.util.*;
public class upload extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException {
res.setContentType("text/html"); PrintWriter out = res.getWriter(); try{
MultipartRequest multi = new MultipartRequest
(req,"../image/trim",5*1024*1024);
out.println("<html>");
out.println("<head><title>UploadTest</head></title>");
out.println("<body>");
out.println("<h1>upLoadTest</h1>");
out.println("<pre>");
Enumeration params = multi.getParameterNames();
while (params.hasMoreElements()){
String name = (String)params.nextElement();
String value = multi.getParameter(name);
out.println( name + " = " + value);
}
out.println("</pre>");
out.println("<h3> files : </h3>");
out.println("<pre>");
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()){
String name = (String)files.nextElement();
String filename = multi.getFilesystemName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
out.println("name : " + name);
out.println("file name : "+ filename);
out.println("type : "+type);
if (f!=null){
out.println("length : " +f.length());
out.println();
}
out.println("</pre>");
}
}
catch(Exception e){
out.println("<pre>");
System.out.println(e);
e.printStackTrace(out);
out.println("</pre>");
}
out.println("</body> </html>");
}
}

sai sridhar
Greenhorn

Joined: Aug 23, 2001
Posts: 7
Hi Aravind,
Thanks for u'r info.But sorry to tell you,i still get "404 PageNotFound " error..Seriously,i'm so bugged becoz i'm unable to figure out where i've mistaken
pl revert back if u find
Thanks a tonne
sridhar
Originally posted by arvind:
hi! sridhar,
u can try out this code ,it works:
Ist part:
<html>
<head>
<title>File Upload</title>
</head>
<body bgcolor=bisque>
<form ACTION="http://localhost:8080/servlet/upload" METHOD=post ENCTYPE="multipart/form-data" >
which file do you want to upload ? <input type=file name=file><br>
<input type=submit value=submit>
</form>
</body>
</html>
IInd part:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.*;
import java.util.*;
public class upload extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException {
res.setContentType("text/html"); PrintWriter out = res.getWriter(); try{
MultipartRequest multi = new MultipartRequest
(req,"../image/trim",5*1024*1024);
out.println("<html>");
out.println("<head><title>UploadTest</head></title>");
out.println("<body>");
out.println("<h1>upLoadTest</h1>");
out.println("<pre>");
Enumeration params = multi.getParameterNames();
while (params.hasMoreElements()){
String name = (String)params.nextElement();
String value = multi.getParameter(name);
out.println( name + " = " + value);
}
out.println("</pre>");
out.println("<h3> files : </h3>");
out.println("<pre>");
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()){
String name = (String)files.nextElement();
String filename = multi.getFilesystemName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
out.println("name : " + name);
out.println("file name : "+ filename);
out.println("type : "+type);
if (f!=null){
out.println("length : " +f.length());
out.println();
}
out.println("</pre>");
}
}
catch(Exception e){
out.println("<pre>");
System.out.println(e);
e.printStackTrace(out);
out.println("</pre>");
}
out.println("</body> </html>");
}
}

arvind
Greenhorn

Joined: Aug 27, 2001
Posts: 11
hi! Sridhar,
first of all try running the code using servletrunner. you can save upload.html and upload.java,upload.class files in jsdk\examples folder.then using the browser properties open the html page eg C:\JSDK2.0\examples\upload.html.create a folder anywhere on your c:\drive say image. in your upload.java file in the MultiPartRequest give path like req,../image,1024*1024...
After selection of any file using the browse button ,clicking on the submit button ,the file will be send to the image folder which you had created.
this code works out well.
all the best.
 
GeeCON Prague 2014
 
subject: How to upload a file using servlets to a webserver