E企盈营销工具技术服务商 热线:4006-838-530

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

E企盈直播平台营销卖货系统

小程序代码:  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;        }}可以了~

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信小程序上传图片文件小程序+Java
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏