E aí galera beleza?
TEnho que fazer upload em um site por jsp mas, numca trabalhei com upload. Alguém tem uma luz para eu poder começar?
Desde já agradeço!
E aí galera beleza?
TEnho que fazer upload em um site por jsp mas, numca trabalhei com upload. Alguém tem uma luz para eu poder começar?
Desde já agradeço!
Cara, upload eu nao sei, mas download é assim:
public class Baixador {
/**A url de onde será feito o download (inclui o nome do arquivo)<em>/
private URL url;
/**O local onde o arquivo será gravado na máquina local (inclui o nome do arquivo)</em>/
private String destino;
/**
* Cria uma instancia deste objeto
* @param endNet Endereço de onde será baixado o arquivo (inclui o nome do arquivo)
* @param destino O caminho da máquina local onde o arquivo será gravado (inclui o nome do arquivo)
*/
public Baixador( String endNet, String destino){
try{
url = new URL( endNet );
}catch( MalformedURLException e ){
System.out.println( "Erro: " + e.getMessage() );
}
this.destino = destino;
}
/**
* Faz o download do arquivo para do local especificado
* em url para o local especificado em destino
*/
public void Baixar(){
byte b[] = new byte[1];
try{
InputStream arq = url.openConnection().getInputStream();
FileOutputStream arqNovo = new FileOutputStream( destino );
int tamanho = arq.read(b, 0, 1);
while( tamanho != -1){
arqNovo.write( b, 0, tamanho );
tamanho = arq.read(b, 0, 1);
}
}catch( Exception e ){
System.out.println( e.getMessage() );
}
}
}
Vê se o download te ajuda!
pra tua sorte eu fiz um upload exatamente hj no trabalho, eahehahae, ta ai cara
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( "\"" ) );
//out.print(dataBytes);
int lastIndex = contentType.lastIndexOf( "=" );
String boundary = contentType.substring( lastIndex + 1, contentType.length() );
//out.println(boundary);
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( "teuDiretorio" + "\\" + saveFile );
//fileOut.write(dataBytes);
fileOut.write( dataBytes, startPos, ( endPos - startPos ) );
fileOut.flush();
fileOut.close();
out.println("File saved as " + saveFile );
Hum… vou tentar.
Vc seta o contentType p/o arquivo que vc quer.
Transforma o seu arquivo em um array de bytes.
E escreve atraves da classe ServletOutputStream, que vc pode pegar atraves de response.getOutputStream().
Bem, nunca fiz. Só tinha feito pelo struts. :oops: