struts2上传文件时把文件放到服务器真实路径下的webapps\upload下

以保存客户为例子

public String save() throws IOException{
        // 做文件的上传,说明用户选择了上传的文件了
        if(uploadFileName != null){
            // 打印
            System.out.println("文件类型:"+uploadContentType);
            // 把文件的名称处理一下
            String uuidname = UploadUtils.getUUIDName(uploadFileName);
            // 把文件上传到如 D:\\apache-tomcat-7.0.52\\webapps\\upload
            //获取应用的真实根路径
            String realPath = ServletActionContext.getServletContext().getRealPath("/");
            //获取webapps之前的字符串
            realPath=realPath.substring(0, realPath.indexOf("webapps"));
            //创建带文件名的真实路径
            String path = realPath+"webapps\\upload\\"+uuidname;
            
            // 创建file对象
            File file = new File(path);
            
            // 简单方式
            FileUtils.copyFile(upload, file);
            
            // 把上传的文件的路径,保存到客户表中
            customer.setFilepath(path);
        }
        
        // 保存客户成功了
        customerService.save(customer);
        return "save";
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值