Pessoal, estou com um problemão aqui…
É o seguinte, tenho uma página que faz upload de um arquivo .cvs e depois lê ele e imprime os dados.
Está acontecendo o seguinte: Qdo existem caracteres especias como "Ç " no arquivo, dá esse erro “java.lang.ArrayIndexOutOfBoundsException”
Notei que qdo eu retiro este tipo de caracter do texto funciona perfeitamente.
Existem também em uma parte do código um “split(”;")", qdo está sem o “Ç” no texto a contagem dos vetores dá certinho.
Exemplo: tenho 3 linhas e 4 colunas no arquivo .cvs… O correto é aparecer 4 4 4 na contagem dos vetores. Só que qdo tem um “ç” a contagem aparece assim 4 4 4 1 e dá pau.
Sei lá o pq aparece esse 1 a mais.
Esse código peguei na NET e estou fazendo adaptações…
int num = strar.length;
Acredito que o meu problema está aqui, mas não consigo resolver.
String filePathOnServer= "/opt/www/tomcat/webapps/portal/"+saveFile;
FileInputStream fis = new FileInputStream(filePathOnServer);
DataInputStream myInput = new DataInputStream(fis);
String thisLine;
while ((thisLine = myInput.readLine()) != null)
{
String strar[] = thisLine.split(";");
int num = strar.length;
out.println(num);
//out.print(" <b>" +strar[0]+strar[1]+strar[2]+strar[3]+ "</b><br>");
}
Alguém pode me ajudar.
Segue abaixo o meu código:
Form:
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<HTml>
<HEAD>
<meta http-equiv="Content-Type" content="multipart/form-data; charset=ISO-8859-1" />
<TITLE>Display file upload form to the user</TITLE></HEAD>
<BODY>
<FORM ENCTYPE="multipart/form-data" accept-charset="ISO-8859-1" ACTION="upload_page.jsp" METHOD=POST>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></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="Send File" ></td>
</tr>
</table>
</FORM>
</BODY>
</HTML>
Upload_page.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.io.*,java.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="multipart/form-data; charset=ISO-8859-1;" />
</head>
<%
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);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.println("saveFile=" + saveFile);
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
//out.println("saveFile" + saveFile);
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;
FileOutputStream fileOut = new FileOutputStream("/opt/www/tomcat/webapps/portal/"+saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
String filePathOnServer= "/opt/www/tomcat/webapps/portal/"+saveFile;
FileInputStream fis = new FileInputStream(filePathOnServer);
DataInputStream myInput = new DataInputStream(fis);
String thisLine;
while ((thisLine = myInput.readLine()) != null)
{
String strar[] = thisLine.split(";");
int num = strar.length;
out.println(num);
//out.print(" <b>" +strar[0]+strar[1]+strar[2]+strar[3]+ "</b><br>");
}
%>
<b>File <% out.println(saveFile); %> has been uploaded and inserted into Database.</b>
<%
}
%>
</html>