[Java] 修改图片 →→→→→进入此内容的聊天室

来自 , 2020-05-12, 写在 Java, 查看 104 次.
URL http://www.code666.cn/view/07871915
  1. /**
  2.          *
  3.          * 修改
  4.          * @throws Exception
  5.          * @throws Exception
  6.          *
  7.          */
  8.        
  9.         //带图片的增删改查
  10.         //删除原来的图片
  11.         //上传新图片
  12.         //不修改原来图片获取原来图片
  13.         public String update() throws Exception  {
  14.                 File file = this.getFile();
  15.                 if(file != null && !("").equals(toGongQiuShangJiaXinXi.getGimge()) ) {  //如果不存在(没有改变) 获取原来图片的路径
  16.                         String oldpath  = ServletActionContext.getServletContext().getRealPath("/upload/");
  17.                         System.out.println(oldpath);
  18.                         File oldImageFile = new File(oldpath + File.separator + toGongQiuShangJiaXinXi.getGimge());
  19.                         if (oldImageFile.exists()) {
  20.                                 oldImageFile.delete();
  21.                         }
  22.                         ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
  23.                         String newpath = ServletActionContext.getServletContext().getRealPath("upload/"+ fileFileName);
  24.                         System.out.println(newpath);
  25.                         FileOutputStream fos = new FileOutputStream(newpath);
  26.                         FileInputStream fis = new FileInputStream(file);
  27.                         byte[] butter = new byte[1024];
  28.                         int len = 0;
  29.                         while ((len = fis.read(butter))!= -1) {
  30.                                 fos.write(butter,0,len);
  31.                         }
  32.                         fos.close();
  33.                         fis.close();
  34.                         toGongQiuShangJiaXinXi.setGimge(fileFileName);
  35.                 } else {
  36.                         if (toGongQiuShangJiaXinXi.getGimge().equals(1)) {
  37.                                 ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
  38.                                 //图片没有修改时,输入框中的值为NULL,获取原来图片的地址在上传
  39.                                 String path = ServletActionContext.getServletContext().getRealPath("upload/"+ toGongQiuShangJiaXinXi.getGimge());
  40.                                 System.out.println(path);
  41.                                 FileOutputStream fos = new FileOutputStream(path);
  42.                                 FileInputStream fis = new FileInputStream(file);
  43.                                 byte[] butter = new byte[1024];
  44.                                 int len = 0;
  45.                                 while ((len = fis.read(butter))!= -1) {
  46.                                         fos.write(butter,0,len);
  47.                                 }
  48.                                 fos.close();
  49.                                 fis.close();
  50.                                 toGongQiuShangJiaXinXi.setGimge(fileFileName);
  51.                         } else {
  52.                                 System.out.println("图片不能为空!!!");
  53.                         }
  54.                 }
  55.                 toGongQiuShangJiaXinXiService.update(toGongQiuShangJiaXinXi);
  56.                 return "update";
  57.                 }

回复 "修改图片"

这儿你可以回复上面这条便签

captcha