Upload de arquivo

Preciso disponibilizar na pagina WEB um opção para anexar arquivo. Como faço o upload do arquivo para o servidor?

segue um exemplo em Jsp, só lembrando q na linha:

FileOutputStream fileOut = new FileOutputStream( getServletContext().getInitParameter("tempPath") + "\\" + saveFile );

o parametro “tempPath” é o caminho do diretório onde estou salvando o arquivo

<%@ page import="java.io.*" %>

<%@ include file="/jsp/includes/common.jsp" %>

<%
    try {

    String contentType = request.getContentType();
    System.out.println( "Content type is :: " + contentType );
    
    if ( ( contentType != null ) && ( contentType.indexOf( "multipart/form-data" ) >= 0 ) ) 
    {
        DataInputStream in = new DataInputStream( request.getInputStream() );
        int formDataLength = request.getContentLength();

        byte dataBytes[] = new byte[ formDataLength ];
        int byteRead = 0;
        int totalBytesRead = 0;

        while ( totalBytesRead < formDataLength ) 
        {
            byteRead = in.read( dataBytes, totalBytesRead, formDataLength );
            totalBytesRead += byteRead;
        }

        String file = new String( dataBytes );
        String saveFile = file.substring( file.indexOf( "filename=\"" ) + 10 );
        saveFile = saveFile.substring( 0, saveFile.indexOf( "\n" ) );
        saveFile = saveFile.substring( saveFile.lastIndexOf( "\\" ) + 1, saveFile.indexOf( "\"" ) );


        int lastIndex = contentType.lastIndexOf( "=" );
        String boundary = contentType.substring( lastIndex + 1, contentType.length() );

        int pos;

        pos = file.indexOf( "filename=\"" );

        pos = file.indexOf( "\n", pos ) + 1;

        pos = file.indexOf( "\n", pos ) + 1;

        pos = file.indexOf( "\n", pos ) + 1;

        int boundaryLocation = file.indexOf( boundary, pos ) - 4;
        int startPos = ( ( file.substring( 0, pos ) ).getBytes() ).length;
        int endPos = ( ( file.substring( 0, boundaryLocation ) ).getBytes() ).length;

        FileOutputStream fileOut = new FileOutputStream( getServletContext().getInitParameter("tempPath") + "\\" + saveFile );

        fileOut.write( dataBytes, startPos, ( endPos - startPos ) );

        fileOut.flush();
        fileOut.close();

        out.println("File saved as " + saveFile );

Da pra usar o componente fileupload do projeto jakarta.

Eh soh entrar em: http://jakarta.apache.org/commons/fileupload/

eh bem legalzinho esse componente. Qq duvida na utilizacao dele eh so postar aqui.

Obrigado darkseid. Consegui usar o pacote jar do projeto jakarta. Só que agora tenho outra duvida. Na hora de salvar o arquivo no HD (no servidor) eu tenho que passsar o caminho completo (C:\Tomcat…). Alguem sabe se tem como descobrir o caminho completo a partir de uma url em java? como a “http:\localhost\Exemplo\arquivos”

Ae ebuhali, tem jeito sim, soh me diz uma coisa, vc ta fazendo em servlet ou em jsp!?

Tanto faz. A gente tá desenvolvendo um projeto usando as duas coisas. Tendo em vista que todo jsp é transformado em servlet, é fácil voce mesmo transformar um jsp em servlet em jsp tambem. Mas acho que vou fazer em uma página jsp.