java之spring mvc之文件上传
生活随笔
收集整理的這篇文章主要介紹了
java之spring mvc之文件上传
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄結構如下:
?
注意,下面說的配置文件,一般都是值的src下的配置文件,即mvc.xml。如果是web.xml,則直接說 web.xml
1. 文件上傳的注意點
表單必須是post提交,必須將 enctype 設置為 “multipart/form-data”,
使用 commons-fileupload 提交文件,需要添加 commons-fileupload 和 commons-io 的 jar 包。
2.Jsp 頁面
<form action="file/upload.do" method="post" enctype="multipart/form-data"> 文件:<input type="file" name="file"/><input type="submit" value="上傳"/> </form> </body>3.Controller類
@Controller //窄化 @RequestMapping("/file") public class UploadController {@RequestMapping("/upload.do")public String upload(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest req) throws Exception{String path=req.getServletContext().getRealPath("/upload");//獲取文件名String fileName=file.getOriginalFilename();InputStream is = file.getInputStream();OutputStream os = new FileOutputStream(new File(path,fileName));byte[] buffer = new byte[400];int len=0;while((len=is.read(buffer))!=-1){os.write(buffer, 0, len);}os.close();is.close();return "redirect:/index.jsp";} }4. 在配置 文件中添加 multipartResolver
<!-- 文件上傳配置 --><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="1000000"/></bean>?
?附錄:
附一,這里附上mvc.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:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 注解開發適配器 --><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 配置視圖解析器 --><bean id="viewResolver"class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><!-- 為響應的視圖名稱加上前綴 --><property name="prefix" value="/WEB-INF/jsp/"/><!-- 為響應的視圖名稱加上后綴 --><property name="suffix" value=".jsp"/></bean><!-- 文件上傳配置 --><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="1000000"/></bean><!-- 掃描注解類 --><context:component-scan base-package="cn.sxt.controller"/> </beans>?
?這里再附上 WebContent/WEB-INF/ 下的 web.xml 文件內容
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>01springmvc_helloworld</display-name><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 改變springmvc配置文件的路徑及名稱 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list> </web-app>?
轉載于:https://www.cnblogs.com/Vincent-yuan/p/11278749.html
總結
以上是生活随笔為你收集整理的java之spring mvc之文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 车贷款还清后解押流程
- 下一篇: 小程序登录设置