• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

How to upload a file using servlets to a webserver

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Are you here to take over the surface world? Because this tiny ad will stop you!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic