zoukankan      html  css  js  c++  java
  • ajax+MultipartFile上传文件到本地

    .html

    <th data-options="field:'op',align:'center',220,formatter:rmtManager.supportOpFormmater()">操作</th>
    <div id="uploadCertificate" >
    <div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center'" style="padding:10px;">
    <div style="margin-bottom:20px">
    <form id="excelform" method="post" enctype="multipart/form-data">
    <div>请选择要上传的证书:</div>
    <input name="certificate" class="easyui-filebox" data-options="required:'true',buttonText:'选择文件'" style="100%">
    <input type="hidden" id="accountId" name="accountId" value="" />
    </form>
    </div>
    </div>
    <div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
    <a id="import" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" >上传</a>
    <a id="cancel" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">取消</a>
    </div>
    </div>
    </div>


    .js

    (function($) {
    var pageType= $('#glPageType').val();
    var ctx= $('#ctx').val();
    var editIndex = undefined;
    var busMng = rmtCMHelper.createBusMngInst({
    busMngOptions:{
                    toUpload: function(flag){
    $('#accountId').val(flag);
    $('#uploadCertificate').window({
    300,
    height:150,
    title:'证书导入',
    minimizable:false,
    maximizable:false,
    collapsible:false,
    modal:true
    });
    $('#uploadCertificate').window('open');
    },
           supportOpFormmater: function(){
    return function(value,row,index){
    return '<a href="javascript:void(0);" class="todo" onclick="rmtManager.toToken( '+row.id+' )">查看TOKEN</a>&nbsp;&nbsp;&nbsp;&nbsp;'
    + '<a href="javascript:void(0);" class="todo" onclick="rmtManager.addConfig( '+row.id+' )">配置</a>&nbsp;&nbsp;&nbsp;&nbsp;'
    + '<a href="javascript:void(0);" class="todo" onclick="rmtManager.toView( '+row.id+' )">查看</a>&nbsp;&nbsp;&nbsp;&nbsp;'
    +' <a href="javascript:void(0);" class="todo" onclick="rmtManager.toUpload('+row.id+' )">上传证书</a>&nbsp;&nbsp;&nbsp;&nbsp;';
    }
    }
    });
    $('#import').bind('click', function(){


    $('#excelform').form('submit', {
    url: ctx+"/wxbase/account/upload.html",
    onSubmit: function(){
    if($('#excelform').form('validate'))
    rmtHelper.showMask();
    },
    success:function(data){
    var data = eval('(' + data + ')');
    if(data.success){
    rmtHelper.hideMask();
    $('#uploadCertificate').window('close');
    rmtHelper.showMsg(data.content);
    rmtHelper.getCurCurdIst().reloadGrid();
    }else{
    rmtHelper.hideMask();
    rmtHelper.showMsg(data.content);
    }
    }
    });
    });
    $('#cancel').bind('click', function(){
    $('#uploadCertificate').window('close');
    });
    controller


    /**
    * 上传证书
    * @param request
    * @return
    */
    @RequestMapping(value="upload", method = RequestMethod.POST)
    @ResponseBody
    public ResultJsonInfo uploadCertificate(@RequestParam("certificate") MultipartFile certificate, HttpServletRequest request)throws Exception {
    String accountId = request.getParameter("accountId");
    String fileName="";
    try {
    //本地地址,与下面荧光字一样的效果,二取一
    String picPath ="D://"+ accountId +"/";
    //判断之前是否存在证书
    File certificateFile = new File(picPath+accountId+".cer");

    if(certificateFile.exists()) {
    //已存在证书则删除
    certificateFile.delete();
    }

    //使用配置文件拼接地址,上传服务器
    String picPath = SysConfigHelper.getConfig("cer.dierct")+ accountId +"//";
    //判断之前是否存在证书 SysConfigHelper.getConfig
    File certificateFiles = new File(picPath);

    if(certificateFiles.isDirectory()) {
    File[] a= certificateFiles.listFiles();
    for(int i=0;i<a.length;i++){
    a[i].delete();

    }
    }

    //根据真实路径创建目录文件
    File picSaveFile = new File(picPath);
    //判断是否存在路径
    if(!picSaveFile.exists()) {
    //如果不存在则创建路径
    picSaveFile.mkdirs();
    }
    if (!certificate.isEmpty()) {
    try {
    byte[] buffer =new byte[1024*1024];
    int bytesum = 0;
    int byteread = 0;
    fileName=accountId+".cer";
    FileOutputStream fs=new FileOutputStream( picPath + fileName);
    bytesum+=byteread;
    fs.write(buffer,0,byteread);
    System.out.println("文件流"+certificate.getOriginalFilename());
    // 转存文件 */
    /*certificate.transferTo(new File(picPath));*/
    } catch (Exception e) {
    e.printStackTrace();
    }
    return new ResultJsonInfo(true,"上传成功"+",");
    }
    }catch (Exception e) {
    e.printStackTrace();
    return new ResultJsonInfo(false,"上传失败"+",");
    }
    /*logger.info("导入成功笔数:"+count+",耗时:" + (end - start) + "毫秒");*/
    return new ResultJsonInfo(true,"上传成功"+",");
    }

    紫色部分都要相同命名
    What do you want to be?
  • 相关阅读:
    〖Linux〗秒开www.stackoverflow.com,非代理方式
    〖Linux〗git push orgin master不能解析域名的解决方法
    unity, terrain道出为obj
    unity, 顶点对齐
    world machine, 输出lightmap
    unity, scene视图查看场景时应调成正交模式
    unity, 由scriptableObject创建.asset
    unity, 播放循环背景音乐注意事项
    用audacity制作循环背景音乐
    unity, 保存prefab时material丢失问题
  • 原文地址:https://www.cnblogs.com/CatsBlog/p/9212498.html
Copyright © 2011-2022 走看看