How to pass a file from the JSP to the servlet?

6

I have a .jsp page where I have an input file. I need to open this file to use the information in it. Suppose it's a pdf. I get this pdf, sending it to the servlet for the purpose of using it in my JAVA application. The easiest way would be to take the whole path of the file and send it in a string. But I do not know if you can do this or how you do it! Another thing, was to get the file in the same .jsp and turn the whole content into string. So pass this string to my JAVA application. But I do not know how to do that either. Any solution? How can I get the file and send it to my application?

    
asked by anonymous 08.07.2014 / 17:01

2 answers

5

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.

    
08.07.2014 / 21:56
2

Friend, this is called file upload =)

Here are great references on the subject: Locaweb and codigofontes.com.br . Both have super simple code to understand, I have nothing else to add.

It seems to me that you did not quite understand the "WEB" issue of the thing. Your .jsp file is processed on the server, but at the end of the day it is interpreted by the client browser ... The servlet is a simple class that stays on the server and accepts socket requests. Note that one is in the other client on the server, communication between these two points is done through the HTTP protocol, it is not simply "move" from one to another.

I suggest you try to understand how (%) request / response works before anything else.

Att

    
08.07.2014 / 21:29