Galera,
to com um problema que imagino ser bobo, não está cadastrando o nome do arquivo que quero fazer upload no banco, seria o filename ta tudo certo com o banco , tabelas e conexão, não sei o que pode estar errado se alguém puder ajudar.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.gjt.mm.mysql.Connection;
import conexao.Conexao;
public class Upload extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
String savePath = "/nome_do_caminho_correto/";
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+13, s.length() - 1);
pos = filepath.lastIndexOf("/");
if(pos != -1) {
filename = filepath.substring(pos + 1);
} else {
filename = filepath;
}
Conexao con = new Conexao();
Connection conn = (Connection) con.retornaConexao();
if (conn != null) {
try {
PreparedStatement ps = conn.prepareStatement("INSERT INTO arquivo (Imagem) VALUES(filename)");
int ii = ps.executeUpdate();
if(ii != 0) System.out.println ("ARQUIVO INCLUÍDO");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
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);
}
}
}
}