当前位置: 移动技术网 > IT编程>开发语言>Java > 详解spring mvc(注解)上传文件的简单例子

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

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> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网