로컬 MVS 프로젝트의 이미지 관리자 시스템에서 로컬 이미지 폴더에 저장된 모든 이미지 갤러리를 표시하고 이미지를 삭제하고 새 이미지를 업로드하는 기본 기능을 사용하여 작업하고 있습니다.스프링 MVC 리소스가 새로 고침되지 않습니다.
새 이미지를 업로드하면 페이지가 방금 추가 한 이미지를 포함하여 이미지 갤러리를 다시로드합니다. 사실 새 이미지가 HD에 정확하게 저장되지만 Java 프로젝트의 resources/img 폴더에는 자동으로 표시되지 않습니다. 따라서 페이지가 다시로드되면 새 이미지가 없습니다. 프로젝트를 수동으로 새로 고칠 때만 새 이미지가 resources/img 폴더에 나타납니다.
이상한 점은 삭제 방법과 동일한 문제가 없습니다. 일단 이미지가 삭제되면 HD 및 resources/img 폴더에서 사라지고 페이지가 이미지를 표시하지 않고 갤러리를 다시로드합니다. 방금 삭제되었습니다.
어디에서 문제가 발생할 수 있으십니까? 이 때문에 IDE 서버 자동 배포에 아마 상쾌
WebContent/resources/img
:
여기 내 컨트롤러
@Controller
public class imagesManagerController {
// READ ALL FILES FROM IMG FOLDER
@RequestMapping(value = "/imagesManager", method = RequestMethod.GET)
public ModelAndView readImages
(@RequestParam(value = "error", required = false) String error) {
// create model and link it to jsp imagesManager
ModelAndView model = new ModelAndView("imagesManager");
// return content from images folder and add it to model
File imgsPath = new File("C:/Users/Alessandro/workspace/SpringMVCBlog/WebContent/resources/img");
String[] imgsNames = imgsPath.list();
model.addObject("imgsNames", imgsNames);
//if upload fails, display error message
if (error != null) {
model.addObject("error",
"Please select a file to upload");
}
return model;
}
//UPLOAD FILE TO HD
@RequestMapping(value = "/imagesManager/upload", method = RequestMethod.POST)
public String handleFileUpload (@RequestParam("file") MultipartFile file) {
//get img name
String imgName = file.getOriginalFilename();
System.out.println(imgName);
//create file path
String folder = "C:/Users/Alessandro/workspace/SpringMVCBlog/WebContent/resources/img/";
File path = new File (folder+imgName);
System.out.println(path);
if (!file.isEmpty()) {
try {
//get bytes array from file
byte[] bytes = file.getBytes();
//create output stream
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(path));
//write img content on path
stream.write(bytes);
//close stream
stream.close();
//if upload is successful, reload page
return "redirect:/imagesManager";
} catch (Exception e) {
return "You failed to upload " + imgName + " => " + e.getMessage();
}
} else {
return "redirect:/imagesManager?error";
}
}
// DELETE FILE FROM HD
@RequestMapping(value = "/imagesManager/delete", method = RequestMethod.POST)
public String deleteFile(@RequestParam (value="imgName") String imgName) {
//create file path to be deleted
String folder = "C:/Users/Alessandro/workspace/SpringMVCBlog/WebContent/resources/img/";
File path = new File (folder+imgName);
// delete file
if (path.delete()) {
//if delete is successful, reload page
return "redirect:/imagesManager";
} else {
return "Delete operation failed";
}
}
}
업로드 된 파일을 로컬 디스크에 저장하고 싶습니다. 즉, 내가 C : \ Users \ Public \ Pictures와 같은 IDE 프로젝트 폴더에 저장하지 말아야한다는 말입니까? –
수정하십시오. 프로덕션 배포의 경우 클라우드 또는 전용 파일 서버를 사용해야합니다. –
좋습니다.하지만 IDE 프로젝트 폴더에 파일을 저장하지 않으면 컨트롤러에서 어떻게 읽을 수 있습니까? DB 또는 외부 서버/클라우드가 아닌 jpg로 로컬 디스크에 파일을 저장하려고합니다. –