У меня проблема, потому что я могу просто обработать один файл в jsp, я не знаю, как отличить один файл от другого. Я использую JSP (без фреймворка или сервлета). Вот мой код, работающий для одного CSV-файла.
<form class="form-horizontal" action="carga_datos2.jsp" method="post" name="uploadCSV"
enctype="multipart/form-data">
<div class="input-row">
<h4 class="control-label">CSV Curva Spot</h4>
<input type="file" name="file_curva_spot" id="file" accept=".csv">
<br/>
<br/>
</div>
<div class="input-row">
<h4 class="control-label">CSV Instrumento Emisores</h4>
<input type="file" name="file_emisores" id="file" accept=".csv">
<br/>
<br/>
</div>
<button type="submit" id="submit" name="import" class="btn btn-lg btn-primary">Cargar <i
class="fa fa-cloud-upload" aria-hidden="true"></i></button>
<div id="labelError"></div>
</form>
Как видите, у меня есть два INPUT для каждого CSV-файла. Вот часть «обработки» кода (carga_datos2. jsp)
<%
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 name = file.substring(file.indexOf("name=\"")+6);
System.out.print("name="+name);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
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(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
/**/
Statement pst=null;
String line = null;
String value=null;
String[] column = null;
String anios= "";
String tasa = "";
String fecha = "";
java.util.Date datef;
String moneda = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sds = new SimpleDateFormat("dd/MM/yyyy");
try{
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));
//Saltando headers
input.readLine();
while (( line = input.readLine()) != null){
System.out.print("Line:"+line.toString());
//Saving values in columns
column = line.split(",");
anios = column[0];
tasa = column[1];
fecha = sdf.format(sds.parse(column[2]));
moneda = column[3];
}
//value = contents.toString();
}
catch(Exception e)
{}
}
Я не могу обработать каждый файл другим алгоритмом, потому что не знаю, как различать каждый файл.