欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序?qū)崿F(xiàn)上傳照片代碼實(shí)例解析

 更新時間:2020年08月04日 15:18:10   作者:大專欄  
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)上傳照片代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

紙上談坑

在我實(shí)現(xiàn)了這個功能之前,我講講我是怎么在這個坑里爬上來的:

我實(shí)現(xiàn)上傳文件后端的接口的參數(shù)是String類型的

前臺傳的參數(shù):http://tmp/wx忽略很多字母數(shù)字.png

但由于這張是本地照片url(外網(wǎng)無法訪問),我后臺拿到的是一個String類型,是沒有辦法是去識別這是一張圖片的,訪問不了這個數(shù)據(jù),僅僅把它當(dāng)做字符串而已。(低級錯誤)

代碼實(shí)現(xiàn)

前言:后端接受文件有2種方式(參數(shù)): 1. MultipartFile 2.base64

微信上傳文件的開發(fā)文檔

小程序代碼

<!-- index.wxml -->
<view>
 <view>文件上傳</view>
 <view>
  <input id="file" type="file" bindtap="uploader"></input>
 </view>
</view>


// index.js
Page({
 data: {
  
 },
 uploader: function () {
  wx.chooseImage({
   count: 1,
   success: function(res) {
    let imgPath = res.tempFilePaths[0]
    wx.uploadFile({
     url: 'http://localhost:8080/customerRegister/uploadPricture',
     filePath: imgPath,
     name: 'files',
     success:res=>{
      console.log(res)
     }
    })
   } 
  })
 },
})

java后端代碼

@RequestMapping(value = "/customerRegister",produces = "application/json;charset=utf-8")
public class {

  @RequestMapping("/uploadPricture")
  @ResponseBody
  public String uploadPricture(@RequestParam("file") MultipartFile[] file) throws IOException {
    MultipartFile multipartFile = file[0];
    System.out.println("圖片名稱:"+multipartFile.getOriginalFilename());
    
    InputStream inputStream = multipartFile.getInputStream();
    return "{"mas":"ok"}";
  }

P.s. 注意:這是一個ssm項(xiàng)目,因此你需要在pom.xml中添加依賴和在springmvc.xml中添加以下代碼(這個問題搞了我?guī)讉€小時,因?yàn)樯倭松蟼魑募呐渲?,就會?dǎo)致multipartfile這個類失效)

 <!--pom.xml 文件上傳所需要的依賴-->
 <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
 </dependency> 
 <!--springmvc.xml-->
   <!-- SpringMVC上傳文件時,需要配置MultipartResolver處理器 -->
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <property name="defaultEncoding" value="UTF-8"></property>
     <!-- 指定所上傳的總大小不能超過1T。注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件 -->
     <property name="maxUploadSize" value="10485760000" />
     <property name="maxInMemorySize" value="40960" />
   </bean>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論