upload file from a client machine to server machine

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • praveenkumarvpk
    New Member
    • Oct 2007
    • 22

    upload file from a client machine to server machine

    Hi friends Please help me!

    Following is my servlet-code

    import java.util.Enume ration;
    import javax.servlet.* ;
    import javax.servlet.h ttp.*;
    import java.io.*;

    import com.oreilly.ser vlet.MultipartR equest;
    import com.oreilly.ser vlet.MultipartR esponse;
    import com.oreilly.ser vlet.multipart. DefaultFileRena mePolicy;

    public class DemoUpload extends HttpServlet {
    private String dirName;

    public void init(ServletCon fig config) throws ServletExceptio n {
    super.init(conf ig);
    // read the uploadDir from the servlet parameters
    dirName = config.getInitP arameter("uploa dDir");
    dirName="c:/a";//SPORTS.JPEG";

    //System.out.prin tln("before :"+dirName);

    if (dirName == null) {
    throw new ServletExceptio n("Please supply uploadDir parameter");
    }
    }

    public void doGet(HttpServl etRequest request, HttpServletResp onse response)
    throws ServletExceptio n, IOException {
    PrintWriter out = response.getWri ter();
    response.setCon tentType("text/plain");
    out.println("De mo Upload Servlet using MultipartReques t");
    out.println();

    try {
    // Use an advanced form of the constructor that specifies a character
    // encoding of the request (not of the file contents) and a file
    // rename policy.
    System.out.prin tln("before :"+dirName);
    MultipartReques t multi =new MultipartReques t(request, dirName, (10*1024*1024), "ISO-8859-1", new DefaultFileRena mePolicy());

    System.out.prin tln("After :"+dirName);
    out.println("PA RAMS:");
    Enumeration params = multi.getParame terNames();
    while (params.hasMore Elements()) {
    String name = (String)params. nextElement();
    String value = multi.getParame ter(name);
    out.println(nam e + "=" + value);
    }
    out.println();

    out.println("FI LES:");
    Enumeration files = multi.getFileNa mes();
    while (files.hasMoreE lements()) {
    String name = (String)files.n extElement();
    String filename = multi.getFilesy stemName(name);
    String originalFilenam e = multi.getOrigin alFileName(name );
    String type = multi.getConten tType(name);
    File f = multi.getFile(n ame);
    out.println("na me: " + name);
    out.println("fi lename: " + filename);
    out.println("or iginalFilename: " + originalFilenam e);
    out.println("ty pe: " + type);
    if (f != null) {
    out.println("f. toString(): " + f.toString());
    out.println("f. getName(): " + f.getName());
    out.println("f. exists(): " + f.exists());
    out.println("f. length(): " + f.length());
    }
    out.println();
    }
    }
    catch (IOException lEx) {
    this.getServlet Context().log(l Ex, "error reading or saving file");

    System.out.prin tln("HERE THE EXCEPTION IS RAISING : "+lEx);
    }
    }
    }


    When i execute this i'm getting an exception that


    before :c:/a
    Feb 2, 2008 12:17:50 PM org.apache.cata lina.core.Appli cationContext log
    SEVERE: error reading or saving file
    java.io.IOExcep tion: Posted content type isn't multipart/form-data
    at com.oreilly.ser vlet.multipart. MultipartParser .<init>(Multipa rtParser.java:1 66)
    at com.oreilly.ser vlet.MultipartR equest.<init>(M ultipartRequest .java:222)
    at DemoUpload.doGe t(DemoUpload.ja va:38)


    Please help to upload a file
  • praveenkumarvpk
    New Member
    • Oct 2007
    • 22

    #2
    Hey guys Please help to find it.... urgent

    Thank U

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Read your exception trace. It's telling you exactly what the problem is.

      Comment

      Working...