Uploading files with Java

Asked

Viewed 1,756 times

1

Good morning! I was trying to upload files, where I have a web page that makes a request to my Folder. In the form of the page I have an input of type file and another of type text. The encoding mode of this form is as "Multipart/form-data". My great difficulty is that all the examples I’ve seen of using package org.apache.Tomcat.util.http.fileupload have the following snippet of code:

    try
    { 
        // Analisar a solicitação para obter itens de arquivo.
        List <FileItem>fileItems = upload.parseRequest(request);

        // Processar os itens de arquivos enviados
        Iterator <FileItem> i = fileItems.iterator();

However in my case occurs the error of the image below:

erro_eclipse

The method parseRequest(Requestcontext) in the type Fileuploadbase is not applicable for the Arguments (Httpservletrequest)

If anyone has any suggestions or has ever had the same problem and can help me, I appreciate it. Or if you have any other more recommended method of uploading files in java I am accepting tips, because this is the first time I am studying about it.

Hugs.

2 answers

2


  • That worked, thank you so much for the answer, I was breaking my head a long time ago :)

  • I’m glad it worked out.

0

The function method you are calling is waiting for an object of another type.

Try to do according to the example below:

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        /*Obtem o caminho relatorio da pasta img*/
        String path = request.getServletContext().getRealPath("img")+ File.separator;

        File files = new File(path);
        response.setContentType("image/jpeg");

        /*Mostra o arquivo que está na pasta img onde foi realizado o upload*/
        for (String file : files.list()) {
            File f = new File(path + file);
            BufferedImage bi = ImageIO.read(f);
            OutputStream out = response.getOutputStream();
            ImageIO.write(bi, "jpg", out);
            out.close();
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.