微信小程序> 微信小程序上传图片文件小程序+Java

微信小程序上传图片文件小程序+Java

浏览量:731 时间: 来源:彭珂个人网

小程序代码:

  chooseImage(){    wx.chooseImage({      success: function (res) {        var tempFilePaths = res.tempFilePaths        wx.uploadFile({          url: 'http://127.0.0.1:8080/xxxx', // 后台上传文件url           filePath: tempFilePaths[0],          name: 'file',          formData: {            'user': 'test' //需要传入的其它参数给后台          },          success: function (res) {            var data = res.data            //do something          },fail:function(err){            console.log(err)          }        })      }    })  }

Java代码:

package com.core.controller;import com.core.entity.Massage;import net.sf.json.JSONObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;/** * @ClassName WxUploadController * @Description 微信文件上传 * @Author PengKe * @Wx VIP694046220 * @Date 2018/9/19 0019 20:45 * @Version 1.0 **/@Controller@RequestMapping(value = "wxUpload")public class WxUploadController {    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);    @RequestMapping("/upload")        public ModelAndView uploadImage(HttpServletRequest request, HttpServletResponse response) throws IOException {            System.out.println("文件上传!");            MultipartHttpServletRequest req =(MultipartHttpServletRequest)request;            PrintWriter out = response.getWriter();        Massage massage = new Massage();            MultipartFile multipartFile =  req.getFile("file");            String uId = request.getParameter("uId");//这就是获取微信传过来的其它参数            String imgType = request.getParameter("imgType");            System.out.println(uId+"---"+imgType);            String realPath = request.getRealPath("/upload") + "/";            try {                File dir = new File(realPath);                if (!dir.exists()) {                    dir.mkdir();                }                File file  =  new File(realPath,uId+"_"+imgType+".jpg");// 文件保存位置和文件名                multipartFile.transferTo(file);                massage.setCode(1);                massage.setDatas("");                massage.setMsg("上传成功");            } catch (IOException e) {                massage.setCode(0);                massage.setDatas(e);                massage.setMsg("上传失败");                e.printStackTrace();            } catch (IllegalStateException e) {                massage.setCode(0);                massage.setDatas(e);                massage.setMsg("上传失败!");                e.printStackTrace();            }        JSONObject jsonObject = JSONObject.fromObject(massage);        out.print(jsonObject.toString());        out.close();            return null;        }}

可以了~

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎