SpringMVc上传excel或csv文件

1、JSP页面代码

<form  enctype=""multipart/form-data"  method="post">
<input type="file" name="file">
<input type="submit" value="上传" >
</form>

2、controller中代码

@RequestMapping("/sysfindheimingdan/excelmobanupload")
@ResponseBody
public void uploadExcelModel(HttpServletRequest request,HttpServletResponse response) throws Exception {
//开始上传excel
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile("file");
String sourceName = multipartFile.getOriginalFilename(); // 原始文件名
Long size = multipartFile.getSize();
String fileType = sourceName.substring(sourceName.lastIndexOf("."));
System.out.println("上传的文件名为:"+sourceName+"类型为:"+fileType+"大小:"+size);
String base = request.getSession().getServletContext().getRealPath("/upload//"); //获取文件上传的路径,在webapp下的upload中
File file = new File(base);
if(!file.exists()){
file.mkdirs();
}
String path=base + File.separator + sourceName;
multipartFile.transferTo(new File(path)); }
上一篇:也发一个自己实现的android简单文件选择器代码。支持多卡,排序


下一篇:IntelliJ IDEA像Eclipse一样打开多个项目