struts2 上传文件

项目需求上传图片

由于之前项目都是用spring  mvc框架  用MultipartFile来上传图片

今日遇到struts2 框架还棘手了一把 ,不过不的说strut2 上传文件真的是麻烦

首先配置xml

struts.xml 配置上传文件

    <constant name="struts.multipart.saveDir" value="C:/InfoPath/tmp"></constant>//  此处路径是临时文件存放

//  struts    上传文件 生成的是临时文件 需要获取这个临时文件夹中的临时文件在通过流的方式写入到  你要存的盘符中
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.action.extension" value="action"></constant>
    <constant name="struts.configuration.xml.reload" value="true" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.custom.i18n.resources" value="app,message"></constant>
    <constant name="struts.multipart.maxSize" value="1000485760"/>


代码

首先是表单

  重点是name  接下来再看 后台代码

      <div class="form-group">
                <label id="labelSpecial" for="textSpecial" class="col-sm-2 control-label">缩略图1:</label>
                <div>
                    <input type="file" maxlength="500" id="imgFile1" name="pic1" οnchange="getPhotoSize(this)"/>
                    <span style="padding:0 0 0 5px;"><b class="redColor">*</b></span>
                </div>
              </div>
              
              <div class="form-group">
                <label id="labelSpecial" for="textSpecial" class="col-sm-2 control-label">缩略图2:</label>
                <div>
                    <input type="file" maxlength="500" id="imgFile2" name="pic2" οnchange="getPhotoSize(this)"/>
                </div>
              </div>
              
              <div class="form-group">
                <label id="labelSpecial" for="textSpecial" class="col-sm-2 control-label">缩略图3:</label>
                <div>
                    <input type="file" maxlength="500" id="imgFile3" name="pic3" οnchange="getPhotoSize(this)"/>
                </div>
              </div>
              
              <div class="form-group">
                <label id="labelSpecial" for="textSpecial" class="col-sm-2 control-label">缩略图4:</label>
                <div>
                    <input type="file" maxlength="500" id="imgFile4" name="pic4" οnchange="getPhotoSize(this)" />
                </div>
              </div>
           

后台代码

//  此处变量名 和表单中 name 对应起来

    private  File pic1;
    private  File pic2;
    private  File pic3;
    private  File pic4;
    //    xxxxFileName   固定格式
    private String pic1FileName;
    private String pic2FileName;
    private String pic3FileName;
    private String pic4FileName;
    //  文件类型  可不写
    private String pic1ContentType;

//  当然以上变量都需要setget方法 不多说

一下才是真正上传文件的代码

    if (pic1 != null && !"".equals(pic1)) {
                fileName1 = WebUtils.getRandomId()+ pic1FileName.substring(pic1FileName.length() - 4,pic1FileName.length());

// root  为路径

                String result = uploadFile(root, pic1, fileName1);
                if (result.equals("success")) {
                    String image1 = contextPath + Constant.img + fileName1;
                    // 删除原图
                    if (organizations.getPic_url()!=null&&!"".equals(organizations.getPic_url())) {
                        String str = organizations.getPic_url().substring(organizations.getPic_url().lastIndexOf("/"));
                        String path = realPath+str;
                        File oldfile = new File(path);
                        oldfile.delete();
                    }
                    organizations.setPic_url(image1);
                    
                    pic1 = null;
                }


public   String uploadFile(String root ,File thumbanil,String fileName) throws IOException{
        
        //  上传文件方法
        FileInputStream fiptS = new FileInputStream(thumbanil);
        InputStream iptS = (InputStream) fiptS;
        FileOutputStream foptS = new FileOutputStream(new File(root, fileName));
        OutputStream optS = (OutputStream) foptS;
        int c;
        while((c=iptS.read())!=-1)
        {
            optS.write(c);
        }
        optS.flush();
        optS.close();
        
        return "success";
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值