Spring MVC资源没有刷新

时间:2017-01-02 16:28:21

标签: image spring-mvc refresh

我正在为我的Spring MVC项目开发一个图像管理器系统,其基本功能是显示存储在本地图像文件夹中的所有图像的库,删除图像和上传新图像。

我希望,一旦上传新图像,页面就会重新加载图片库,包括刚刚添加的图片。事实上,新图像正确保存在HD上,但它并没有自动显示在Java项目的resources / img文件夹中;因此,一旦重新加载页面,新图像就不存在了。仅当我手动刷新项目时,新图像才会显示在resources / img文件夹中。

奇怪的是,我对删除方法没有同样的问题:一旦删除了图像,它就会从HD和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";
    }
}

}

1 个答案:

答案 0 :(得分:1)

问题出在路径上:

WebContent/resources/img

可能是由于IDE服务器自动部署而令人耳目一新。使用%TEMP%路径进行测试并检查。

1)您不应将上传的文件保存到应用程序服务器文件系统。

2)您不应将上传的文件保存到应用程序文件夹中,因为它是部署的一部分。它只会被部署一次,该文件夹仅用于应用程序文件。

相反,请使用云或专用文件系统。