Spring-MVC文件上传

     

  1. <!--jsp页面-->
  2. <form action="testUpload" method="post" enctype="multipart/form-data">
  3.             <input type="file" name="file"><br>
  4.             <input type="text" name="desc"><br>
  5.             <input type="submit" value="提交">
  6.  </form>

     

     

  • //controller配置
  • @Controller
  • public class ServletFile {
  •     @RequestMapping("testUpload")
  •     public String testUpload(@RequestParam("desc") String desc , @RequestParam("file") MultipartFile file) throws IOException{
  •             InputStream is = file.getInputStream();
  •             String relname = file.getOriginalFilename();
  •             OutputStream os = new FileOutputStream("D:\\"+relname);
  •             byte[] byt = new byte[1024];
  •             int len = -1;
  •             
  •             while((len = is.read(byt)) != -1){
  •                 os.write(byt,0,len);
  •             }
  •             os.close();
  •             is.close();   
  •         return "MyJsp";
  •     }
  • }

     

     

  • <!-- Springmvc.xml配置 -->
  • <!-- 文件上传 -->
  •         <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  •             <!-- 上传编码 -->
  •             <property name="defaultEncoding" value="UTF-8"></property>
  •             <!-- 上传最大文件  "-1"为无限 -->
  •             <property name="maxUploadSize" value="1024000"></property>
  •             <!-- 客户端》服务器内存》》服务器硬盘   此处大小代表服务器内存 -->
  •             <property name="maxInMemorySize" value="666666" ></property>
  •         </bean>

  

posted @ 2020-10-17 14:24  黑质白章  阅读(100)  评论(0编辑  收藏  举报