欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程语言 > c/c >内容正文

c/c

spring mvc(注解)上传文件的简单例子 -凯发k8官方网

发布时间:2025/1/21 c/c 24 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 spring mvc(注解)上传文件的简单例子 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

 spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方
1.form的enctype=”multipart/form-data” 这个是上传文件必须的
2.applicationcontext.xml中 关于文件上传的配置不能少

 

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

 

web.xml

[html] view plaincopyprint?
  • 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5">  
  •   <display-name>webtestdisplay-name>  
  •   
  •   <listener>  
  •         <listener-class>org.springframework.web.context.contextloaderlistenerlistener-class>  
  •     listener>  
  •     <context-param>  
  •         <param-name>contextconfiglocationparam-name>  
  •         <param-value>  
  •             /web-inf/config/applicationcontext.xml  
  •             /web-inf/config/codeifaction.xml  
  •         param-value>  
  •     context-param>  
  •   
  •     <servlet>  
  •         <servlet-name>dispatcherservletservlet-name>  
  •         <servlet-class>org.springframework.web.servlet.dispatcherservletservlet-class>  
  •         <init-param>  
  •             <param-name>contextconfiglocationparam-name>  
  •             <param-value>/web-inf/config/codeifaction.xmlparam-value>  
  •         init-param>  
  •         <load-on-startup>1load-on-startup>  
  •     servlet>  
  •       
  •     <servlet-mapping>  
  •         <servlet-name>dispatcherservletservlet-name>  
  •         <url-pattern>*.dourl-pattern>  
  •     servlet-mapping>  
  •   
  •   <welcome-file-list>  
  •     <welcome-file>index.dowelcome-file>  
  •   welcome-file-list>  
  • web-app>  

  •  

     

    applicationcontext.xml

    [html] view plaincopyprint?
  • 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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  •     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"  
  •     default-lazy-init="true">  
  •   
  •       
  •     <bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter" lazy-init="false" />  
  •   
  •       
  •     <bean class="org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping" />  
  •   
  •       
  •     <bean class="org.springframework.web.servlet.view.internalresourceviewresolver" p:prefix="/web-inf/jsp/" p:suffix=".jsp" />  
  •   
  •       
  •     <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"/>  
  •   
  • beans>  

  •  

     

    codeifaction.xml

    [html] view plaincopyprint?
  • 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:context="http://www.springframework.org/schema/context"  
  •     xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  •     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"  
  •     default-lazy-init="true">  
  •   
  •     <bean id="uploadaction" class="com.codeif.action.uploadaction" />  
  •   
  • beans>  

  •  

     

    uploadaction.java

    [java] view plaincopyprint?
  • package com.codeif.action;  
  •   
  • import java.io.file;  
  • import java.util.date;  
  •   
  • import javax.servlet.http.httpservletrequest;  
  •   
  • import org.springframework.stereotype.controller;  
  • import org.springframework.ui.modelmap;  
  • import org.springframework.web.bind.annotation.requestmapping;  
  • import org.springframework.web.bind.annotation.requestparam;  
  • import org.springframework.web.multipart.multipartfile;  
  •   
  • @controller  
  • public class uploadaction {  
  •   
  •     @requestmapping(value = "/upload.do")  
  •     public string upload(@requestparam(value = "file", required = false) multipartfile file, httpservletrequest request, modelmap model) {  
  •   
  •         system.out.println("开始");  
  •         string path = request.getsession().getservletcontext().getrealpath("upload");  
  •         string filename = file.getoriginalfilename();  
  • //        string filename = new date().gettime() ".jpg";  
  •         system.out.println(path);  
  •         file targetfile = new file(path, filename);  
  •         if(!targetfile.exists()){  
  •             targetfile.mkdirs();  
  •         }  
  •   
  •         //保存  
  •         try {  
  •             file.transferto(targetfile);  
  •         } catch (exception e) {  
  •             e.printstacktrace();  
  •         }  
  •         model.addattribute("fileurl", request.getcontextpath() "/upload/" filename);  
  •   
  •         return "result";  
  •     }  
  •   
  • }  

  •  

    index.jsp

    [html] view plaincopyprint?
  • <%@ page pageencoding="utf-8"%>  
  • >  
  • <html>  
  • <head>  
  • <meta charset="utf-8">  
  • <title>上传图片title>  
  • head>  
  • <body>  
  • <form action="upload.do" method="post" enctype="multipart/form-data">  
  • <input type="file" name="file" /> <input type="submit" value="submit" />form>  
  • body>  
  • html>  

  •  

    web-inf/jsp/下的result.jsp

    [html] view plaincopyprint?
  • <%@ page pageencoding="utf-8"%>  
  • >  
  • <html>  
  • <head>  
  • <meta charset="utf-8">  
  • <title>上传结果title>  
  • head>  
  • <body>  
  • <img alt="" src="${fileurl }" />  
  • body>  
  • html>  
  • 总结

    以上是凯发k8官方网为你收集整理的spring mvc(注解)上传文件的简单例子的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

    网站地图