十、文件上传
十、文件上传
https://www.cnblogs.com/sunniest/p/4555801.html
1.需要导入两个jar包

2.在SpringMVC配置文件中加入
    <!-- upload settings -->
    <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="102400000"></property>
    </bean>
3.方法代码
    @RequestMapping(value="/upload",method=RequestMethod.POST)
    public String upload(HttpServletRequest req) throws Exception{
        MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;
        MultipartFile file = mreq.getFile("file");
        String fileName = file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");        
        FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/")+
                "upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.')));
        fos.write(file.getBytes());
        fos.flush();
        fos.close();
        
        return "hello";
    }
4.前台form表单
      <form action="mvc/upload" method="post" enctype="multipart/form-data">
          <input type="file" name="file"><br>
          <input type="submit" value="submit">
      </form>
package test.SpringMVC; /* * D:\Indigo_workspace2\TrySpringMVC\src\test\SpringMVC\ upController.java * * */ import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; @Controller @RequestMapping("/up") public class upController { //http://localhost:8080/TrySpringMVC/up/upload @RequestMapping(value="/upload",method=RequestMethod.POST) public String upload(HttpServletRequest req) throws Exception{ MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req; MultipartFile file = mreq.getFile("file"); String fileName = file.getOriginalFilename(); System.out.println(fileName); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String filepath=req.getSession().getServletContext().getRealPath("/")+ "upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.')); System.out.println(filepath); try{ FileOutputStream fos = new FileOutputStream(filepath); fos.write(file.getBytes()); fos.flush(); fos.close(); }catch(Exception ex){ System.out.println(ex.getMessage()); } return "hello"; } }
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> D:\Indigo_workspace2\TrySpringMVC\WebContent\upload.html </title> <meta charset="UTF-8"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> </head> <body> <form action="up/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"><br> <input type="submit" value="submit"> </form> </body> </html>

 

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号