spring MVCを使用した画像をsrc/main/webapp/resourcesにアップロードして、jspのimgタグ(<img src="<c:url value="/resources/images/image.jpg" />" alt="image" />
)に表示しようとしています。 。私はこのコントローラを持っている:C:/absolute/path/to/webapp/resources
または/absolute/path/to/webapp/resources
:Spring MVCでsrc/main/webapp/resourcesに画像をアップロードするには
@Controller
public class FileUploadController implements ServletContextAware {
private ServletContext servletContext;
private String rootPath;
@RequestMapping(value = "/uploadSingleFile", method = RequestMethod.GET)
public ModelAndView uploadSingleFileFormDisplay() {
return new ModelAndView("uploadSingleFile");
}
@RequestMapping(value = "/uploadSingleFile", method = RequestMethod.POST)
public @ResponseBody String uploadSingleFileHandler(@RequestParam("file") MultipartFile file) {
String filename = file.getOriginalFilename();
rootPath = servletContext.getRealPath("/") + "resources/uploads";
if (!file.isEmpty()) {
try {
Document document = new Document();
Image image = new Image();
byte[] bytes = file.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(
rootPath + "/" + filename
)));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + filename + "!";
} catch (Exception e) {
return "You failed to upload " + filename + " => " + e.getMessage();
}
} else {
return "You failed to upload " + filename + " because the file was empty.";
}
}
(...)
}
は、どのように私はこのような私のシステムの絶対パスを持っているrootPath
を構築するのですか?
アップロードパスプロパティの定義方法は? – simslay
回答が更新されました – StanislavL