<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                先看一下Servlet是如何處理文件上傳的: [Servlets - File Uploading](http://www.tutorialspoint.com/servlets/servlets-file-uploading.htm) [Java File Upload Example with Servlet 3.0 API](http://www.codejava.net/java-ee/servlet/java-file-upload-example-with-servlet-30-api) Spring MVC下的處理是類似的。 下面展示一個簡單的實現。 ## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#項目結構與源碼)項目結構與源碼 [![](https://box.kancloud.cn/2015-10-17_5621d5eaa9dc5.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-09/01.png) ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#webxml)web.xml ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> <multipart-config> <file-size-threshold>1000000</file-size-threshold> <max-file-size>2000000</max-file-size> <max-request-size>4000000</max-request-size> </multipart-config> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> </web-app> ~~~ 注意其中的限制文件大小的配置: ~~~ <multipart-config> <file-size-threshold>1000000</file-size-threshold> <max-file-size>2000000</max-file-size> <max-request-size>4000000</max-request-size> </multipart-config> ~~~ ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#applicationcontextxml)applicationContext.xml ~~~ <?xml version='1.0' encoding='UTF-8' ?> <!-- was: <?xml version="1.0" encoding="UTF-8"?> --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> </beans> ~~~ ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#dispatcher-servletxml)dispatcher-servlet.xml ~~~ <?xml version='1.0' encoding='UTF-8' ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="me.letiantian.controller" /> <context:component-scan base-package="me.letiantian.form" /> <mvc:annotation-driven/> <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"> </bean> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index">indexController</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController" p:viewName="index" /> <mvc:resources mapping="/static/**" location="/static/"/> </beans> ~~~ 注意,這里增加了一個multipart解析器`StandardServletMultipartResolver`,用來處理上傳的文件。`/static`是存放靜態資源的目錄, 我們也準備將上傳的文件放到這個目錄里。 ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#uploadedfilejava)UploadedFile.java ~~~ package me.letiantian.form; import org.springframework.web.multipart.MultipartFile; public class UploadedFile { private String fileName; private MultipartFile multipartFile; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public MultipartFile getMultipartFile() { return multipartFile; } public void setMultipartFile(MultipartFile multipartFile) { this.multipartFile = multipartFile; } } ~~~ ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#hellocontrollerjava)HelloController.java ~~~ package me.letiantian.controller; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import me.letiantian.form.UploadedFile; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.multipart.MultipartFile; @Controller @RequestMapping("/hello") public class HelloController{ @RequestMapping(value = "") public String index() { return "hello/index"; } @RequestMapping(value = "/upload") public void output(@ModelAttribute UploadedFile uploadedFile, HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); MultipartFile multiPartFile = uploadedFile.getMultipartFile(); System.out.println("文件原始名稱:"+multiPartFile.getOriginalFilename()); System.out.println("表單給定的文件名稱:"+uploadedFile.getFileName()); try{ System.out.println("上傳目錄:"+request.getServletContext().getRealPath("/static")); File file = new File(request.getServletContext().getRealPath("/static"), uploadedFile.getFileName()); multiPartFile.transferTo(file); // 將文件寫入本地 out.println("<h2>上傳成功</h2>"); } catch (Exception ex) { System.out.println(""+ex.getMessage()); out.println("<h2>上傳失敗</h2>"); } } } ~~~ 注意,表單數據被綁定到了`uploadedFile`對象中。 ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#helloindexjsp)hello/index.jsp ~~~ <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <form method="post" action="${pageContext.request.contextPath}/hello/upload" enctype="multipart/form-data"> 指定文件名:<input type="text" name="fileName" /> <br/> 選擇文件: <input type="file" name="multipartFile" size="60"/> <br/> <input type="submit" value="Upload" /> </form> </body> </html> ~~~ ## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#測試程序)測試程序 選擇文件,指定名稱: [![](https://box.kancloud.cn/2015-10-17_5621d5eab82eb.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-09/02.png) 上傳成功: [![](https://box.kancloud.cn/2015-10-17_5621d5eac2c55.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-09/03.png) 查看上傳的文件: [![](https://box.kancloud.cn/2015-10-17_5621d5eacf7ef.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-09/04.png) Tomcat輸出: ~~~ 文件原始名稱:01.png 表單給定的文件名稱:1.png 上傳目錄:/data/Code/netbeans/Project_0109/build/web/static ~~~ ## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#資料)資料 《Spring MVC學習指南》 第11章先看一下Servlet是如何處理文件上傳的: [Servlets - File Uploading](http://www.tutorialspoint.com/servlets/servlets-file-uploading.htm) [Java File Upload Example with Servlet 3.0 API](http://www.codejava.net/java-ee/servlet/java-file-upload-example-with-servlet-30-api) Spring MVC下的處理是類似的。 下面展示一個簡單的實現。 ## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#項目結構與源碼)項目結構與源碼 [![](https://box.kancloud.cn/2015-10-17_5621d5eaec91e.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-09/01.png) ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#webxml)web.xml ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> <multipart-config> <file-size-threshold>1000000</file-size-threshold> <max-file-size>2000000</max-file-size> <max-request-size>4000000</max-request-size> </multipart-config> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> </web-app> ~~~ 注意其中的限制文件大小的配置: ~~~ <multipart-config> <file-size-threshold>1000000</file-size-threshold> <max-file-size>2000000</max-file-size> <max-request-size>4000000</max-request-size> </multipart-config> ~~~ ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#applicationcontextxml)applicationContext.xml ~~~ <?xml version='1.0' encoding='UTF-8' ?> <!-- was: <?xml version="1.0" encoding="UTF-8"?> --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> </beans> ~~~ ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#dispatcher-servletxml)dispatcher-servlet.xml ~~~ <?xml version='1.0' encoding='UTF-8' ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="me.letiantian.controller" /> <context:component-scan base-package="me.letiantian.form" /> <mvc:annotation-driven/> <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"> </bean> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index">indexController</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController" p:viewName="index" /> <mvc:resources mapping="/static/**" location="/static/"/> </beans> ~~~ 注意,這里增加了一個multipart解析器`StandardServletMultipartResolver`,用來處理上傳的文件。`/static`是存放靜態資源的目錄, 我們也準備將上傳的文件放到這個目錄里。 ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#uploadedfilejava)UploadedFile.java ~~~ package me.letiantian.form; import org.springframework.web.multipart.MultipartFile; public class UploadedFile { private String fileName; private MultipartFile multipartFile; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public MultipartFile getMultipartFile() { return multipartFile; } public void setMultipartFile(MultipartFile multipartFile) { this.multipartFile = multipartFile; } } ~~~ ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#hellocontrollerjava)HelloController.java ~~~ package me.letiantian.controller; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import me.letiantian.form.UploadedFile; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.multipart.MultipartFile; @Controller @RequestMapping("/hello") public class HelloController{ @RequestMapping(value = "") public String index() { return "hello/index"; } @RequestMapping(value = "/upload") public void output(@ModelAttribute UploadedFile uploadedFile, HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); MultipartFile multiPartFile = uploadedFile.getMultipartFile(); System.out.println("文件原始名稱:"+multiPartFile.getOriginalFilename()); System.out.println("表單給定的文件名稱:"+uploadedFile.getFileName()); try{ System.out.println("上傳目錄:"+request.getServletContext().getRealPath("/static")); File file = new File(request.getServletContext().getRealPath("/static"), uploadedFile.getFileName()); multiPartFile.transferTo(file); // 將文件寫入本地 out.println("<h2>上傳成功</h2>"); } catch (Exception ex) { System.out.println(""+ex.getMessage()); out.println("<h2>上傳失敗</h2>"); } } } ~~~ 注意,表單數據被綁定到了`uploadedFile`對象中。 ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#helloindexjsp)hello/index.jsp ~~~ <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <form method="post" action="${pageContext.request.contextPath}/hello/upload" enctype="multipart/form-data"> 指定文件名:<input type="text" name="fileName" /> <br/> 選擇文件: <input type="file" name="multipartFile" size="60"/> <br/> <input type="submit" value="Upload" /> </form> </body> </html> ~~~ ## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#測試程序)測試程序 選擇文件,指定名稱: [![](https://box.kancloud.cn/2015-10-17_5621d5eb0377c.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-09/02.png) 上傳成功: [![](https://box.kancloud.cn/2015-10-17_5621d5eb0c7ce.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-09/03.png) 查看上傳的文件: [![](https://box.kancloud.cn/2015-10-17_5621d5eb15f1b.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-09/04.png) Tomcat輸出: ~~~ 文件原始名稱:01.png 表單給定的文件名稱:1.png 上傳目錄:/data/Code/netbeans/Project_0109/build/web/static ~~~ ## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-09.md#資料)資料 《Spring MVC學習指南》 第11章
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看