Here is how we did it , this can be implemented right away , you may have to
modify a little because I was passing data from a Java client app to a servlet ,
you may have to code it ot post from a JSP instead of a Java client app.
  Alos my code copies the posted data into a file , you can eliminate that step
, because you just want the data to get there

1 . Java client app posts data to a  2. Upload Servlet

hope this helps
Santosh


1. Code for part one

import java.net.*;
import java.io.*;

public class HTTPPostClient
{

  /*----------------------------------------------------------------------------
  * usage
  *---------------------------------------------------------------------------*/
  private static void usage()
  {
    System.out.println("Usage: HTTPostClient file");
  }

  /*----------------------------------------------------------------------------
  * main
  *---------------------------------------------------------------------------*/
  public static void main(String args[])
  {
    try
    {

      if (args.length != 1)
      {
        usage();
        return;
      }

      // Get connection to Upload Servlet
      URL theURL = new URL("http://localhost:8000/servlet/UploadServlet");
      HttpURLConnection theConnection =
        (HttpURLConnection)theURL.openConnection();
      theConnection.setRequestProperty("Content-Type", "text/xml");
      theConnection.setRequestMethod("POST");
      theConnection.setDoOutput(true);
      PrintWriter out = new PrintWriter(theConnection.getOutputStream());


      // Read XML File
      String fileContents = "";
      String line = null;
      FileReader f1 = new FileReader(args[0]);
      BufferedReader theReader = new BufferedReader(f1);

    //   In your case you don't have to read data from a file to send it , you
may collect your data from
   //  a form , from the database ,or any other source of input



      // Send each line to Servlet
      while ((line = theReader.readLine()) != null)
      {
        System.out.println(line);
        out.println(line);
      }
      out.close();
      System.out.println("Data Written");

      // Get Response
      System.out.println("Response: ");
      String inputLine;
      InputStream in = theConnection.getInputStream();
      BufferedReader responseReader =
        new BufferedReader(new InputStreamReader(in));
      while ((inputLine = responseReader.readLine()) != null)
      {
        System.out.println(inputLine);
      }
      in.close();

    }
    catch (Exception e)
    {
      System.out.println("Exception Caught:\n" + e);
    }
  }
}


2. Code for upload servlet


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/*------------------------------------------------------------------------------
* Upload Servlet
*-----------------------------------------------------------------------------*/
public class UploadServlet extends HttpServlet
{(in));

  /*----------------------------------------------------------------------------
  * Data
  * Modify directory to where you want to store uploaded files!
  *---------------------------------------------------------------------------*/

  private static String m_uploadDirectory = "\\upload";


  /*----------------------------------------------------------------------------
  * doGet
  *---------------------------------------------------------------------------*/
  public void doGet (HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {

    System.out.println("UploadServlet.doGet()");

    // Set content type and other response header fields first
    String title = "Simple Servlet Output";
    response.setContentType("text/html");

    // Write the data of the response
    PrintWriter out = response.getWriter();

    out.println("<HTML><HEAD><TITLE>");
    out.println(title);
    out.println("</TITLE></HEAD><BODY>");
    out.println("<H1>" + title + "</H1>");
    out.println("<P>This is output from UploadServlet");
    out.println("</BODY></HTML>");
    out.close();
  }

  /*----------------------------------------------------------------------------
  * doPost
  *---------------------------------------------------------------------------*/
  public void doPost (HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    BufferedWriter uploadFile = null;
    try
    {

      System.out.println("UploadServlet.doPost()");
      System.out.println("CONTENT: " + request.getContentType());
      System.out.println("LENGTH : " + request.getContentLength());

      // Get the Input Stream
      ServletInputStream input = request.getInputStream();
      BufferedReader theReader =
      new BufferedReader(new InputStreamReader(input));

      // Create output file
      String uploadFileName = m_uploadDirectory + '\\' + filename;

      uploadFile = new BufferedWriter(new FileWriter(uploadFileName));

      String line = null;
      while ((line = theReader.readLine()) != null)
      {
        uploadFile.write(line);
        uploadFile.newLine();
      }

      // Write the data of the response
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("OK");
      out.close();
    }
    catch (Exception e)
    {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("ERROR\n:" + e);
      out.close();
    }
    finally
    {
      if (uploadFile != null)
      {
        uploadFile.close();
      }
    }
  }

}

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to