多文件上传

来源:互联网 发布:it企业招人难 编辑:程序博客网 时间:2024/06/05 08:57
 <div class="contract_item">
        <span>合同文件:</span>
        <div style="margin-left:100px">
        <form id="form" name="form">
<input type="file" ms-duplex="file" ms-change="filechange" name="file" id="contract_file1"/>
<input type="file" ms-duplex="file" ms-change="filechange" name="file" id="contract_file2"/>
<input type="file" ms-duplex="file" ms-change="filechange" name="file" id="contract_file3">
</form>
        </div>
       

       </div>

function saveInfo() {
       if (confirm("确定要签订吗?")) {
        obj=new Object();
               obj.order_id = $('#order_id').val();
               obj.contract_start = $('#contract_start').val();
               obj.contract_end = $('#contract_end').val();
               obj.contract_amount = $('#contract_amount').val();
               obj.contract_code = $('#contract_code').val();
               obj.ifContract = $('#ifContract').val();
               var params = $.param(obj);
               
               if(obj.contract_amount.length==0){
               alert("请输入合同金额!");
               return;
               }
               if(obj.contract_code.length==0){
               alert("请输入合同编号!");
               return;
               }
               if($('#contract_file1').val().length==0||$('#contract_file2').val().length==0||$('#contract_file3').val().length==0){
               alert("请选择上传文件!");
               return;
               }
               
               var fd = new FormData(document.querySelector("form"));
               
               var dat_url = "<%=path%>/aOrder/signedContract.do";
               $.ajax({
                       url: dat_url + '?' + $.param(obj),
                       type: 'POST',
                       contentType: false ,
                       processData: false,
                       data: fd,
                       success: function(data) {
                               if (data.success) {
                                       alert("签订成功");
                                       $("#bt_close").click();
                                       getData(curpage);
                               }else {
                                   alert("签订失败");
                               }
                       }
               });
       }
}

/**
* 签订合同
* @param request
* @return
* @throws SQLException
* @throws IOException 
*/
@ResponseBody
@RequestMapping("/signedContract")
public Map<String, Object> signedContract(@RequestParam("order_id") String orderId, // /
@RequestParam("contract_start") String contract_start, //
@RequestParam("contract_end") String contract_end, //
@RequestParam("contract_amount") Double contract_amount,//
@RequestParam("contract_code") String contract_code, //
@RequestParam("ifContract") String ifContract, //
@RequestParam("file")  MultipartFile[] files,//
HttpServletResponse response,//
HttpServletRequest request) throws SQLException, IOException {



Map<String, Object> mapParam = new HashMap<String, Object>();
Map<String, Object> result = new HashMap<String, Object>();


String srcfiles = "";
for(MultipartFile myfile : files){  
            if(!myfile.isEmpty()){  
                System.out.println("文件长度: " + myfile.getSize());  
                System.out.println("文件类型: " + myfile.getContentType());  
                System.out.println("文件名称: " + myfile.getName());  
                System.out.println("文件原名: " + myfile.getOriginalFilename());  
                System.out.println("========================================");  
                //如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中  
                String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");  
                File file = new File(realPath);
        if(!file.exists()){
        file.mkdir();
        }
                //这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的  
                FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));
                srcfiles += myfile.getOriginalFilename()+",";
            }
        } 
if(StringUtils.isNotEmpty(srcfiles)){
srcfiles = srcfiles.substring(0, srcfiles.length() - 1);
}




mapParam.put("orderId", orderId);
mapParam.put("contractFile", srcfiles);
mapParam.put("contractstart", contract_start);
mapParam.put("contractend", contract_end);
mapParam.put("contractAmount", contract_amount);
mapParam.put("contractCode", contract_code);
mapParam.put("ifContract", ifContract);


try {
int flag = orderService.update(mapParam);
if (flag > 0) {
result = successMap("合同签订成功!");
} else {
result = failMap("合同签订失败!");
}
} catch (Exception e) {
logger.error("签订合同失败{}", e);
}
return result;
}


<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
        <!-- uploadTempDir为上传的临时目录 -->
        <property name="uploadTempDir" value="fileUpload/temp" />  
    </bean> 

原创粉丝点击