Uploading files with Java

1

Good morning! I was trying to upload files, where I have a webpage that makes a request for my servlet. In the form of the page I have an input of type file and another one of type text. The coding mode of this form is as "multipart / form-data". My major difficulty is that all the examples I've seen using the package org.apache.tomcat.util.http.fileupload have the following code snippet:

    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();

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

  

The method parseRequest (RequestContext) in the type FileUploadBase is not applicable for the arguments (HttpServletRequest)

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

Hugs.

    
asked by anonymous 31.01.2018 / 11:10

2 answers

2

The parseRequest (RequestContext ctx) expects RequestContext to be an argument, but the approved argument is instance of HttpServletRequest

Use ServletRequestContext to create a RequestContext instance as follows.

List<FileItem> items = upload.parseRequest(new ServletRequestContext(request));

link

    
31.01.2018 / 12:45
0

The method of the function you are calling is expecting 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();
        }
    }
    
31.01.2018 / 11:33