struts2文件和圖片上傳-最完整的

  1. 準備:導入commons-io-2.0.1.jar和commons-fileupload-1.2.1.jar,版本可以不一樣  
  2.   
  3.   
  4. 前臺:  
  5.       <struts:form action="weiboAction!uploadPicture.shtml" enctype="multipart/form-data" namespace="/" method="post">  
  6.         <struts:file name="image" label="文件"></struts:file>  
  7.         <struts:submit value="上傳"/>  
  8.     </struts:form>  
  9.   
  10.   
  11.   
  12.   
  13. 後臺:  
  14.     /** 
  15.      * 作者:劉鵬 
  16.      * 時間:2013-07-07 
  17.      * 描述:微博列表中的圖片和文件上傳顯示 
  18.      * @return 
  19.      */  
  20.       
  21. /*****************以下爲上傳部分*******************************/  
  22.     private File image;                        //得到上傳的文件  
  23.     private String imageFileName;              //得到文件的名稱,寫法是固定的  
  24.     private String imageContentType;           //得到文件的類型  
  25.   
  26.   
  27.     public String getImageContentType() {  
  28.         return imageContentType;  
  29.     }  
  30.     public void setImageContentType(String imageContentType) {  
  31.         this.imageContentType = imageContentType;  
  32.     }  
  33.     public String getImageFileName() {  
  34.         return imageFileName;  
  35.     }  
  36.     public void setImageFileName(String imageFileName) {  
  37.         this.imageFileName = imageFileName;  
  38.     }  
  39.     public File getImage() {  
  40.         return image;  
  41.     }   
  42.     public void setImage(File image) {  
  43.         this.image = image;  
  44.     }  
  45.     public String addUI(){  
  46.         return SUCCESS;  
  47.     }  
  48.     public String uploadPicture(){  
  49.         Weibo model = getModel();  
  50.         Date date = new Date();  
  51.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  52.         String dateString = simpleDateFormat.format(date);  
  53.         model.setTime(dateString);  
  54.         HttpServletRequest request = ServletActionContext.getRequest() ;  
  55.         HttpServletResponse response = ServletActionContext.getResponse();  
  56.           
  57.         //以下爲圖片上傳部分  
  58.         //保存到根目錄下的uploadImages文件夾下  
  59.         String realPath = ServletActionContext.getServletContext().getRealPath("/uploadImages");    //取得真實路徑  
  60.         System.out.println(realPath);  
  61.         System.out.println(imageFileName);  
  62.         System.out.println(imageContentType);  
  63.           
  64.         //自動命名  
  65.     if(imageFileName!=null && imageFileName.length()!=0){  
  66.         Random random = new Random(99999);  
  67.         int tempInt = random.nextInt();  
  68.         Date datenew = new Date();  
  69.         SimpleDateFormat simpleDateFormatnew = new SimpleDateFormat("yyyyMMddhhmmss");  
  70.         int last = imageFileName.lastIndexOf(".");  
  71.         String head = imageFileName.substring(0,last);  
  72.         String type = imageFileName.substring(last);  
  73.         imageFileName = simpleDateFormatnew.format(datenew) + tempInt + type;  
  74.         System.out.println("新的文件名稱是:"+imageFileName);  
  75.           
  76.         //創建父文件夾  
  77.         if(image!=null){  
  78.         File saveFile = new File(new File(realPath), imageFileName);  
  79.         if(!saveFile.getParentFile().exists()){     //如果Images文件夾不存在  
  80.             saveFile.getParentFile().mkdirs();      //則創建新的多級文件夾  
  81.               
  82.         }  
  83.         try {  
  84.             FileUtils.copyFile(image, saveFile);     //保存文件  
  85.             ActionContext.getContext().put("message""上傳成功!");  
  86.               
  87.             String path = request.getContextPath();  
  88.             String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  89.             String picturePath = basePath +"uploadImages"+"/"+imageFileName;  
  90.             model.setImage(picturePath);  
  91.               
  92.             //request.setAttribute("uploadsuccess", imageFileName);  
  93.         } catch (IOException e) {  
  94.               
  95.             e.printStackTrace();  
  96.         }  
  97.         }  
  98.         }else{  
  99.             model.setImage("");  
  100.         }  
  101.       
  102.         //增加微博的時候將當前登錄人對應的頭像信息路徑加入到微博表中(此時model中已經存在userName)  
  103.         String picturePathString = getiWeiboService().selectPicturePath(model);  
  104.         model.setAvatar(picturePathString);  
  105.         getiWeiboService().insertWeibo(model);  
  106.           
  107.         try {  
  108.             response.sendRedirect("jsp/weibo/index.jsp");  
  109.         } catch (IOException e) {  
  110.             e.printStackTrace();  
  111.         }  
  112.             }  
  113.       
  114.       
  115. /*****************以上爲上傳部分*******************************/  
  116.   
  117.   
  118. 後臺將圖片的地址保存到數據庫中  
  119.     //先從數據庫中將所有數據讀出來,放入到request中  
  120.     request.setAttribute("weibotest", list);  
  121.   
  122.   
  123. 前臺使用OGNL語言讀取出圖片地址,並且顯示圖片  
  124.     <s:iterator value="#request.weibotest" var="user">  
  125.         <s:property value="#user.getContent()"/>  
  126.         <img src ='<s:property value ="#user.getImage()" />' width="200">     //顯示圖片  
  127.     </s:iterator> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章