Um exemplo de upload de imagens q funcione?

3 respostas
G

Por favor, gostaria muito de aprender e usar no meu projeto(q é apenas para meu aprendizado) de como fazer upload de imagens e depois mostrar a imagem usando mysql como banco. Me passaram um totorial, mas ele não funciona. Por favor, se alguem tiver, só gostaria q me mandassem um exemplo, um sisteminha pequeno com isso. Obrigado

3 Respostas

L

Fala cara,
po, exemplo de upload é raro se achar na internet viu, e a galera nunca posta o código, sempre só falando algumas coisas, aqui vai o código pra vc de um que eu fiz, faz upload do que for, arquivos e imagens.

upload.jsp

<html>
    <head>
    </head>
    
    <body>
    
      <form ACTION="recebeupload.jsp" ENCTYPE="MULTIPART/FORM-DATA" METHOD="POST">
      
          
          <INPUT TYPE="FILE" name="Filename"> <br>
          <INPUT TYPE="SUBMIT" name="Enviar">
      
      
      </form>
    
    </body>
</html>

recebeupload.jsp

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

<%
    String savePath = "\\C:\\imagens\\";
    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 && !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);
    
    }
%>


<img src="<%=filename%>">

savePath é o caminho aonde vc vai guardar o arquivo.

Falou

G

Valew Luciano, coloquei meu savePath para “\C:\eclipse 2.1\workspace\upload\”, mas não esta salvando lá, o q pode ser?

L

Olá Guaraviton,
certifique-se de na página JSP passar o parametro “name=filename” tenta alguem outro diretório para testar.

Criado 12 de janeiro de 2005
Ultima resposta 20 de jan. de 2005
Respostas 3
Participantes 2