So I understand you need to send a file through an element of type input="file" and read the contents of it.
I'll need to do a project like the answer I gave in your other question :
The big difference is that you will have to use a FileReader to read the file passed by your input on the home page.
Example:
index.html - a simple html that sends a file from an input file to a servlet
<body>
<form method="get" action="LeArq.do">
<input type="file" name="arquivo" size="chars" />
<input type="submit" value="Envia Arquivo" />
</form>
</body>
Template.java - a very simple class
package com.example.model;
public class Modelo {
public void trataArquivo(String str) {
if(str.equals("4")) {
System.out.println("achei a linha que tem escrito 4 nela!!");
}
}
}
LeInputFile.java - your servlet, mapped to DD as "LeArq.do"
package com.example.web;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.model.Modelo;
public class LeInputFile extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Modelo modelo = new Modelo();
String endereco = req.getParameter("arquivo");
ServletContext context = getServletContext();
String enderecoCompleto = context.getRealPath(endereco);
BufferedReader buff = new BufferedReader(new FileReader(enderecoCompleto));
String str;
while((str = buff.readLine()) != null) {
System.out.println(str); //apenas para você saber o que tem no seu arquivo
modelo.trataArquivo(str);
}
buff.close();
}
}
If you read a file that has the following content:
a test
second line
three
4
5
The output will be:
a test
second line
three
4
I found the line that has written 4 in it !!
5
I made this example on a local server, maybe for a remote server you have to concatenate the full path where the file will be, but I do not have how to test it now, so I test I'll return you.