如何利用form表单提交文件,和SSM如何接收文件参数,并写入到指定路径下的文件中

来源:互联网 发布:上海交大网络教育网 编辑:程序博客网 时间:2024/05/18 15:23


<form action="user/excelImport.do" method="post" enctype="multipart/form-data">
  提交文件:<input type="file" name="file"><br><br>
  <input type="button" value="获取模板" onclick="excel();"/><br><br>
  <input type="submit" value="确认" onclick="refresh();"/>
 </form>


@RequestMapping("/excelImport.do")
@ResponseBody
//MultipartFile file 文件过大需要导入commons-fileupload-1.3.jar
//然后配置bean
public void excelImport(
@RequestParam("file") MultipartFile file,
HttpServletRequest req,HttpServletResponse rep) throws IOException {@SuppressWarnings("deprecation")

  String Time =System.currentTimeMillis()+"";

// 这里的路径是我项目中的文件夹的名称
String filePath = req.getRealPath("wdxsl")+"\\"+Time;
File importExcel = new File(filePath);
InputStream is = file.getInputStream();
FileOutputStream os = new FileOutputStream(importExcel);

byte[] by = new byte[1024*10];
while(is.read(by)!=-1){
os.write(by);
}
is.close();
os.close();
}

0 0