あなたの答えはリックジェームズありがとう!
だからこれは私のHTMLフォームです:
<form method="POST" enctype="multipart/form-data" action="/">
<table>
<tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
<tr><td></td><td><input type="submit" value="Upload" /></td></tr>
</table>
</form>
ストレージサービス:
@Service
public class StorageService {
Logger log = LoggerFactory.getLogger(this.getClass().getName());
private final Path rootLocation = Paths.get("upload-dir");
public void store(MultipartFile file){
try {
Files.copy(file.getInputStream(), this.rootLocation.resolve(file.getOriginalFilename()));
} catch (Exception e) {
throw new RuntimeException("FAIL!");
}
}
public Resource loadFile(String filename) {
try {
Path file = rootLocation.resolve(filename);
Resource resource = new UrlResource(file.toUri());
if(resource.exists() || resource.isReadable()) {
return resource;
}else{
throw new RuntimeException("FAIL!");
}
} catch (MalformedURLException e) {
throw new RuntimeException("FAIL!");
}
}
public void deleteAll() {
FileSystemUtils.deleteRecursively(rootLocation.toFile());
}
public void init() {
try {
Files.createDirectory(rootLocation);
} catch (IOException e) {
throw new RuntimeException("Could not initialize storage!");
}
}
}
と最後ではなく、少なくとも私のStorageController:
@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file) {
try {
storageService.store(file);
files.add(file.getOriginalFilename());
} catch (Exception e) {
System.out.println("Upload Failure!");
}
return "redirect:/restaurant/uploadForm.xhtml";
}
私の唯一の問題r今度は、これらのイメージを表示したいとき、リソースディレクトリにないので、パスを見つけることができません。
Project sources
私からの完全なパスを使用する/またはそれは私がたまに別のサーバーで、このWebアプリケーションを実行できるように、プロジェクトのルートディレクトリを使用することが可能であるべきでしょうか?
ありがとうございます!
アプリケーションのコードは作成しません。あなたの問題をいくつかのコードスニペットで提供してください。 – Sim