首页 > 代码库 > springmvc下上传文件
springmvc下上传文件
使用ajax+表单+jQuery;
function sendFile() { var action = "c/goFile.do"; $("#form").ajaxSubmit( { url : action, success : function(data) { var htm = "<per>"+data "</per>" $("#content").text(data); console.log(data); } }); return false;}
<form id="form" enctype="multipart/form-data" method="post" onsubmit="return saveReport()"> <input id="file" name="file" type="file" onchange="fileChange()"/> </form>
@RequestMapping("/goFile") @ResponseBody public String goFile(@RequestParam MultipartFile file, Model model) throws Exception{ String str = InputStreamTOString(file.getInputStream(), "utf-8"); return str ; } public String InputStreamTOString(InputStream in,String encoding) throws Exception{ int BUFFER_SIZE = 1024; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = http://www.mamicode.com/new byte[BUFFER_SIZE]; int count = -1; while((count = in.read(data,0,BUFFER_SIZE)) != -1) outStream.write(data, 0, count); data = null; return new String(outStream.toByteArray(),encoding); }
springmvc下上传文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。