我正在尝试将一些文件上传到我的spring mvc web应用程序中。我在WEB-INF中创建了一个img文件夹,并尝试将上传的图像放入其中。但我无法找到正确的道路。下面的代码不起作用。
//Doesn`t get into the if (dir.isDirectory()) condition
Iterator<String> itr = req.getFileNames();
MultipartFile file = req.getFile(itr.next());
String fileName = file.getOriginalFilename();
File dir = new File("img");
if (dir.isDirectory()) {
System.out.println("directory found");
File serverFile = new File(dir, fileName);
BufferedOutputStream stream;
try {
stream = new BufferedOutputStream(new FileOutputStream(serverFile));
stream.write(file.getBytes());
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}