EasyUI+SpringMVC 單個/多個文件上傳


 

關於文件上傳和下載網上有很多相關教程,但針對入門的新人來說大部分都不夠詳細,而且由於一直換工作的原因(主要是因為自己的懶惰)把這篇整理拖延了很久,終於在今天打算將它完成了……(此處應有雞蛋)

 

首先說要要完成的事情:

基於EasyUi+SSM框架完成文件上傳,主要實用了easyui-filebox上傳按鈕;

准備工具:
環境:
  Eclipse 4.3
  jdk1.7.0_72
  apache-maven-3.0.4
  apache-tomcat-7.0.53
  spring-mvc-4.0
  spring-beans-4.0
  mybatis-3.2.8
  oracle 11.2.0.3.0

jar包:
  commons-io-2.4
  commons-fileupload-1.3

基本步驟:

  按照數據流向來闡述:

    >>> 頁面提供文件上傳控件;
    >>> contorller端接收頁面傳入的文件流,在這個步驟可以寫入數據庫或者放到指定目錄;
    >>> 將上傳的返回結果寫回頁面;

代碼部分:

#jsp.js.jquery
 1 function uplodad(
 2     $('#fromNameId').form({    
 3                 url:'controllerurl',    
 4                 onSubmit: function(){
 5                     //uploda before something
 6                 },    
 7                 success:function(data){    
 8                     //upload after something
 9                 },error:function(date){
10                     $.messager.alert(data.errormsg);
11                 }  
12             });    
13             $('#fromNameId').submit();
14 );

#jsp.html

1 <form id="rundApplyFrom" method="post" enctype="multipart/form-data"> 
2     <tr id="trIdCard"> 
3         <td>msg:</td> 
4         <td><input class="easyui-filebox" id="uploadId" name="sourceFile" style="width:200px"></td>
5     </tr>
6 </from>
7 
8 <!-- 這里的name屬性需要和controller方法中用於接收上傳文件Bean中的byte[]字段名字相同 -->

這里使用了EasyUi自帶的filbox控件,在網上找了很多文件上傳的資料都說要實用第三方插件什么的,但對於還是新人的我來說看着都一個頭倆個個大;

於是看了一下相關資料和文檔發現了這個東西;

使用步驟很簡單:

  1.在html/jsp頁面做如上配置,一個用於上傳文件的選擇框;

  2.通過ajax方式,使用jquery的標簽選擇器將form區域的id作為提交標記發起ajax訪問controller中的upload方法(js部分代碼);

在controller中的upload方法形參可以接收一個pojo,內部需要有一個MultipartFile屬性命名和form域中對應的name相同,這個例子中我使用的是sourceFile(見上);

通過文章最開始提到的jar包自帶的功能和SpringMVC的配置,可以將這里頁面用戶選擇的文件自動存入pojo中對應的字段中(即sourceFile);

 

# springMVC.xml 配置

  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="defaultEncoding" value="UTF-8"/>  
  </bean> 

 

# pojo.java

public class AReFundApplyProcess implements Serializable {

       private String prosn  ; 
       private String applyid; 
       private byte[] annex  ; 
       private Object content;
       private MultipartFile[] sourceFile;

// sourceFile geter, seter

在這個pojo中我用於接收文件的MultipartFile字段用的是一個數組,因為在案例中是多文件上傳;

在頁面上file-box中的文件會通過springMVC存入這個pojo的MultipartFile字段中,對於多文件來講通過獲取角標即可獲取到對應的文件;

 

#serviceImpl.java

  /**
     * uplodaApplyFile
     * 方法描述:文件上傳方法;
     * @param request
     */
    public void uplodaApplyFile(
            AReFundApplyProcess arProcess, 
            AReFundApply aRefundApply){
        
        MultipartFile[] uploadfile=arProcess.getSourceFile();//這里用於獲取前台傳入Bean中Byte字段中的流;
        InputStream fileIs= null;                            //因為案例中是多文件上傳,所以是數組;
        aRefundApply.setOrderno(arProcess.getProsn());
        
        try {
            if(uploadfile != null&& uploadfile.length>0){
                for (int i = 0; i < uploadfile.length; i++) {
                    MultipartFile file=uploadfile[i];
                    fileIs= file.getInputStream();
                    byte[] b = FileCopyUtils.copyToByteArray(fileIs);
                    if(b.length>0){
                        arProcess.setAnnex(b);
                        aReFundApplyCheckService.insertAReFundApplyProcess(arProcess);
                    }
                }
            }
        } catch (IOException e) {
            log.error("上傳文件異常...",e);
        }
    }

這里可以看到,MultipartFile類可以有方法獲取到一個Inputstrem,這樣再將其轉換為byte數組即可存入數據庫中的BLOB字段了;

順帶一提關於BLOB字段存入Oracle數據庫中的問題,經過驗證,Mybatis支持直接將byte數組作為BLOB字段存入數據庫;只需要在Mapper中指定jdbcType=BLOB即可;

 

參考資料:

http://blog.csdn.net/songanling/article/details/38951013


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM