1. 程式人生 > >ssh框架中對檔案的上傳

ssh框架中對檔案的上傳

. 新增檔案上傳的選擇項
1. 客戶端三個注意事項
* method="post"
* enctype="multipart/form-data"
* <input type="file" name="myfile">

2. Struts2框架的使用攔截器完成了檔案上傳,並且底層使用也是FileUpload開源的元件。
* 提供 FileUpload 攔截器,用於解析 multipart/form-data 編碼格式請求,解析上傳檔案的內容 
* fileUpload攔截器 預設在 defaultStack 棧中, 預設會執行的 

* 在Action中編寫檔案上傳,需要定義三個屬性
> 檔案型別File ,屬性名與表單中file的name屬性名一致.
> 字串型別String , 屬性名:前段是name屬性名一致 + ContentType;
> 字串型別String , 屬性名:前段是name屬性名一致+FileName;

> 最後需要為上述的三個屬性提供set方法。

> 可以通過FileUtils提供 copyFile 進行檔案複製,將上傳檔案 儲存到伺服器端 

程式碼如下:

 /**
* 檔案的上傳,需要在CustomerAction類中定義成員的屬性,命名是有規則的!!
* private File upload;// 表示要上傳的檔案
* private String uploadFileName;表示是上傳檔案的名稱(沒有中文亂碼)
* private String uploadContentType;表示上傳檔案的MIME型別
* 提供set方法,攔截器就注入值了
*/

// 要上傳的檔案
private File upload;
// 檔案的名稱
private String uploadFileName;
// 檔案的MIME的型別
private String uploadContentType;

public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}


4. 檔案上傳中存在的問題
* 先配置input邏輯檢視
* 在頁面中顯示錯誤資訊

* 檔案上傳的總大小預設值是2M,如果超過了2M,程式會報出異常。可以使用<s:actionError>來檢視具體資訊!
> 解決總大小的設定,找到常量:
* struts.multipart.parser=jakarta-- 預設檔案上傳解析器,就是FileUpload元件
* struts.multipart.saveDir=-- 檔案上傳的臨時檔案儲存目錄
* struts.multipart.maxSize=2097152-- 檔案上傳的最大值(總大小),預設是2M

> 可以在struts.xml中設定常量,修改檔案上傳的預設總大小!!!
* <constant name="struts.multipart.maxSize" value="5000000"></constant>

5. 還可以通過配置攔截器來設定檔案上傳的一些屬性
* 先在<action>標籤中引入檔案上傳的攔截器
<interceptor-ref name="defaultStack">
<!-- 設定單個上傳檔案的大小 -->
<param name="fileUpload.maximumSize">2097152</param>
<!-- 設定副檔名 -->
<param name="fileUpload.allowedExtensions">.txt</param>

</interceptor-ref>

jsp程式碼:

<form action="${pageContext.request.contextPath}/restaurant_edit" method="post" enctype="multipart/form-data">
       
      <table>
        
        <tr>       
        <td> 店鋪圖片</td>
        <td><input type="file" name="upload"></td>
        </tr>
         <tr>
            <td>店鋪名稱</td>
            <td><input type="text" name="restaurant_name" value="${restaurant.restaurant_name }"></td>
          </tr> 
          <tr> 
            <td>店鋪關鍵詞</td> 
             <td><input type="text" name="keywords" value="${restaurant.keywords }"></td>                
           </tr>   
          <tr>
            <td>店鋪詳情</td>
            <td><input type="text" name="detail" value="${restaurant.detail }"></td>
          </tr>
          <tr>  
            <td>店鋪營業時間</td>
            <td><input type="text" name="business_time" value="${restaurant.business_time }"></td>
           </tr>
           <tr> 
            <td>店鋪電話</td>
             <td><input type="text" name="tel" value="${restaurant.tel }"></td>
           </tr>
           <tr> 
            <td>店鋪位置</td> 
             <td><input type="text" name="address" value="${restaurant.address }"></td>                
           </tr>
           <tr>
             <td><input type="submit" value="提交"></td>
           </tr>    
      </table>
  </form>

action中的程式碼:

 /**
* 檔案的上傳,需要在CustomerAction類中定義成員的屬性,命名是有規則的!!
* private File upload;// 表示要上傳的檔案
* private String uploadFileName;表示是上傳檔案的名稱(沒有中文亂碼)
* private String uploadContentType;表示上傳檔案的MIME型別
* 提供set方法,攔截器就注入值了
*/

// 要上傳的檔案
private File upload;
// 檔案的名稱
private String uploadFileName;
// 檔案的MIME的型別
private String uploadContentType;

public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
    public String edit() throws IOException{
    // 做檔案的上傳,說明使用者選擇了上傳的檔案了
    if(uploadFileName != null){
    // 列印
    System.out.println("檔案型別:"+uploadContentType);
    // 把檔案的名稱處理一下
    String uuidname = UploadUtils.getUUIDName(uploadFileName);
    // 把檔案上傳到D:\\apache-tomcat-7.0.52\\webapps\\upload
    String path=ServletActionContext.getServletContext().getRealPath("/business/image/");

                               (這裡我把圖片儲存在專案下的/business/image/目錄中,或者儲存在伺服器中,一般都會保持在圖片伺服器中。)
    //String path = "F:\\apache-tomcat-7.0.42\\webapps\\upload\\";
    // 建立file物件
    File file = new File(path+"//"+uuidname);
    // 簡單方式
    FileUtils.copyFile(upload, file);
    System.out.println(path+uuidname);
    //System.out.println("111"+restaurant);
    // 把上傳的檔案的路徑,儲存到客戶表中
    restaurant.setImg(uuidname);
    Business business=(Business) ServletActionContext.getRequest().getSession().getAttribute("exitBusiness");
    restaurant.setRestaurant_id(business.getBid());
    System.out.println("111"+restaurant);
    }
   
    // 修改客戶成功了
   
    restaurantService.update(restaurant);
    //查詢,返回展示頁面
    findById();
    return "store";
    }

service程式碼:

@Transactional
public void update(Restaurant restaurant) {
restaurantDao.update(restaurant);

}

dao  程式碼:

public void update(Restaurant restaurant) {
this.getHibernateTemplate().update(restaurant);
}

前端顯示介面:

 <div class="clearfix h44"></div>
 
 <div class="store_img" ng-click="func_showAction()">
 <img src="<c:url value='/business/image/${restaurant.img }'/>" alt="" id="store_img">
 <span>圖片</span>
 </div>
 <div class="store_name" ui-sref="storeName">
 <span class="store_name_tip">店鋪電話</span>
 <span class="store_name_show">${restaurant.tel }
  <i>&gt;</i>
 </span>
 </div>
 <div class="store_name" ui-sref="storeName">
 <span class="store_name_tip">店鋪營業時間</span>
 <span class="store_name_show">${restaurant.business_time }
  <i>&gt;</i>
 </span>
 </div>
 <div class="store_name" ui-sref="storeName">
 <span class="store_name_tip">店鋪詳情</span>
 <span class="store_name_show">${restaurant.detail }
  <i>&gt;</i>
 </span>
 </div>
 <div class="store_name" ui-sref="storeName">
 <span class="store_name_tip">店鋪名稱</span>
 <span class="store_name_show">${restaurant.restaurant_name }
  <i>&gt;</i>
 </span>
 </div>
 <div class="store_name" ui-sref="storeName">
 <span class="store_name_tip">店鋪關鍵詞</span>
 <span class="store_name_show">${restaurant.keywords }
  <i>&gt;</i>
 </span>
 </div>
 <div class="clearfix"></div>
 <div class="store_address" ui-sref="storeAddress">
 <span class="store_address_tip">店鋪位置</span>
 <span class="store_address_show">${restaurant.address }
  <i>&gt;</i>
 </span>
 </div>
   <input type="button" value="修改" onclick="edit()">
</ion-view>
</body>
 <script type="text/javascript">
function edit(){
window.location.href="${pageContext.request.contextPath}/restaurant_preEdit";
}
  </script>