From what I understand you need to send a file through an input="file" type element and read the contents of it.
I will 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>
Java model. - 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 exit will be:
a test
second line
three
4
I found the line that has written 4 on it!!
5
I did 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 have no way to test it now, as soon as I test it I return you.
I did some tests with itext and realized that there are zilhares different ways to treat the pdf in question. From what I saw in another question you already have a certain familiarity with itext, so I ask: could you say exactly what would be your question regarding the manipulation of the pdf file? The way to send the pdf can be done as I did with the code below, the difference is that you will not use Filereader, but will use the methods that itext offers.
– Math
I never touched itext, but that’s okay. What I need is to open the pdf and go through all the text contained in it. I will go through each word in the pdf
– Pacíficão
Yes, maybe without knowing, but used, rs.. See: How to separate a PDF file row by row, in Java? and here: Class Pdftextextractor
– Math