title: File Deletion from uploaded files
body: <p>These program is for uploading multiple files and one file which is selected. </p>
  
  <p>I have done with uploading multiple files and now I should list all files and delete one file from those list. I am able to list the files in console. But, my sir told me that is not the way.</p>
  
  <p>MY question is how to list those files and delete one among them.</p>
  
  <p>Explain me the concept of listing the upload files and selecting one file and delete. </p>
  
  <p>Controller program is working fine for multiple file upload.</p>
  
  <pre><code>/*This is controller program*/
  
  import java.io.File;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.stream.Collectors;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.core.io.Resource;
  import org.springframework.http.HttpHeaders;
  import org.springframework.http.ResponseEntity;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.ResponseBody;
  import org.springframework.web.multipart.MultipartFile;
  import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
  
  import com.javasampleapproach.uploadfile.storage.StorageService;
  
  @Controller
  public class UploadController {
  
      @Autowired
      StorageService storageService;
  
      List&lt;String&gt; files = new ArrayList&lt;String&gt;();
      final String directoryWindows ="E://temp";
  
      @GetMapping("/")
      public String listUploadedFiles(Model model)
      {
          return "uploadForm";
      }
  
      @PostMapping("/")
      public String handleFileUpload(@RequestParam("filefield") File fname,
                                     @RequestParam("file") MultipartFile file, 
                                     Model model) 
      {
          try
          {
              storageService.store(file);
              model.addAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!");
              files.add(file.getOriginalFilename());
  
                  String filename =fname.getName();
  
                  System.out.println(filename);
                  File folder=new File("E:\\temp\\"+filename+"\\");
  
                  folder.mkdirs();
                  System.out.println(folder);
  
                  byte[] bytes = file.getBytes();
                  Path path = Paths.get("E:\\temp\\"+filename+"\\"+ file.getOriginalFilename());
                  Files.write(path, bytes);
  
                  listFilesForFolder(directoryWindows);
  
  
          }
          catch (Exception e) 
          {
              model.addAttribute("message", "FAIL to upload " + file.getOriginalFilename() + "!");
          }
          return "uploadForm";
      }
  
      public void listFilesForFolder(String folder)
      {
          File directory=new File(folder);
  
          for (final File fileEntry : directory.listFiles()) 
          {
               System.out.println(fileEntry.getName());
          }
       }
  
  
      @GetMapping("/gellallfiles")
      public String getListFiles( //@RequestParam("name") File first,
                                  Model model)
      {
          model.addAttribute("files",
                  files.stream()
                          .map(fileName -&gt; MvcUriComponentsBuilder
                                  .fromMethodName(UploadController.class, "getFile", fileName).build().toString())
                          .collect(Collectors.toList()));
          model.addAttribute("totalFiles", "TotalFiles: " + files.size());
          //String filesnames =first.getName();
          //System.out.println("welcome"+filesnames);
          return "listFiles";
  
  
  
      }
  
      @GetMapping("/files/{filename:.+}")
      @ResponseBody
      public ResponseEntity&lt;Resource&gt; getFile(@PathVariable String filename) 
      {
          Resource file = storageService.loadFile(filename);
          return ResponseEntity.ok()
                  .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
                  .body(file);
      }
  }
  
  
  /* This is html program*/
  
  &lt;html xmlns:th="http://www.thymeleaf.org"&gt;
  &lt;body&gt;
      &lt;h2&gt;List All Uploaded Files&lt;/h2&gt;
      &lt;div&gt;
          &lt;ul&gt;
  
              &lt;li th:each="file : ${files}"&gt;
                  &lt;a th:href="${file}" th:text="${file}" /&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/div&gt;
      &lt;div&gt;
          Enter File to Delete:&lt;input type="text" name="name" id="id"/&gt;&lt;br&gt;&lt;/br&gt;
          &lt;input type="submit" value="delete" /&gt;
      &lt;/div&gt;
      &lt;div&gt;
          &lt;h3 th:text="${totalFiles}"/&gt;
          &lt;a href="/"&gt;Back to UploadForm!&lt;/a&gt;
      &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  
  
  
  [output][1]
  
  /* This is how output is printed */
  How can I list those files and select one among them and delete it.
  
    [1]: https://i.stack.imgur.com/RzLVQ.png
  </code></pre>