使用java spring更新数据库中已插入的图像

时间:2018-03-25 03:06:08

标签: file file-upload insert-update

我这样绑,我想使用相同的方法进行插入和更新,同样的形式也可以插入和更新。请帮帮我..这是我的控制器,如果我在更新时选择新图像它工作正常,但如果我在更新时没有选择图像而只更新其他细节,那么图像在数据库中更新为null ..

public ModelAndView addOrUpdateFoodItems(@RequestParam(name = "file")CommonsMultipartFile file, 
                                    @RequestParam(name = "food_name")String food_name,
                                    @RequestParam(name = "food_type")String food_type,
                                    @RequestParam(name = "food_tags")String food_tags,
                                    @RequestParam(name = "food_desription")String food_desription,
                                    @RequestParam(name = "protein_intake")String protein_intake,
                                    @RequestParam(name = "calorie_intake")String calorie_intake,
                                    @RequestParam(name = "carbs_intake")String carbs_intake,ModelAndView model, HttpServletRequest request){
    FoodItemDetails foodItemDetails;
    foodItemDetails=    (FoodItemDetails) request.getSession().getAttribute("FoodItemDetails");
    if(foodItemDetails==null){
         foodItemDetails=new FoodItemDetails();
    }else{
    System.out.println(foodItemDetails.getFood_name());


    }
        foodItemDetails.setFood_name(food_name);
        foodItemDetails.setFood_type(food_type);
        foodItemDetails.setFood_tags(food_tags);
        foodItemDetails.setFood_desription(food_desription);
        foodItemDetails.setProtein_intake(protein_intake);
        foodItemDetails.setCalorie_intake(calorie_intake);
        foodItemDetails.setCarbs_intake(carbs_intake);
        if(file.getOriginalFilename()!=null){
        foodItemDetails.setFood_item_image(file.getBytes());
        foodItemDetails.setFood_item_image_name(file.getOriginalFilename());}
        else{
            foodItemDetails.setFood_item_image_name(foodItemDetails.getFood_item_image_name());
            foodItemDetails.setFood_item_image(foodItemDetails.getFood_item_image());
        }

        boolean done= trainerService.saveOrUpdateFoodItems(foodItemDetails);
        model.setViewName("redirect:/trainer/addFoodItems");
    return model;
}

0 个答案:

没有答案