发布网友 发布时间:2022-06-06 15:55
共1个回答
热心网友 时间:2023-10-10 14:27
//jsp
<form id="tjform1" action="unzip" method="post" enctype="multipart/form-data">
<table>
<div class="list-3 fix"><span>导入:</span>
<input type="file" style="background:#ccc" name="file" class="search-btn mlr10"/>
<input id="drzp" type="button" class="search-btn mlr10" value="导入" />
</div>
</table>
</form>
//action
@RequestMapping(value = "unzip")
public String unzip(MultipartFile file, HttpServletRequest request, HttpServletResponse response){
/***********************上传到服务器*************************/
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String newfilename = new SimpleDateFormat("yyyy-MM-dd-HHmmssSSS").format(new Date()) + suffix;
String str1 = new StringBuilder().append(request.getSession().getServletContext().getRealPath("")).append(File.separator).append("test").append(File.separator).append("excel").append(File.separator).append(newfilename).toString();//存储路径 webapp/test/excel
File file1 = new File(str1);
//创建文件夹
if (!(file1.getParentFile().exists())){
file1.getParentFile().mkdirs();
}
if (!(file1.exists())) {
file1.createNewFile();
}
//输出文件
file.transferTo(file1);
}