Oi Rodolfo.

Talvez o c�digo abaixo lhe ajude! � um Servlet que faz upload de
arquivos. Voc� pode conseguir mais alguns c�digos fontes de servlets em
http://www.servletsource.com.br

Espero que ajude.

[]'s
Handerson F. Gomes

//-------
//-
//- upload.java
//-
//- Author:   AR Williamson
//- Date:   May 1998
//- Copyright:  Maybe used for non-commercial use.
//-
//-------

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

public class upload extends HttpServlet {

  public void doPost( HttpServletRequest _req, HttpServletResponse _res
) throws ServletException, IOException{

    parseMultiForm pMF = new parseMultiForm( _req );

    String param;
    while ( (param = pMF.getNextParameter()) != null ){
      if ( param.equalsIgnoreCase("USERFILE") ){
        FileOutputStream OutFile = new FileOutputStream(
pMF.getFilename() );
        pMF.getParameter( OutFile );
        OutFile.close();
      }else{
        System.out.println( "Key  : " + param );
        System.out.println( "Value: " + pMF.getParameter() );
      }
    }
    _res.setStatus( HttpServletResponse.SC_NO_CONTENT );
  }
}

class parseMultiForm extends java.lang.Object {
  private ServletInputStream In;
  private byte buffer[] = new byte[4096];
  private String delimitor = null;
  private String filename=null;

  public parseMultiForm( ServletRequest _req ) throws IOException{
    In  = _req.getInputStream();
    delimitor   = _req.getContentType();
    if ( delimitor.indexOf("boundary=") != -1 ){
      delimitor = delimitor.substring( delimitor.indexOf("boundary=")+9,
delimitor.length() );
      delimitor = "--" + delimitor;
    }
  }

  private String readLine(){
    try{
      int noData = In.readLine( buffer, 0, buffer.length );
      if ( noData != -1 )
        return new String( buffer, 0, noData, "ISO-8859-1");
    }catch(Exception E){}
    return null;
  }

  void test() throws IOException{
    String LineIn;
    while( (LineIn=readLine()) != null )
      System.out.println( LineIn  );
  }

  public String getFilename(){
    return filename;
  }

  public String getParameter(){
    return readLine();
  }

  public String getNextParameter() {
    try{
      String LineIn=null, paramName=null;

      while ( (LineIn=readLine()) != null ){
        if ( LineIn.indexOf( "name=" ) != -1 ){
          int c1 = LineIn.indexOf( "name=" );
          int c2 = LineIn.indexOf( "\"", c1+6 );
          paramName = LineIn.substring( c1+6, c2 );

          if ( LineIn.indexOf( "filename=") != -1 ){
            c1 = LineIn.indexOf( "filename=" );
            c2 = LineIn.indexOf( "\"", c1+10 );
            filename = LineIn.substring( c1+10, c2 );
            if ( filename.lastIndexOf( "\\" ) != -1 )
              filename = filename.substring( filename.lastIndexOf( "\\"
)+1 );

            if ( filename.length() == 0 )
              filename = null;
          }

          //- Move the pointer to the start of the data
          LineIn = readLine();
          if ( LineIn.indexOf( "Content-Type" ) != -1 )
            LineIn = readLine();

          return paramName;
        }
      }
    }
    catch( Exception E ){}
    return null;
  }

  public boolean getParameter( OutputStream _Out ){
    try{
        int noData;
        while ( (noData=In.readLine(buffer,0,buffer.length)) != -1 ){
          if ( buffer[0] == '-' ){
            if ( new String( buffer, 0, noData,
"ISO-8859-1").indexOf(delimitor) == 0 )
              break;
          }else
            _Out.write( buffer, 0, noData );
        }

        _Out.flush();
      return true;
    }
    catch( Exception E ){
      System.out.println( E  );
    }

    return false;
  }
}

Jo�o Carlos Bortoletto Jr wrote:

> Ola pessoal.     Aproveitando a d�vida do Rodolfo, algu�m sabe se �
> poss�vel fazer uploade de Arquivos do browser para o servidor
> utilizando um applet Java?    Qualquer dica ser� bem vinda... Jo�o
> Carlos Bortoletto Jr

    --------------------------- LISTA SOUJAVA ---------------------------
    http://www.soujava.org.br  -  Sociedade de Usu�rios Java da Sucesu-SP
    [para sair da lista: http://www.soujava.org.br/forum/cadastrados.htm]
    ---------------------------------------------------------------------

Responder a