博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springmvc异步上传文件
阅读量:5163 次
发布时间:2019-06-13

本文共 3365 字,大约阅读时间需要 11 分钟。

前提条件

注意:bean的id名不能改变

 

前台页面upload.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    
Document
上传XML模版文件:

 

 

后台Controller代码

//这种是使用传统的form表单提交的,即在前台页面上点击‘上传’按钮上传文件的形式, //而且在form标签上必须指定enctype="multipart/form-data" @RequestMapping(value = "getParamFromFile",method = RequestMethod.POST)	@ResponseBody	public Map
getParamFromFile(@RequestParam("uploadFile") MultipartFile uploadFile,HttpServletRequest request, HttpServletResponse response) { Map
paramMap = new LinkedHashMap
(); FileInputStream inputStream = null; try{ String filename = uploadFile.getName(); String originalFilename = uploadFile.getOriginalFilename(); System.out.println("FileName = " + filename); System.out.println("originalFilename = " + originalFilename); inputStream = (FileInputStream) uploadFile.getInputStream(); } catch (IOException e1) { paramMap.put("message", "上传文件错误"); return paramMap; } //String filePath = "E:\\work\\svn\\openeap\\code\\openeap\\src\\main\\webapp\\template\\北京市房屋租赁合同5.xml"; //paramMap = ImportFile.getParamFromFile(filePath); paramMap = ImportFile.getParamFromStream(inputStream); Iterator it = paramMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); System.out.println("参数 Key: " + e.getKey() + "; 参数 Value: " + e.getValue()); } System.out.println("生成的json为: " + JsonMapper.getInstance().toJson(paramMap)); return paramMap; } //jQuery form插件的使用 后台 @RequestMapping(value = "getParamFromFileForAjax",method = RequestMethod.POST) @ResponseBody public Map
getParamFromFileForAjax(HttpServletRequest request,String fileName) { Map
paramMap = new LinkedHashMap
(); FileInputStream inputStream = null; //把Request强转成多部件请求对象 MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request; //根据文件名称获取文件对象 CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartHttpServletRequest.getFile(fileName); try{ String filename = commonsMultipartFile.getName(); String originalFilename = commonsMultipartFile.getOriginalFilename(); System.out.println("FileName = " + filename); System.out.println("originalFilename = " + originalFilename); inputStream = (FileInputStream) commonsMultipartFile.getInputStream(); } catch (IOException e1) { paramMap.put("message", "上传文件错误"); return paramMap; } //String filePath = "E:\\work\\svn\\openeap\\code\\openeap\\src\\main\\webapp\\template\\北京市房屋租赁合同5.xml"; //paramMap = ImportFile.getParamFromFile(filePath); paramMap = ImportFile.getParamFromStream(inputStream); Iterator it = paramMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); System.out.println("参数 Key: " + e.getKey() + "; 参数 Value: " + e.getValue()); } System.out.println("生成的json为: " + JsonMapper.getInstance().toJson(paramMap)); return paramMap; }

  

  异步上传文件有两种方式:

  参考的文章:

    http://www.cnblogs.com/zhuxiaojie/p/4783939.html#autoid-1-0-0

 

转载于:https://www.cnblogs.com/move22/p/7246336.html

你可能感兴趣的文章
关于PHP会话:session和cookie
查看>>
导航,头部,CSS基础
查看>>
转负二进制(个人模版)
查看>>
LintCode-Backpack
查看>>
查询数据库锁
查看>>
面试时被问到的问题
查看>>
注解小结
查看>>
201421410014蒋佳奇
查看>>
Xcode5和ObjC新特性
查看>>
CSS属性值currentColor
查看>>
Real-Time Rendering 笔记
查看>>
多路复用
查看>>
处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“Manag
查看>>
利用SignalR来同步更新Winfrom
查看>>
反射机制
查看>>
CocoaPod
查看>>
BZOJ 1251: 序列终结者 [splay]
查看>>
5G边缘网络虚拟化的利器:vCPE和SD-WAN
查看>>
MATLAB基础入门笔记
查看>>
【UVA】434-Matty&#39;s Blocks
查看>>