Spring mvc 文件上传到文件夹(转载+心得)

 spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方
1.form的enctype=”multipart/form-data” 这个是上传文件必须的
2.applicationContext.xml中 <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/> 关于文件上传的配置不能少

 

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  6.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"  
  7.     default-lazy-init="true">  
  8.   
  9.     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
  10.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" lazy-init="false" />  
  11.   
  12.     <!-- 另外最好还要加入DefaultAnnotationHandlerMapping,不然会被 XML或其它的映射覆盖! -->  
  13.     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
  14.   
  15.     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  
  16.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />  
  17.   
  18.     <!-- 支持上传文件  value表示上传文件的大小-->  
  19.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  20.  <property name="maxUploadSize" value="10000000000000"/></bean>
  21.   
  22. </beans>  

 

UploadAction.java

  1. package com.codeif.action;  
  2.   
  3. import java.io.File;  
  4. import java.util.Date;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7.   
  8. import org.springframework.stereotype.Controller;  
  9. import org.springframework.ui.ModelMap;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.bind.annotation.RequestParam;  
  12. import org.springframework.web.multipart.MultipartFile;  
  13.   
  14. @Controller  
  15. public class UploadAction {  
  16.   
  17.     @RequestMapping(value = "/upload.do")  
  18.     public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) {  
  19.   
  20.         System.out.println("开始");  
  21.         String path = request.getSession().getServletContext().getRealPath("upload");  
  22.         String fileName = file.getOriginalFilename();  
  23. //        String fileName = new Date().getTime()+".jpg";  
  24.         System.out.println(path);  
  25.         File targetFile = new File(path, fileName);  
  26.         if(!targetFile.exists()){  
  27.             targetFile.mkdirs();  
  28.         }  
  29.   
  30.         //保存  
  31.         try {  
  32.            // file.transferTo(targetFile);  
  33.  //也可以用这种方式,上面的方式能上传,但是老是报输出流被占用的错误。
  34.     SaveFileFromInputStream(
          file.getInputStream(), realPath + filePath, name + "."+ fileType);
  35.         } catch (Exception e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.         model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);  
  39.   
  40.         return "result";  
  41.     }  
  42.   
  43. }  

SaveFileFromInputStream()的实现如下:

public void SaveFileFromInputStream(InputStream stream, String path,
   String filename) throws IOException {
   {      //path + "/"+ filename
    FileOutputStream fs = new FileOutputStream(path + "/"+ filename);
          byte[] buffer =new byte[1024*1024];
          int bytesum = 0;
          int byteread = 0;
          while ((byteread=stream.read(buffer))!=-1)
          {
             bytesum+=byteread;
             fs.write(buffer,0,byteread);
             fs.flush();
          }
          fs.close();
          stream.close();     
      }      
 

index.jsp

  1. <%@ page pageEncoding="utf-8"%>  
  2. <!DOCTYPE html>  
  3. <html>  
  4. <head>  
  5. <meta charset="utf-8">  
  6. <title>上传图片</title>  
  7. </head>  
  8. <body>  
  9. <form action="upload.do" method="post" enctype="multipart/form-data">  
  10. <input type="file" name="file" /> <input type="submit" value="Submit" /></form>  
  11. </body>  
  12. </html>  


 

转载于:https://www.cnblogs.com/wjwen/p/4054514.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值