码迷,mamicode.com
首页 > Web开发 > 详细

文件上传

时间:2015-08-31 13:22:54      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!