Hi Simon,
             You can post files to a servlet. The servlet can read from the
input stream and save to a desired location.
The other way is to use a MultiPart file upload program provided by O'reilly.
You can download this package from
servlets.com , this logic is also explained in the Jason Hunter book Java
Servlet Programming , O'reilly Publication.

However if you want code I'm using , here it is. Although this code has file
size and type limitation.

Here is the code , you may have to alter it to suit your scenario and
requirements


Santosh


Client Code - where client is your interface that will post files , could be JSP
, Servlet or even a Java app

// Get connection to Upload Servlet

      String url ="http://your_server/servlet/UploadServlet?filename="+filename;


      URL theURL = new URL(url);

      HttpURLConnection theConnection =
(HttpURLConnection)theURL.openConnection();
      theConnection.setRequestMethod("POST");
      theConnection.setDoOutput(true);
      theConnection.setUseCaches(false);

      PrintWriter out = new PrintWriter(theConnection.getOutputStream());

      // Read File

      String line = null;
      FileReader f1 = new FileReader(filename);
      BufferedReader theReader = new BufferedReader(f1);

      // Send each line to Servlet
      while ((line = theReader.readLine()) != null)
      {
        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));

     //StringBuffer buf = new StringBuffer();

      System.out.println("before while");
      while ((inputLine = responseReader.readLine()) != null)
      {
        System.out.println(inputLine);
      }
     }
    catch (UnknownServiceException e)
    {
      System.out.println("Unknown Service Exception Caught: \n" + e);
    }
  }

UploadServlet Code


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


/*------------------------------------------------------------------------------
* Upload Servlet
*-----------------------------------------------------------------------------*/
public class UploadServlet extends HttpServlet
{

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

    private static String m_uploadDirectory = "E:\\uploadlog";


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

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

    response.setContentType("application/octet-stream");

    // 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));

      filename = request.getParameter("filename");


      String uploadFileName = m_uploadDirectory + '\\' + filename;


      System.out.println("Creating File "+filename);
      uploadFile = new BufferedWriter(new FileWriter(uploadFileName));
      System.out.println("File Creation Completed"+filename);

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

      response.setContentType("application/octet-stream");

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


      PrintWriter out = response.getWriter();
      out.println("any message u want to send back to the client");
      out.close();
    }
    catch (Exception e)
    {
       System.out.println(e);
      response.setContentType("application/octet-stream");
      PrintWriter out = response.getWriter();
      out.println("ERROR\n:" + e);
      out.close();
    }
    finally
    {
      if (uploadFile != null)
      {
        uploadFile.close();
      }
    }
  }

}










Clair e Simon <[EMAIL PROTECTED]> on 05/15/2001 09:17:39 AM

Please respond to A mailing list about Java Server Pages specification and
      reference <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:    (bcc: Santosh Daryani/IT/Aon Consulting)

Subject:  JSP Upload



Hi Everyone,

I'm doing a web site in JSP and I need to upload files in a folder on my server.

Someone know how to do this or where on the Net I could have more information?


Thank you

Simon

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
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

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
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