java后台接收小程序上传图片 好用~

用了半天时间才脱坑,用的struts2框架,因此纠结了很长很长时间。。。。因为网上找不到struts2框架接收小程序所传图片的教程,自己搞了一个,亲测好用,废话不说粘代码。。。。

package com.jeanLeung.gogo.controller.auth;

import com.jeanLeung.gogo.model.AdviseUser;
import com.jeanLeung.gogo.service.AdviseUserService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.StrutsUploadedFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;


public class Sign extends ActionSupport {

    private HttpServletRequest request = ServletActionContext.getRequest();
    private StrutsUploadedFile userInfo;
   

    @Override
    public String execute() throws IOException {
        System.out.println("userInfo:" + userInfo);

       
            System.out.println(userInfo.isFile());

            String realPath = "F:/image";
            try {
                File dir = new File(realPath);
                if (!dir.exists()) {
                    dir.mkdir();
                }
                File file  =  new File(realPath,idCard + ".jpg");
                InputStream inputStream = new FileInputStream(userInfo.getContent());
                OutputStream outputStream = new FileOutputStream(file);
                byte[] b=new byte[1024];
                //一边读一边写
                while(inputStream.read(b)!=-1) {
                    outputStream.write(b);
                }
                //关闭流
                inputStream.close();
                outputStream.close();
                System.out.println("文件copy成功");

            } catch (IOException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }


        return NONE;
    }
    
        public StrutsUploadedFile getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(StrutsUploadedFile userInfo) {
        this.userInfo = userInfo;
    }
   
}

思想其实很简单,就是用private StrutsUploadedFile userInfo;来接收微信小程序传来的图片,哦对了,我的微信小程序前端代码:

wx.uploadFile({
          url: `${app.globalData.subDomain}/Frank/Auth/sign?id=${+index}`, // +index 把index 转换成number格式
          filePath: filePaths[index],
          name: 'userInfo',
          header: {
            'content-type': 'multipart/form-data'
          }, // 设置请求的 header
          formData: info, // HTTP 请求中其他额外的 form data
          success: function (res) {
            console.log('信息上传成功', res)
            wx.showToast({
              title: '信息上传成功!',
              icon: 'success',
              duration: 0
            })
            wx.navigateTo({
              url: "/pages/payment/payment"
            })
          }
        })  

可以看出我微信小程序上传的那张图片的名字是userInfo,所以我们定义变量StrutsUploadedFile userInfo以及该变量的set方法来让struts2框架帮我们自动设置进去,然后就简单了,吧userInfo转成File,再用输入流输出流写到事先定义好的文件里,转为图片。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

请保持优秀。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值