java 实现多文件动态上传

多文件可编辑上传
<form action="../reportset/add.action" name="queryForm"  enctype="multipart/form-data" method="post"   >
 <input type="button"  οnclick="addFileInput()" value="添加文件行"></input>
<table>
    <thead>
    <tr style="background-color:#EFEFEF">
        <th width="30%" colspan="1"  nowrap="true" rowspan="1"><strong>待上传文件名</strong></th>
        <th width="20%" colspan="1"  rowspan="1"><strong>开放下载</strong></th>
        <th width="20%" colspan="1"  rowspan="1"><strong>删除</strong></th>
    </tr>
    </thead>
    <tbody class="filecontent" id="filecontent">
        <tr>
            <td><input type="file" name="file" accept="*.jpg,*.png,*.gif,*.pdf,*.doc,*.docx,*.xls,*.xlsx,*.tar.gz,*.zip,*.rar"></td>
            <td><input type="checkbox" name="downloads" fileid="1"  οnclick="setUpfileValue(this)" value="0"/>
                <input type="hidden" id="1upfile" name="downloads" value="0"/></td>
            <td><a οnclick="deleteTr(this)">删除</a></td>
        </tr>
    </tbody>
</table>

 <input type="hidden" id="upfileId"  name="upfileId" value="1"/>
</form>

js
function addFileInput(){
    var upfileId = $('input[name="upfileId"]').val();
    $('input[name="upfileId"]').val(upfileId+1);
    var html = '';
        html += "<tr>" +
                "<td><input type='file' name='file'   accept=''></td>"+
                "<td><input type=\"checkbox\" name=\"downloads\" fileid=\""+upfileId+"\"  οnclick=\"setUpfileValue(this)\" value=\"0\"/>\n" +
                     "<input type=\"hidden\" id=\""+upfileId+"upfile\" name=\"downloads\" value=\"0\"/></td>" +
                "<td><a οnclick=\"deleteTr(this)\">删除</a></td>" +
                "</tr>";
    $('.filecontent').append(html);
}

function deleteTr(obj){
    $(obj).parent().parent().remove();
}

function setUpfileValue(obj){
    var fileid = $(obj).attr('fileid');
    var className= fileid+'upfile';
    document.getElementById(className).value  =obj.checked?1:0;
}

    @RequestMapping(value="/reportset/add.action")
    public String add(ReportsetDto dto,  @RequestParam("file") List<CommonsMultipartFile> files,  Integer[] downloads,String organid ){
        for(int i=0 ;i<files.size();i++){
            reportsetService.saveFile(dto,files.get(i),downloads[i]);
        }
        return "reportset/reportset-reload";
    }

    public boolean saveFile(ReportsetDto dto, CommonsMultipartFile file, Integer download){
        if(file == null) {
            return false;
        }
        // 先创建保存文件的文件夹
        File reportsetPath = new File("/Users/mazhen/desktop/reportpath/"+dto.getId());
        // 判断文件夹已经存在
        if(!reportsetPath.exists()){
            boolean mkdir = reportsetPath.mkdir();
            logger.info("路径不存在!"+reportsetPath.getAbsolutePath() + (mkdir==true?"成功":"失败"));
        }
        String fileNameWithPath = file.getFileItem().getName();
        String fileName = fileNameWithPath.substring(fileNameWithPath.lastIndexOf(File.separator)+1);
        File newFile=new File(this.getStorepath(dto.getId(), fileName));
        // 创建文件
        if(!newFile.exists()&&!newFile.isDirectory()){
            boolean mkdir = newFile.mkdir();
            logger.info("文件不存在!"+newFile.getAbsolutePath() + (mkdir==true?"成功":"失败"));
        }
        //通过CommonsMultipartFile的方法直接写文件
        try {
            // 文件上传
            file.transferTo(newFile);
        } catch (IOException e) {
            logger.error("文件上传失败",e);
            // TODO 失败处理
            return false;
        }
        return true;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值