Olá André,
 
Temos uma classe aqui para isso que veio de algum lugar da internet e tem funcionado muito bem. Para invocar, use algo como:
 
  Upload upload = new Upload();
  try {
   upload.setTargetDir(<<indique o diretório aqui>>);
   upload.uploadFile (request.getInputStream());
catch (Exception e) {
<< trate a exception aqui >>
}
 
Segue o código da classe:
 
 
import java.io.*;
import java.util.*;
 
public class Upload {
    private final   int         BUFFERSIZE = 8192;
    private final   int         MAXTOKEN   = 1024;
    private int                 action = 10;    // 10 - Upload dont't started
                                                // 20 - Upload in action
                                                // 30 - Upload terminated
    private int                 bytesToRead = 1000000;
    private String              targetDir = "";
    private Vector  allFiles =  new Vector(16);
    private int     upindex  =  -1;
 
    public boolean uploadFile (InputStream isFromWeb) throws Exception {
        DataInputStream disFromWeb = new DataInputStream(isFromWeb);
 
        String boundary = disFromWeb.readLine()+"--";
        StringTokenizer stt = new StringTokenizer(disFromWeb.readLine(), "\"=\n");
        stt.nextToken(); stt.nextToken(); stt.nextToken();
        if (! stt.hasMoreElements()) return false;
        String filename = (new File(stt.nextToken())).getName();
 
        StringTokenizer filenameToken = new StringTokenizer(filename, "\\");
        while (filenameToken.hasMoreElements())
         filename = filenameToken.nextToken();
 
        filenameToken = new StringTokenizer(filename, "/");
        while (filenameToken.hasMoreElements())
         filename = filenameToken.nextToken();
 
        while (!disFromWeb.readLine().equals("")) ;
 
        byte    bytesBuffer[]          = new byte[BUFFERSIZE+MAXTOKEN];
        boolean end                    = false;
        int     allBytesRead           = 0;
        int     thisRead               = 0;
        int     lastRead               = 0;
        int     toProcess              = 0;   // Always equals to MAXTOKEN
        FileOutputStream outFileStream = new FileOutputStream(targetDir + filename);
 
        do {
            for (int i=0; i < toProcess; i++)
                bytesBuffer[i] = bytesBuffer[thisRead+i];
            thisRead = toProcess;
 
            int result;
            do {
                result = disFromWeb.read(bytesBuffer, thisRead, BUFFERSIZE);
            } while ((result != -1)  && ((thisRead += result) < MAXTOKEN));
 
            if (thisRead > MAXTOKEN) {
                thisRead -= MAXTOKEN;
                toProcess = MAXTOKEN;
            }
            else {
                thisRead -= boundary.length() + 4 /* CR+LF at 2 last lines */;
                end = true;
            }
 
            outFileStream.write(bytesBuffer, 0, Math.min(thisRead, bytesToRead - allBytesRead));
            allBytesRead += thisRead;
        } while ((! end) && (allBytesRead < bytesToRead));
 
        while (disFromWeb.read(bytesBuffer, 0, BUFFERSIZE) != -1) ;
        disFromWeb.close();
        outFileStream.close();
 
        allFiles.addElement(filename);
        return true;
    }
 
    public int getBytesToRead() {
        return bytesToRead;
    }
 
    public void setBytesToRead(int bytesToRead) {
        this.bytesToRead = bytesToRead;
    }
 
    public String getTargetDir() {
        return targetDir;
    }
 
    public void setTargetDir(String targetDir) {
        this.targetDir = targetDir;
    }
 
    public int getAction() {
        return action;
    }
 
    public void setAction(int action) {
        this.action = action;
        if (action == 10)
            allFiles.clear();
    }
 
    public boolean getNextUpload() {
        if (++upindex < allFiles.size())
            return true;
        upindex = -1;
        return false;
    }
 
    public String getUploadFileName() {
        return (String) allFiles.elementAt(upindex);
    }
}
 
 
Abraços,
 
Vinícius Teles
 
Improve It - www.improveit.com.br
Applied Software Engineering
R. Rodrigo de Brito, 28 - Botafogo
Rio de Janeiro, RJ, 22280-100, Brasil
+55-21-2542-9968
+55-21-9228-5434
----- Original Message -----
Sent: Monday, July 29, 2002 4:59 PM
Subject: [java-list] UpLoad

Olá,
 
Alguém sabe como eu faço uma classe para fazer um upload.
 
Grato
 
 
Andre

Responder a