java 微信小程序文件上传ssh

public class CoachuploadAction extends ActionSupport implements ServletResponseAware {
    private HttpServletResponse response;

    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }

    private File file;//跟form表单中的名字相对应

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String coachupload() throws IOException {
        System.out.println("文件上传---");
        String fileName = file.getName();
        String webPath = this.getClass().getResource("/").getPath().replaceAll("^\\/", "");
        String[] strArr = webPath.split("\\/");
        System.out.println(strArr.length); //这里输出3
        String str2 = "";
        for (int i = 0; i < strArr.length; ++i){
            if (i == 0) {
                str2 =  strArr[i];
                System.out.println(i+"i:"+str2);//这里输出a b c
            }
            if (i >= 1) {
                str2 = str2+"/"+strArr[i] ;
                System.out.println(i+"i:"+str2);//这里输出a b c
            }
            if (i==4){
                break;
            }
        }
        String savaPath = str2 + "/boxlogin/nuboxsj/idcard";
        String realpath =  savaPath.replace("%20"," ");
        System.out.println("存储地址为:"+savaPath);
        //文件保存地址和原文件名建立上传文件输出流
        if (fileName != null) {
            File file = new File(realpath);
            if (!file.exists()) {
                file.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(realpath + "\\" + fileName);
            FileInputStream fis = new FileInputStream(getFile());
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            response.setContentType("text/json;charset=UTF-8");
            ServletActionContext.getResponse().getWriter().print("上传成功");
        } else {
            response.setContentType("text/json;charset=UTF-8");
            ServletActionContext.getResponse().getWriter().print("没接收到文件");
        }
        return null;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值