这些程序用于上传多个文件和一个被选中的文件。
我已经完成了上传多个文件,现在我应该列出所有文件并从这些列表中删除一个文件。我能够在控制台中列出文件。但是,我的先生告诉我,这不是道路。
我的问题是如何列出这些文件并删除其中一个。
向我解释列出上传文件并选择一个文件并删除的概念。
控制器程序可以正常上传多个文件。
/*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<String> files = new ArrayList<String>();
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 -> 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<Resource> 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*/
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h2>List All Uploaded Files</h2>
<div>
<ul>
<li th:each="file : ${files}">
<a th:href="${file}" th:text="${file}" />
</li>
</ul>
</div>
<div>
Enter File to Delete:<input type="text" name="name" id="id"/><br></br>
<input type="submit" value="delete" />
</div>
<div>
<h3 th:text="${totalFiles}"/>
<a href="/">Back to UploadForm!</a>
</div>
</body>
</html>
[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