springmvc实现文件上传的步骤:html
<form action="uc/adduser" method="post" enctype="multipart/form-data">
<p>
用户名:<input type="text" name="loginname" id="loginname" />
</p>
<p>
密码:<input type="text" name="loginpwd" id="loginpwd" />
</p>
<p>
头像:<input type="file" name="pic" id="pic" />
</p>
<p>
<input type="submit" value="注册" />
</p>
</form>
<!-- 文件上传依赖(该依赖下载时,commons-io-2.2.jar也随着添加到依赖中) --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency>
<!-- 图片上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--每一个文件上传大小-->
<property name="maxUploadSizePerFile" value="102400000"></property>
</bean>
控制器代码:java
@RequestMapping(value = "adduser", method = RequestMethod.POST) public String addUser(Users users, @RequestParam MultipartFile pic, HttpServletRequest request, Model model) { // 原始上传文件名字 String filename = pic.getOriginalFilename(); // 文件类型 String contentType = pic.getContentType(); InputStream is = null; OutputStream os = null; // 上传目录的绝对路径 String realpath = request.getSession().getServletContext() .getRealPath("/upload"); // 获得一个随机的字符串做为上传文件的新名字(防止文件因名字相同而覆盖) String uuid = UUID.randomUUID().toString(); // 得到文件的后缀名 String endname = filename.substring(filename.lastIndexOf("."), filename.length()); // 要把文件输出到硬盘上有两种方式 // 方式一:本身写字节流 并经过边读边写操做完成输出 // try { // is = pic.getInputStream(); // os = new FileOutputStream(new File(realpath + "/" + uuid + endname)); // byte[] b = new byte[1024]; // int len = is.read(b); // while (len != -1) { // os.write(b, 0, len); // len = is.read(b); // } // } catch (IOException e1) { // e1.printStackTrace(); // } finally { // try { // os.flush(); // os.close(); // is.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // 方式2、经过commons-io包提供的工具来完成拷贝 try { is = pic.getInputStream(); os = new FileOutputStream(new File(realpath + "/" + uuid + endname)); FileCopyUtils.copy(is, os); } catch (IOException e) { e.printStackTrace(); } finally { try { os.flush(); os.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } // 上传完成后,还须要把文件路径和名字保存到user对象中,而后把user对象的信息,保存到数据库中 // 下次可以根据数据库的记录,来找到对应的文件 users.setPicpath(uuid + endname); boolean flag = userServiceImpl.addUser(users); if (flag) { model.addAttribute("mypic", uuid + endname); return "add_suc"; } return "index"; }
SpringMVC 文件下载核心代码:web
页面中将须要下载的文件的名字参数传输到控制器中,在控制器中的代码以下(文件保存在项目根目录的upload文件夹里面):spring
/** * 实现下载的步骤: 一、须要把数据以流的形式响应到页面 二、须要在页面上打开Content-disposition=attachment * * @return */ @RequestMapping(value = "downloadfile") public ResponseEntity<byte[]> downloadFile(String mypicname, HttpServletRequest request) { // 读取文件保存的根位置 String realPath = request.getSession().getServletContext() .getRealPath("/upload"); File file = new File(realPath + "/" + mypicname); HttpHeaders header = new HttpHeaders(); header.setContentDispositionFormData("attachment", mypicname); ResponseEntity<byte[]> re = null; try { re = new ResponseEntity<byte[]>( FileUtils.readFileToByteArray(file), header, HttpStatus.OK); } catch (IOException e) { e.printStackTrace(); } return re; }