Boa tarde pessoal estou desenvolvendo um sistema de cadastro e preciso anexar arquivos no final do cadastro, quando clico em salvar o arquivo já e encaminhado para um diretório, criei uma tabela do BD para receber o id do cadastro mais o nome do arquivo, até aqui tudo bem.
o problema e que como os arquivos vão ficar em uma mesma pasta pode dar conflitos de arquivos com o mesmo nome.
preciso que quando o arquivo for igual a um nome que já consta na tabela do bd, ele altere automaticamento o nome do arquivo.
exemplo
ja tem uma arquivo como “teste.txt”, dai altera-se para “teste1.txt”, ou qualquer coisa que de para diferenciar.
segue em anexo as paginas
a pagina que anexo os aquivos Anexar.jsp
<form enctype="multipart/form-data" action="uploadandstore.jsp" method="post">
<br></br><br/>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<td colspan="2" align="center"><center><b>Anexar Arquivo</b></center></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td><center><b>Selecione o anexo desejado:</b></center></td>
<td><input name="file" type="file"/></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Carregar"/><img id="imagemCarregandoGravCad" src="imagens/ajax-loader-barra.gif" /> </td>
</tr>
</table>
a pagina que faz o processo de comparação que já esta pronta só falta renomear uploadandstore.jsp
<%@ page contentType="text/html;charset=ISO-8859-1" language="java" import="java.sql.*" pageEncoding="ISO-8859-1" errorPage="" %>
<%@page import="org.apache.catalina.connector.Request"%>
<%@page import="sicoob.ConexaoBD"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%
String saveFile="";
String contentType = request.getContentType();
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);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
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;
String nomearquivo = saveFile;
saveFile="C:/ArqGravame/"+saveFile;
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
<%
//ok
sicoob.ConexaoBD coxAlmoxarifado = new sicoob.ConexaoBD();
Connection conn2 = coxAlmoxarifado.getConexaoBD();
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
String data = sdf.format(new Date());
PreparedStatement psGetid = conn2.prepareStatement("select * from gravame where usuario = ? and id_pac_usuario = ? and datacadastro = ? order by id");
psGetid.setString(1,(String)session.getAttribute("usuario"));
psGetid.setString(2,(String)session.getAttribute("id_PAC"));
psGetid.setString(3,data);
ResultSet rsGetid = psGetid.executeQuery();
int getID = 0;
while (rsGetid.next()) {
getID = rsGetid.getInt("id");
}
//aqui se for igual ao nome do arquivo já existente na tabela ele deveria trocar o nome
PreparedStatement psVerSaldoDia = conn2.prepareStatement("select * from anexos WHERE nomearquivo = ?");
psVerSaldoDia.setString(1, nomearquivo);
ResultSet rsVerSaldoDia = psVerSaldoDia.executeQuery();
if (rsVerSaldoDia.next()){
//condição que se não ouver outro arquivo igual ele inseri no bd e ba pasta gravame
}else{
PreparedStatement psIsert = conn2.prepareStatement("INSERT INTO anexos (id_gravame, nomearquivo) VALUES (?, ?)");
psIsert.setInt(1, getID );
psIsert.setString(2, nomearquivo );
psIsert.executeUpdate();
}
%>
<%
}
%>