Upload

3 respostas
D

Galera, eu to com um problema eu to fazendo o upload de um arquivo, mas na hora que eu vou salvar ele em um diretorio ele ta dando um erro, pra salvar em um diretorio o arquivo tem que usar barras ao contrario certo?!
\

Bom, ai vai o codigo se alguem conseguir ver algo errado…

<%@ page import=<a href="http://java.io">java.io</a>.*” %>

<%

String savePath = T:\webapps\pc;(onde esta dando erro!!)

String filename = “”;
ServletInputStream  in = request.getInputStream();
byte[] line = new byte[128];
int i = in.readLine(line, 0, 128);
int boundaryLength = i - 2;
String boundary = new String(line , 0, boundaryLength);

while(i != -1){
  String newLine = new String (line, 0 , i);
	if(newLine.startsWith("Content-Disposition: form-data; name=\"")){
	  String s = new String(line, 0, i-2);
	  int pos = s.indexOf("filename=\"");
	    if(pos != -1){
	      String filepath = s.substring(pos+10, s.length()-1);
	      pos = filepath.lastIndexOf("\\");
	        if(pos != -1)
	          filename = filepath.substring(pos + 1);
	           	else
	           	  filename = filepath;
	    }
	        
	        i = in.readLine(line, 0, 128);
	        i = in.readLine(line, 0, 128);
	        i = in.readLine(line, 0, 128);
	        
	        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
	        newLine = new String (line, 0 , i);
	        
	          while(i != -1 &amp;&amp; !newLine.startsWith(boundary)){
	          buffer.write(line, 0, i);
	          i = in.readLine(line, 0, 128); 
	          newLine = new String(line , 0, i);
	          }
	            try{
	              RandomAccessFile f = new RandomAccessFile(savePath + filename, "rw");
	              byte[] bytes = buffer.toByteArray();
	              f.write(bytes, 0, bytes.length -2);
	              f.close();
	            }
	            catch(Exception e){}
	}
	    i = in.readLine(line, 0, 128);
}

%>

Obrigado,

Atenciosamente,

Eduardo

3 Respostas

V
<blockquote><div class="quote-author">“dumestico”:</div>

String savePath = “T:\webapps\pc”;(onde esta dando erro!!)</blockquote>

substitua por

String savePath = “T:\webapps\pc\”; ou

String savePath = “T:/webapps/pc/”;

depois somente concatene o nome do arquivo!
D

Cara, agora ele nao esta mais dando mensagem de erro, mas eu abro o diretorio e ele nao esta la…

Existe mais alguma modificação que deve ser feita!?

Obrigado,

Atenciosamente

V

cara, devo te confessar que não entendi teu algoritmo e, portanto, não posso te dizer onde mexer…

experimente transformar esse teu JSP em um servlet e daí fazer algo mais organizado (desculpe dizer isso!) onde vc possa debugar melhor o q está sendo feito!

se vc tah querendo fazer upload de arquivos experimente pesquisar o Jakarta Commons FileUpload, é provável que facilite mto o teu trabalho…

Criado 22 de fevereiro de 2005
Ultima resposta 22 de fev. de 2005
Respostas 3
Participantes 2