• 上传


    https://www.cnblogs.com/xdp-gacl/p/4200090.html

    <form  enctype="">

       <input type=file name=file><button value="上传">

    </form>

    @RequiresPermissions("dp:dpMaterials:edit")
    @RequestMapping(value = "save")
    public String save(HttpServletRequest request,DpMaterials dpMaterials, String[] library, String[] name,
    @RequestParam(value = "file", required = false) MultipartFile[] file,
    Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, dpMaterials)) {
    return form(dpMaterials, model);
    }
    String id = dpMaterials.getId();
    if (id == null || "".equals(id)) {
    //设置id
    dpMaterials.setIsNewRecord(true);
    id = IdGen.uuid();
    dpMaterials.setId(id);
    dpMaterials.setCreateDate(new Date());
    }
    /**
    * 商家信息
    */
    DpInfo dpInfo = dpInfoService.get(dpMaterials.getMid());
    dpMaterials.setMerchantAccount(dpInfo.getAccount());
    dpMaterials.setName(dpInfo.getName());
    dpMaterials.setIndustry(dpInfo.getIndustry());
    dpMaterials.setGrade(dpInfo.getGrade());

    dpMaterialsService.save(dpMaterials);

    /**
    * 材料库详情
    */
    if (library != null && library.length > 0) {
    for (int i = 0; i < library.length; i++) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    String timeStr = simpleDateFormat.format(new Date());
    String path = request.getSession().getServletContext().getRealPath("/") + "/resources/upload/dpMaterials/" + timeStr + "/";
    String spare="/resources/upload/dpMaterials/" + timeStr + "/";
    MultipartFile f= file[i];
    String myFileName = f.getOriginalFilename();
    //如果名称不为“”,说明该文件存在,否则说明该文件不存在
    if (myFileName.trim() != "") {
    //重命名上传后的文件名
    //定义上传路径
    File localFile = new File(path + myFileName);
    if (!localFile.exists()) {
    localFile.mkdirs();
    }
    try {
    f.transferTo(localFile);
    } catch (IOException e) {
    e.printStackTrace();
    }
    DpMaterialsDetails dpMaterialsDetails = new DpMaterialsDetails();
    dpMaterialsDetails.setMid(id);
    dpMaterialsDetails.setLibrary(library[i]);
    dpMaterialsDetails.setName(name[i]);
    dpMaterialsDetails.setSpare1(spare);
    dpMaterialsDetails.setStorageDate(new Date());
    User user = UserUtils.getUser();
    dpMaterialsDetails.setGradeId(user.getId());
    dpMaterialsDetails.setGradeName(user.getName());
    dpMaterialsDetailsService.save(dpMaterialsDetails);
    }
    }
    }
    addMessage(redirectAttributes, "保存商家材料库管理成功");
    return "redirect:" + Global.getAdminPath() + "/dp/dpMaterials/?repage";
    }

    <form id="saveForm" class="layui-form" action="${basePath}/lab/project/endProject" method="post"
    enctype="multipart/form-data">
    <div class="layui-form-item">
    <label class="layui-form-label">结题时间</label>
    <div class="layui-input-inline">
    <input type="hidden" value="${labProject.projectapplId}" name="projectapplId" id="projectapplId">
    <input type="text" class="layui-input " id="endTime" name="endTime"
    value="<fmt:formatDate value="${labProject.endTime}" pattern="yyyy-MM-dd"/>"
    lay-verify="required|date">
    </div>
    </div>

    <div class="layui-form-item">
    <label class="layui-form-label">是否归档</label>
    <div class="layui-input-inline">
    <select class="newsLook" lay-filter="warning" name="warning" id="warning">
    <option value="0">是</option>
    <option value="1">否</option>
    </select>
    </div>
    </div>
    <div class="layui-upload">
    <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>
    <input class="layui-upload-file" type="file" name="file" multiple="">
    <div class="layui-upload-list">
    <table class="layui-table">
    <thead>
    <tr>
    <th>文件名</th>
    <th>地址</th>
    <%--<th>大小</th>--%>
    <th>状态</th>
    <th>操作</th>
    </tr>
    </thead>
    <tbody id="demoList">

    </tbody>
    </table>
    </div>
    <button type="button" class="layui-btn" id="testListAction">开始上传</button>
    </div>
    <div class="layui-form-item">
    <div class="layui-input-block">
    <button type="button" class="layui-btn" lay-submit lay-filter="saveForm">保存</button>
    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    <button type="reset" class="layui-btn layui-btn-primary" onclick="addTabTr()">添加行</button>
    </div>
    </div>
    </form>
    <jsp:include page="/resources/inc/footer.jsp" flush="true"/>
    <script type="text/javascript">
    $(function () {
    //日期控件
    laydate.render({
    elem: '#endTime'
    });
    //
    form.on("submit(saveForm)", function () {
    saveForm();
    });
    form.on('select(status)', function (data) {
    if (data.value == 1) {//得到被选中的值
    $("#showDiv").show();
    } else {
    $("#showDiv").hide();
    }
    });

    layui.use('upload', function () {
    var upload = layui.upload;
    //多文件列表示例
    var demoListView = $('#demoList')
    ,uploadListIns = upload.render({
    elem: '#testList'
    ,url: '${basePath}/lab/project/upload'
    ,accept: 'file'
    ,multiple: true
    ,auto: false
    ,bindAction: '#testListAction'
    ,choose: function(obj){
    var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
    //读取本地文件
    obj.preview(function(index, file, result){
    var tr = $(['<tr id="upload-'+ index +'">'
    ,'<td><input type="text" class="layui-input newsAuthor" lay-verify="required" name="names" id="names"> </td>'
    ,'<td>'+ file.name +'<input type="hidden" name="url" id="url-'+ index +'"></td>'
    // ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
    ,'<td>等待上传</td>'
    ,'<td>'
    ,'<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
    ,'<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">删除</button>'
    ,'</td>'
    ,'</tr>'].join(''));

    //单个重传
    tr.find('.demo-reload').on('click', function(){
    obj.upload(index, file);
    });

    //删除
    tr.find('.demo-delete').on('click', function(){
    delete files[index]; //删除对应的文件
    tr.remove();
    uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选
    });

    demoListView.append(tr);
    });
    }
    ,done: function(res, index, upload){
    if(res.code == 0){ //上传成功
    var tr = demoListView.find('tr#upload-'+ index)
    ,tds = tr.children();
    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
    tds.eq(3).html(''); //清空操作
    demoListView.find('input#url-'+ index).val(res.src);
    return delete this.files[index]; //删除文件队列已经上传成功的文件
    }
    this.error(index, upload);
    }
    ,error: function(index, upload){
    var tr = demoListView.find('tr#upload-'+ index)
    ,tds = tr.children();
    tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
    tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
    }
    });
    });
    });


    @RequestMapping("/upload")
    @ResponseBody
    public JSONObject upload(HttpServletRequest request,
    @RequestParam(value = "file", required = false) MultipartFile file[], HttpServletResponse response) throws Exception {
    JSONObject jsonObject = new JSONObject();
    try {
    for (int i = 0; i < file.length; i++) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    String timeStr = simpleDateFormat.format(new Date());
    String path = request.getSession().getServletContext().getRealPath("/") + "/resources/upload/project/" + timeStr + "/";
    MultipartFile file1 = file[i];
    String myFileName = file1.getOriginalFilename();

    //如果名称不为“”,说明该文件存在,否则说明该文件不存在
    if (myFileName.trim() != "") {
    //重命名上传后的文件名
    //定义上传路径
    File localFile = new File(path + myFileName);
    if (!localFile.exists()) {
    localFile.mkdirs();
    }
    file1.transferTo(localFile);
    jsonObject.put("code", "0");
    jsonObject.put("msg", "SUCCESS");
    jsonObject.put("src", timeStr + "/" + myFileName);
    jsonObject.put("name", myFileName);
    }
    }


    } catch (Exception e) {
    e.printStackTrace();
    jsonObject.put("code", "1");
    jsonObject.put("msg", e.getLocalizedMessage());
    }
    return jsonObject;
    }

  • 相关阅读:
    C#修饰符
    Exploring jQuery .live() and .die()
    ASP.NET Session丢失问题原因及解决方案
    5个 PHP 安全措施(转)
    jQuery方法click() bind() live() delegate()区别
    高性能网站的十四条黄金法则(雅虎14条)
    计算机科学概论读书笔记系列——绪论
    进程、线程、应用程序域、托管代码、元数据等。
    互联网协议入门(转)
    js跨域访问问题
  • 原文地址:https://www.cnblogs.com/gtbky/p/8124729.html
Copyright © 2020-2023  润新知