标签:
1.jsp页面
<form id="importsurvey"action="。。。.action" method="post"enctype="multipart/form-data">
<input type="file" class="administrator_teaching_inquire_upload_img_zone_filebtn" name="file"/>
<input type="submit" id="upload"class="administrator_teaching_inquire_upload_btn"value="上传">
</form>
2.action页面
@RequestMapping(value = "/surveyimport", method = RequestMethod.POST)
public Object surveyupload(MultipartFile file, String courseId,Model model,RedirectAttributes attr,
HttpServletRequest request) throws ServiceException , IOException{
String path = request.getSession().getServletContext() .getRealPath("upload");
String fileName = file.getOriginalFilename();
File targetFile = new File(path, fileName);
//判断文件路径是否存在
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
String filePath = path + File.separator + fileName;//File.separator相当于"//",但在linus中无法识别
return "redirect:/。。。。.action";
3、上传所需的Jar包
1、commons-fileupload-1.3.jar
2、commons-io-2.4.jar
3、oreilly-10.3.jar
4、servlet-api.jar
标签:
原文地址:http://www.cnblogs.com/pearz/p/4772724.html