IOS 多檔案上傳 Java web端(後台) 使用List<MultipartFile> 接收出現的問題

來源:互聯網
上載者:User

標籤:androi   tde   mda   nss   format   except   time   mat   url   

先上正確的樣本:

  主要是設定我們的request的content-type為multipart/form-data

NSDictionary *param = @{@"assignee"     :self.userId,                            @"projectName"  :itemName.text,                            @"proceedingName":Name.text,                            @"content"      :content.text,                            @"urgency"      :string                            };    BaseNetwork *net = [BaseNetwork new];    [net.httpSessionManager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];    [net uploadImageRequestURL:[NSString stringWithFormat:@"%@act/proceeding/start", GW_Domain] imageArray:self.imageArray parameters:param progress:^(float progress) {

其次是,將我們擷取的imageArray(檔案集合)按照web端(後台)的key約定,組裝。【我們Java 背景檔案集合聲明的key名稱是files】

 

------至此,ios這邊的代碼就結束了。

 

Java後台:

  controller層

  /**   * 發起申請(app)   *    * @param proceeding   * @return   */  @ResponseBody  @RequestMapping("/start")  // @RequiresPermissions("act:proceeding:apply")  public R start(Proceeding proceeding) {    try {      // 啟動事項審批工作流程      proceedingService.appStartProcess(proceeding, getUser());      return R.ok();    } catch (BDException e) {      LOGGER.error("app端發起事項審批次程序異常", e);      return R.error(e.getMessage());    } catch (Exception e) {      LOGGER.error("app端發起事項審批次程序異常", e);      return R.error();    }  }
Proceeding 載體bean
import java.io.Serializable;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.springframework.web.multipart.MultipartFile;import com.shengshi.common.domain.UploadFileDO;import com.shengshi.common.exception.FileException;import com.shengshi.common.utils.Base64Utils;public class Proceeding implements Serializable {  private static final long serialVersionUID = 1L;  /**   * 主鍵UUID   */  private String id;  /**   * 事項審批申請人   */  private Long userId;  /**   * 項目名稱   */  private String projectName;  /**   * 事項名稱   */  private String proceedingName;  /**   * 事項內容   */  private String content;  /**   * 緩急程度 0:常規 1:緊急 2:特級   */  private String urgency;  /**   * 建立時間   */  private Date createTime;  /**   * 事項審批狀態 0:錄入 1:審核中 2:審核通過 3:審核不通過   */  private String status;  /**   * 事項審批次程序執行個體ID   */  private String procInstId;  /**   * 任務指定處理人   */  private String assignee;    /**   * 任務發起時間   */  private Date applyTime;  /**   * 上傳檔案清單,之所以沒有規範為multipartfile是在get方法做了另外封裝【要相容移動端和app】   */  private List<Object> files;  /**   * 關聯檔案,多個用逗號分隔   */  private String delIds;  /**   * 上傳檔案記錄列表   */  private List<UploadFileDO> uploadFileList;  public String getId() {    return id;  }  public void setId(String id) {    this.id = id;  }  public Long getUserId() {    return userId;  }  public void setUserId(Long userId) {    this.userId = userId;  }  public String getProjectName() {    return projectName;  }  public void setProjectName(String projectName) {    this.projectName = projectName;  }  public String getProceedingName() {    return proceedingName;  }  public void setProceedingName(String proceedingName) {    this.proceedingName = proceedingName;  }  public String getContent() {    return content;  }  public void setContent(String content) {    this.content = content;  }  public String getUrgency() {    return urgency;  }  public void setUrgency(String urgency) {    this.urgency = urgency;  }  public Date getCreateTime() {    return createTime;  }  public void setCreateTime(Date createTime) {    this.createTime = createTime;  }  public String getStatus() {    return status;  }  public void setStatus(String status) {    this.status = status;  }  public String getProcInstId() {    return procInstId;  }  public void setProcInstId(String procInstId) {    this.procInstId = procInstId;  }  public String getAssignee() {    return assignee;  }  public void setAssignee(String assignee) {    this.assignee = assignee;  }  public Date getApplyTime() {    return applyTime;  }  public void setApplyTime(Date applyTime) {    this.applyTime = applyTime;  }  public List<Object> getFiles() {    return files;  }  public void setFiles(List<Object> files) {    this.files = files;  } //之所以封裝這個方法,是因為我們需要相容app(直接form表單提交,檔案類型)和h5移動端(將檔案用base64 encode一下,以字串的形式傳輸)的檔案上傳  public List<MultipartFile> getFileList() throws FileException {    List<MultipartFile> list = new ArrayList<MultipartFile>();    if (files == null) {      return list;    }    for (Object obj : files) {      if (obj instanceof MultipartFile) {        list.add((MultipartFile) obj);      } else {// 移動app的from提交,檔案為base64字串        // 截取檔案字串 擷取檔案的類型和內容 base64檔案的形式        // “data:image/jpeg;base64,kjkjfkj4k5j43kj34j34kj534k5j43j34kj5k34j5534kj5534j4”        // 這裡再說明一下,如果將來對接android圖片上傳也採取base64方式的時候,        // 有可能解析檔案不全,因為Java後台預設會把“+”(加號)替換成“ ”空格,接收的時候先把空格全部替換成“+”        String base64Img = obj.toString();        list.add(Base64Utils.base64ConvertFile(base64Img));      }    }    return list;  }  public String getDelIds() {    return delIds;  }  public void setDelIds(String delIds) {    this.delIds = delIds;  }  public List<UploadFileDO> getUploadFileList() {    return uploadFileList;  }  public void setUploadFileList(List<UploadFileDO> uploadFileList) {    this.uploadFileList = uploadFileList;  }}

------正確樣本到此結束

我們出現的問題就是,ios端不小心將檔案參數多封裝了一層,如下:

NSDictionary *param = @{@"assignee"     :self.userId,                            @"projectName"  :itemName.text,                            @"proceedingName":Name.text,                            @"content"      :content.text,                            @"urgency"      :string,
                 @"files" :data
}; BaseNetwork *net = [BaseNetwork new]; [net.httpSessionManager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"]; [net uploadImageRequestURL:[NSString stringWithFormat:@"%@act/proceeding/start", GW_Domain] imageArray:self.imageArray parameters:param progress:^(float progress) {

就是這個多給的參數,【files】. 後台得到的等於是key:files value:formdata。   本來要的是檔案的格式,因為疏忽,導致後台一直按照字串解析,匹配不成檔案類型。記錄一下。【本文圖一已經對imageArray便利處理時加了相應的key,所以無需在form表單再去嵌套】

 

 

 

 

 

IOS 多檔案上傳 Java web端(後台) 使用List<MultipartFile> 接收出現的問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.