下载==MultipartHttpServletRequest==jar包
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3</version> </dependency>
前端代码(伪代码)
function f_savedata_bak(){ var formData = new FormData($('#fileForm')[0]); formData.append('userid','2'); formData.append('header',["对方账号","交易时间","对方单位","转入金额"]); formData.append('tableName','T_CTRI_CW_YHRJZ_TEMP'); formData.append('tableField',["C_ACCOUNTNO","C_JYSJ","C_KHMC","C_ZRJE"]); $.ajax({ type:'post', url:getContextPath() + "/*.IMPXLS", data:formData, cache:false, processData: false, contentType: false, }).success(function(data){ top.dhtmlx.alert({ title:"成功", type: "alert", ok: "确 定", text: "上传成功!", callback: function(){ $("#btnSave").attr("disabled",false); window.returnValue = true; windowHandler.btnClose.click(); } }); }).error(function(){ top.dhtmlx.alert({ title: "错误", type: "alert-error", ok: "返 回", text: "上传失败! "+msg, callback: function(){ $("#btnSave").attr("disabled",false); } }); }); }
将form中的数据封装到 formdata对象中
后端读取 formdata中的数据
/** * @param request * 根据请求解析请求中的参数(文件与非文件)返回Map集合,并将文件上传至服务器 * @return */ private static Map getFilePath(HttpServletRequest request) { String upload_directory = "upload"; String slash_directory = "/"; // 上传配置 int memory_threshold = 1024 * 1024 * 3; // 3MB int max_file_size = 1024 * 1024 * 40; // 40MB int max_request_size = 1024 * 1024 * 50; // 50MB // 配置上传参数 DiskFileItemFactory factory = new DiskFileItemFactory(); // 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中 factory.setSizeThreshold(memory_threshold); // 设置临时存储目录 factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); ServletFileUpload upload = new ServletFileUpload(factory); // 设置最大文件上传值 upload.setFileSizeMax(max_file_size); // 设置最大请求值 (包含文件和表单数据) upload.setSizeMax(max_request_size); upload.setHeaderEncoding("UTF-8"); String uploadPath = request.getContextPath()+slash_directory+upload_directory; String fileName=""; String filePath=""; // 如果目录不存在则创建 File uploadDir = new File(uploadPath); if (!uploadDir.exists()) { uploadDir.mkdirs(); } Map paramMap = new HashMap(); try { // 解析请求的内容提取文件数据 @SuppressWarnings("unchecked") List<FileItem> formItems = upload.parseRequest(request); if (formItems != null && formItems.size() > 0) { // 迭代表单数据 for (FileItem item : formItems) { // 处理不在表单中的字段 图片 if (!item.isFormField()) { fileName = new File(item.getName()).getName(); filePath = uploadPath + slash_directory + fileName; File storeFile = new File(filePath); item.write(storeFile); paramMap.put("fileName", fileName); paramMap.put("filePath", filePath); }else { String value = item.getString("utf-8"); paramMap.put(item.getFieldName(), value); } } } } catch (Exception e) { e.printStackTrace(); } /*Iterator<Map.Entry> entries = paramMap.entrySet().iterator(); while (entries.hasNext()) { Map.Entry<Integer, Integer> entry = entries.next(); System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); }*/ return paramMap; }
接着再调用此方法来获取ajax传入的参数,这里需要注意的是上一步的方法获取到都是字符串形式的参数,因此对于前端传入的数组类型的参数需要再转成数组类型:
Map impfileMap = getFilePath(request);//获取解析后的参数列表 String userid = impfileMap.get("userid").toString();//操作人 String headerStr = impfileMap.get("header").toString();//数组形式,需要转换 String tableName = impfileMap.get("tableName").toString();//表名 String tableFieldStr = impfileMap.get("tableField").toString();//数组形式,需要转换 String[] tHeader = headerStr.split(",");//字符串转为字符串数组 String[] tableField = tableFieldStr.split(",");//字符串转为字符串数组