zoukankan      html  css  js  c++  java
  • 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>  


     

  • 相关阅读:
    scan chain的原理和实现——3.Tester Timing
    pattern——serial/paralel
    Scan and ATPG (Tessent)----1.基础概念
    ipdb介绍及Tensor
    pytorch简介
    Python 之map、filter、reduce
    分类器、logistic回归
    机器学习方法、距离度量、K_Means
    动态规划、图
    T分布
  • 原文地址:https://www.cnblogs.com/wjwen/p/4054514.html
Copyright © 2011-2022 走看看