删除服务器上图片的方法


在web开发时,若要删除一条记录,且对应该记录中的有一个表示图片的字段,那么在删除此记录时,还需要通过程序来删除服务器上图片的路径:

如根据id删除某个brand时,

业务层如下 :

brandService:

[java] view plain copy
1.@Override
2. public void removeBrand(int brandId) {
3. // long annexCount = annexDao.getAnnexCountByBrand(brandId);
4. // long lightCount = lightDao.getLightCountByBrand(brandId);
5. // if (annexCount == 0 && lightCount == 0) {
6. // brandDao.delete(brandId);
7. // return brandId;
8. // } else {
9. try {
10. imageSourceService.removeImageSourceByImgCode(ImageConstans.IMG_CODE_BRAND+brandId);//先删除图片后,在删除brand
11. brandDao.removeBrandByBrandId(brandId);
12. } catch (Exception e) {
13. logger.error("修改品牌并删除品牌照片时发生异常,brandId="+brandId+e.getMessage(),e);
14. }
15.
16. // }
17. }
ImageSourceService:


[java] view plain copy
1. @Override
2. public void removeImageSourceByImgCode(String code) throws Exception {
3. ImageSourceQuery imgQuery = new ImageSourceQuery();
4. imgQuery.setImgCode(code);
5. List<ImageSource> sources = listImages(imgQuery);
6. if (LogicUtils.isNotNullAndEmpty(sources)) {
7. for (ImageSource img : sources) {
8. removeImageSourceById(img.getImgId());
9. }
10. }
11. }
12.
13.@Override
14. public void removeImageSourceById(int imgId) {
15. ImageSource s = imageSourceDao.findOne(imgId);
16. String filePath = s.getImgPhysical();
17. imageSourceDao.delete(s);
18. String filePahtSuffix = filePath.substring(filePath.indexOf(".")+1);
19. List<String> files = new ArrayList<String>();
20. files.add(filePath);
21. files.add(filePath+"_b."+filePahtSuffix);//因存入图片时,有大,中,小文件,所以分别找到图片,删除图片
22. files.add(filePath+"_m."+filePahtSuffix);
23. files.add(filePath+"_s."+filePahtSuffix);
24.
25. if(LogicUtils.isNotNullAndEmpty(files)){
26. Iterator iter = files.iterator();
27. while(iter.hasNext()){
28. String path = (String)iter.next();
29. File file = new File(path);
30. if (file.exists()){
31. file.delete();
32. }
33. }
34. }
35. }

同样在做修改时,在提交修改内容后,要有图片做修改,同样也需要删除图片

控制层如下:

[java] view plain copy
1./**
2. * 保存品牌修改后的数据,并跳转到品牌列表页面
3. * @param brand
4. * @param categoryIds
5. * @param file
6. * @param request
7. * @return
8. */
9. @RequestMapping(value = "editBrand")
10. public String editBrand(@ModelAttribute("brand") Brand brand, @RequestParam("categoryIds") int[] categoryIds,
11. @RequestParam("file") CommonsMultipartFile file,
12. HttpServletRequest request) {
13. if(categoryIds != null && categoryIds.length > 0){
14. Set<Category> categories = new HashSet<Category>();
15. for (int i = 0; i < categoryIds.length; i++) {
16. int cateId = categoryIds[i];
17. Category category = new Category();
18. category.setCategoryId(cateId);
19. categories.add(category);
20. }
21. brand.setCategories(categories);
22.
23. }
24. if(file != null && file.getSize() > 0){
25. try {
26. imageSourceService.removeImageSourceByImgCode(ImageConstans.IMG_CODE_BRAND+brand.getBrandId()); //若有修改过图片,先删除之前图片
27. } catch (Exception e) {
28. systemsServicelog.log(request,"修改品牌删除原有品牌照片时发生异常,brandId="+brand.getBrandId());
29. }
30.
31. String path = ImagesUtils.BRAND_IMAGE_BASE_PATH;
32. String imgCode=ImageConstans.IMG_CODE_BRAND+brand.getBrandId();
33. ImageSource img=new ImageSource();
34. img.setImgCode(imgCode);
35. img = ImageUploadUtil.uploadImage(img,path,file, request);
36. brand.setPicPath(img.getImgPath());
37. }
38. brandService.updateBrand(brand);
39. //增加系统日志
40. systemsServicelog.log(request,"修改了品牌,brandId="+brand.getBrandId());
41. return "redirect:../common/success.do?reurl=brand/brands.do";
42. }
 

关闭
18988993509

020-82315523