文件上传显示保存到数据库实现类

  1. public class UpLoadAction extends ActionSupport implements  
  2.                 ServletRequestAware,ServletResponseAware {  
  3.     private HttpServletRequest request;  
  4.     private HttpServletResponse response;     
  5.     private List<File> uploadFile;  
  6.     private List<String> uploadFileContentType;  
  7.     private List<String> uploadFileFileName;  
  8.       
  9.       
  10.     public void setServletRequest(HttpServletRequest request) {  
  11.         this.request = request;  
  12.     }  
  13.   
  14.     public void setServletResponse(HttpServletResponse response) {  
  15.         this.response = response;  
  16.     }  
  17.       
  18.       
  19.     /** 
  20.      * 上传图片,至路径保存至数据库 
  21.      * @return  
  22.      * @throws Exception 
  23.      */  
  24.     public String uploadImage() throws Exception {  
  25.         String savePath = request.getSession().getServletContext().getRealPath("/upload/temp");  
  26.         String result = "error";  
  27.           
  28.         File dir = new File(savePath);  
  29.         if (!dir.exists()) {  
  30.             dir.mkdirs();  
  31.         }  
  32.         List<File> files = getUploadFile();  
  33.         if((files!=null) && (files.size()>0)){  
  34.             for (int i = 0; i < files.size(); i++) {  
  35.                 FileUtils.copyFile(files.get(i), new File(savePath + "//"  
  36.                         + getUploadFileFileName().get(i)));  
  37.             }  
  38.         }  
  39.         else{  
  40.             return result;  
  41.         }  
  42.           
  43.         return SUCCESS;  
  44.     }  
  45.       
  46.       
  47.     /** 
  48.      * 显示图片 
  49.      * @return  
  50.      */  
  51.     public String getHotelImage(){  
  52.           
  53.          try {  
  54.              String name = request.getParameter("name");  
  55.   
  56.              name = request.getSession().getServletContext().getRealPath("/") +"upload/hotel/" + name;  
  57.              FileInputStream fi = new FileInputStream(name);  
  58.              BufferedInputStream bi = new BufferedInputStream(fi);  
  59.              int c =0;  
  60.              while((c=bi.read())!=-1){  
  61.                 response.getWriter().write(c);  
  62.              }  
  63.              response.getWriter().flush();  
  64.              fi.close();  
  65.         }   
  66.         catch (Exception e) {  
  67.             // TODO Auto-generated catch block  
  68.             e.printStackTrace();  
  69.         }  
  70.         return "success";  
  71.     }  
  72.       
  73.     /** 
  74.      * 持久化到数据库 
  75.      * @return  
  76.      */  
  77.     public String insertiImageInfo() {  
  78.           
  79.         try{  
  80.             hotelimage = new HotelImage();  
  81.             hotelimage.setImageTitle(imageTitle);  
  82.              String image = request.getParameter("uploadFileName");  
  83.              hotelimage.setImageName(request.getParameter("oldUploadFileName"));  
  84.              String savePath = request.getSession().getServletContext().getRealPath("/upload");  
  85.              try {  
  86.                     hotelimage.setImagePath(URLEncoder.encode( image,"utf-8"));  
  87.                     FileUtils.copyFile(new File(savePath+"/temp/" + hotelimage.getImagePath()), new File(savePath+"/hotel/" + hotelimage.getImagePath()));  
  88.                 } catch (Exception e) {  
  89.                     e.printStackTrace();  
  90.                 }  
  91.             HttpSession session = request.getSession();  
  92.             //验证用户是否已登录  
  93.             if(session.getAttribute("AdminSession")!=null){  
  94.                 UserInfo userInfo = (UserInfo) session.getAttribute("AdminSession");  
  95.                 if(userInfo!=null){  
  96.                     hotelimage.setUploadUserId(Integer.parseInt(userInfo.getId()));  
  97.                 }  
  98.             }  
  99.             else{  
  100.                 hotelimage.setUploadUserId(0);  
  101.             }  
  102.               
  103.             hotelimage.setHotelId(hotelId);  
  104.             hotelimage.setImageType(imageType);  
  105.             hotelimage.setImageDesc(content);  
  106.             int t = hotelImageService.addHotelImageService(hotelimage);  
  107.             if(t<=0){  
  108.                 return "error";  
  109.             }  
  110.             hotelId="0";  
  111.             imageType="0";  
  112.             imageTitle="";  
  113.         }  
  114.         catch(Exception ex){  
  115.             ex.printStackTrace();  
  116.             return "error";  
  117.         }  
  118.           
  119.           
  120.         return "success";  
  121.     }  
  122.       
  123.       
  124.       
  125.   
  126.       
  127.     public String goEditImage(){  
  128.         String id = request.getParameter("id");  
  129.         HotelImage image = hotelImageService.getImageByID(Long.valueOf(id));  
  130.         List<HotelInfo>  hotels = hotelImageService.queryHotelListService(hotelInfo);  
  131.         request.setAttribute("hotels", hotels);  
  132.         request.setAttribute("image", image);  
  133.         if("audit".equals(request.getParameter("type"))){  
  134.             return "audit";  
  135.         }  
  136.         return "success";  
  137.     }  
  138.       
  139.     public String editImage() {  
  140.         try{  
  141.             hotelimage = new HotelImage();  
  142.             hotelimage.setImageTitle(imageTitle);  
  143.             String image = request.getParameter("uploadFileName");  
  144.             hotelimage.setImageName(request.getParameter("oldUploadFileName"));  
  145.             String savePath = request.getSession().getServletContext().getRealPath("/upload");  
  146.             try {  
  147.                     hotelimage.setImagePath(URLEncoder.encode( image,"utf-8"));  
  148.                     if(!"0".equals(request.getParameter("upload"))){  
  149.                         FileUtils.copyFile(new File(savePath+"/temp/" + hotelimage.getImagePath()), new File(savePath+"/hotel/" + hotelimage.getImagePath()));  
  150.                     }  
  151.             } catch (Exception e) {  
  152.                     e.printStackTrace();  
  153.             }  
  154.             HttpSession session = request.getSession();  
  155.             //验证用户是否已登录  
  156.             if(session.getAttribute("AdminSession")!=null){  
  157.                 UserInfo userInfo = (UserInfo) session.getAttribute("AdminSession");  
  158.                 if(userInfo!=null){  
  159.                     hotelimage.setUploadUserId(Integer.parseInt(userInfo.getId()));  
  160.                 }  
  161.             }  
  162.             else{  
  163.                 hotelimage.setUploadUserId(0);  
  164.             }  
  165.               
  166.             hotelimage.setHotelId(hotelId);  
  167.             hotelimage.setImageType(imageType);  
  168.             hotelimage.setImageDesc(content);  
  169.             hotelimage.setImageId(imageId);  
  170.             int t = hotelImageService.updateImage(hotelimage);  
  171.             if(t<=0){  
  172.                 return "error";  
  173.             }  
  174.             hotelId="0";  
  175.             imageType="0";  
  176.             imageTitle="";  
  177.         }  
  178.         catch(Exception ex){  
  179.             ex.printStackTrace();  
  180.             return "error";  
  181.         }  
  182.           
  183.         return ActionSupport.SUCCESS;  
  184.     }  
  185.       
  186.       
  187.     public String auditImage() {  
  188.         HttpServletRequest request = ServletActionContext.getRequest();  
  189.         HttpSession session = request.getSession();  
  190.         UserInfo userInfo = (UserInfo) session.getAttribute("AdminSession");  
  191.         HotelImage image =new HotelImage();  
  192.         image.setImageId(imageId);  
  193.         image.setAudit_flag(request.getParameter("audit_flag"));  
  194.         image.setAudit_fault_reson(request.getParameter("audit_fault_reason"));  
  195.         image.setAudit_user(Integer.valueOf(userInfo.getId()));  
  196.         hotelImageService.auditImage(image);  
  197.         return ActionSupport.SUCCESS;  
  198.           
  199.     }  
  200.       
  201.     public String deleteImage(){  
  202.         HttpServletRequest request = ServletActionContext.getRequest();  
  203.         String [] ids = request.getParameterValues("checkedIds");  
  204.         String newsIds = StringUtils.join(ids, ",");  
  205.         hotelImageService.deleteImageByIds(newsIds);  
  206.         return "success";  
  207.     }  
  208.       
  209.     public String getGallerialImage(){  
  210.         HotelImage image = new HotelImage();  
  211.         image.setStartRow(1);  
  212.         image.setPageSize(20);  
  213.         String path = request.getContextPath() +"/upload/hotel/";  
  214.         List<HotelImage> list = hotelImageService.getImageList(image);  
  215.         List<GalleriaImage> galleriaImages  = new ArrayList<GalleriaImage>();  
  216.         GalleriaImage galleriaImage = null;  
  217.         for(int i=0;i<list.size();i++){  
  218.             galleriaImage = new GalleriaImage();  
  219.             image = list.get(i);  
  220.             galleriaImage.setDescription(image.getImageDesc());  
  221.             galleriaImage.setImage(path+image.getImagePath());  
  222.             galleriaImage.setTitle(image.getImageTitle());  
  223.             galleriaImages.add(galleriaImage);  
  224.         }  
  225.           
  226.         JSONArray jo = JSONArray.fromObject(galleriaImages);  
  227.         json = jo.toString();  
  228.         return "success";  
  229.     }  
  230. }  
  231.   
  232. //getter  setter略  

你可能感兴趣的:(文件上传显示保存到数据库实现类)