亲宝软件园·资讯

展开

spring mvc 文件上传 详解spring mvc(注解)上传文件的简单例子

@ 小浩 人气:0
想了解详解spring mvc(注解)上传文件的简单例子的相关内容吗,@ 小浩在本文为您仔细讲解spring mvc 文件上传的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,mvc,文件上传,springmvc的文件上传,springmvc,大文件上传,下面大家一起来学习吧。

spring mvc(注解)上传文件的简单例子。

这有几个需要注意的地方

1.form的enctype=”multipart/form-data” 这个是上传文件必须的

2.applicationContext.xml中 <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/> 关于文件上传的配置不能少

3、亲这两个jar包不能少哦,之前就是因为少了这个两个jar,一直报一些奇葩的错,给我累了个半死~(版本没什么要求)

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar 

大家可以看具体代码如下:

web.xml

<?xml version="0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns="http://javasuncom/xml/ns/javaee" xmlns:web="http://javasuncom/xml/ns/javaee/web-app_2_xsd" xsi:schemaLocation="http://javasuncom/xml/ns/javaee http://javasuncom/xml/ns/javaee/web-app_2_xsd" id="WebApp_ID" version="5"> 
 <display-name>webtest</display-name> 
 
 <listener> 
    <listener-class>orgspringframeworkwebcontextContextLoaderListener</listener-class> 
  </listener> 
  <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
      /WEB-INF/config/applicationContextxml 
      /WEB-INF/config/codeifActionxml 
    </param-value> 
  </context-param> 
 
  <servlet> 
    <servlet-name>dispatcherServlet</servlet-name> 
    <servlet-class>orgspringframeworkwebservletDispatcherServlet</servlet-class> 
    <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/config/codeifActionxml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
  </servlet> 
  <!-- 拦截所有以do结尾的请求 --> 
  <servlet-mapping> 
    <servlet-name>dispatcherServlet</servlet-name> 
    <url-pattern>*do</url-pattern> 
  </servlet-mapping> 
 
 <welcome-file-list> 
  <welcome-file>indexdo</welcome-file> 
 </welcome-file-list> 
</web-app> 

applicationContext.xml

<?xml version="0" encoding="UTF-8"?> 
<beans xmlns="http://wwwspringframeworkorg/schema/beans" 
  xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:p="http://wwwspringframeworkorg/schema/p" 
  xmlns:context="http://wwwspringframeworkorg/schema/context" 
  xsi:schemaLocation="http://wwwspringframeworkorg/schema/beans http://wwwspringframeworkorg/schema/beans/spring-beans-xsd 
  http://wwwspringframeworkorg/schema/context http://wwwspringframeworkorg/schema/context/spring-context-xsd" 
  default-lazy-init="true"> 
 
  <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> 
  <bean class="orgspringframeworkwebservletmvcannotationAnnotationMethodHandlerAdapter" lazy-init="false" /> 
 
  <!-- 另外最好还要加入DefaultAnnotationHandlerMapping,不然会被 XML或其它的映射覆盖! --> 
  <bean class="orgspringframeworkwebservletmvcannotationDefaultAnnotationHandlerMapping" /> 
 
  <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> 
  <bean class="orgspringframeworkwebservletviewInternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix="jsp" /> 
 
  <!-- 支持上传文件 --> 
  <bean id="multipartResolver" class="orgspringframeworkwebmultipartcommonsCommonsMultipartResolver"/> 
 
</beans> 

codeifAction.xml

<?xml version="0" encoding="UTF-8"?> 
<beans xmlns="http://wwwspringframeworkorg/schema/beans" 
  xmlns:xsi="http://wwwworg/2001/XMLSchema-instance" xmlns:context="http://wwwspringframeworkorg/schema/context" 
  xsi:schemaLocation="http://wwwspringframeworkorg/schema/beans http://wwwspringframeworkorg/schema/beans/spring-beans-xsd 
  http://wwwspringframeworkorg/schema/context http://wwwspringframeworkorg/schema/context/spring-context-xsd" 
  default-lazy-init="true"> 
 
  <bean id="uploadAction" class="comcodeifactionUploadAction" /> 
 
</beans> 

UploadAction.java

package comcodeifaction; 
 
import javaioFile; 
import javautilDate; 
 
import javaxservlethttpHttpServletRequest; 
 
import orgspringframeworkstereotypeController; 
import orgspringframeworkuiModelMap; 
import orgspringframeworkwebbindannotationRequestMapping; 
import orgspringframeworkwebbindannotationRequestParam; 
import orgspringframeworkwebmultipartMultipartFile; 
 
@Controller 
public class UploadAction { 
 
  @RequestMapping(value = "/uploaddo") 
  public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) { 
 
    Systemoutprintln("开始"); 
    String path = requestgetSession()getServletContext()getRealPath("upload"); 
    String fileName = filegetOriginalFilename(); 
//    String fileName = new Date()getTime()+"jpg"; 
    Systemoutprintln(path); 
    File targetFile = new File(path, fileName); 
    if(!targetFileexists()){ 
      targetFilemkdirs(); 
    } 
 
    //保存 
    try { 
      filetransferTo(targetFile); 
    } catch (Exception e) { 
      eprintStackTrace(); 
    } 
    modeladdAttribute("fileUrl", requestgetContextPath()+"/upload/"+fileName); 
 
    return "result"; 
  } 
 
} 


index.jsp

<%@ page pageEncoding="utf-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>上传图片</title> 
</head> 
<body> 
<form action="uploaddo" method="post" enctype="multipart/form-data"> 
<input type="file" name="file" /> <input type="submit" value="Submit" /></form> 
</body> 
</html> 


WEB-INF/jsp/下的result.jsp

<%@ page pageEncoding="utf-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>上传结果</title> 
</head> 
<body> 
<img alt="" src="${fileUrl }" /> 
</body> 
</html> 

加载全部内容

相关教程
猜你喜欢
用户评论