欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在SpringMVC框架下实现文件的上传和下载示例

程序员文章站 2024-03-07 14:18:33
在eclipse中的javaee环境下:导入必要的架包 web.xml的配置文件:

在eclipse中的javaee环境下:导入必要的架包

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_2_5.xsd" 
id="webapp_id" version="2.5">
 
   <!-- 配置springmvc的dispatcherservlet -->
  <servlet>
    <servlet-name>springdispatcherservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
    <init-param>
      <param-name>contextconfiglocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springdispatcherservlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
 
  <!-- 配置 hiddenhttpmethodfilter: 把 post 请求转为 delete、put 请求 -->
   <filter>
     <filter-name>hiddenhttpmethodfilter</filter-name>
     <filter-class>org.springframework.web.filter.hiddenhttpmethodfilter</filter-class>
   </filter>
   
   <filter-mapping>
     <filter-name>hiddenhttpmethodfilter</filter-name>
     <url-pattern>/*</url-pattern>
   </filter-mapping>
 
</web-app> 

spring的bean的配置文件springmvc.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:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemalocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  
  <!-- 配置自动扫描的包 -->
  <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>
  
  <!-- 配置视图解析器 -->
  <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
    <property name="prefix" value="/web-inf/views/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>
  
  <!-- 
    default-servlet-handler 将在 springmvc 上下文中定义一个 defaultservlethttprequesthandler,
    它会对进入 dispatcherservlet 的请求进行筛查, 如果发现是没有经过映射的请求, 就将该请求交由 web 应用服务器默认的 
    servlet 处理. 如果不是静态资源的请求,才由 dispatcherservlet 继续处理

    一般 web 应用服务器默认的 servlet 的名称都是 default.
    若所使用的 web 服务器的默认 servlet 名称不是 default,则需要通过 default-servlet-name 属性显式指定
    
  -->
  <mvc:default-servlet-handler/>
  
  <!-- 一般都会配置这个 <mvc:annotation-driven ></mvc:annotation-driven>,
  由于。。。requestmapping请求实现不了,使用这个,会使requestmapping请求一定实现
  -->
  <mvc:annotation-driven ></mvc:annotation-driven> 
  <!-- 配置 multipartresolver ,即配置文件上传的属性-->
   <bean id="multipartresolver" 
    class="org.springframework.web.multipart.commons.commonsmultipartresolver">
    <!-- 默认的字符编码 -->
    <property name="defaultencoding" value="utf-8"></property> 
    <!-- 上传文件的大小 ,最大上传大小-->
    <property name="maxuploadsize" value="1024000"></property>
   </bean>
 </beans> 

 handler类方法:实现文件的上传和下载的方法

 @controller
public class springmvctest {
  
  @autowired
  private employeedao employeedao;
  //实现文件的下载
  //需要说明的是文件的上传和下载不需要其他配置
  @requestmapping("testresponseentity")
  public responseentity<byte[]> testresponseentity(httpsession session) throws ioexception{
    
    byte[] body=null;
    servletcontext servletcontext=session.getservletcontext();
    ///files/abc.txt:所要下载文件的地址
    inputstream in=servletcontext.getresourceasstream("/files/abc.txt");
    body=new byte[in.available()];
    in.read(body);
    
    httpheaders headers=new httpheaders();
    //响应头的名字和响应头的值
    headers.add("content-disposition", "attachment;filename=abc.txt");
    
    httpstatus statuscode=httpstatus.ok;
    
    responseentity<byte[]> response=new responseentity<byte[]>(body, headers, statuscode);
    return response;
  } 
  //文件上传,
     @requestmapping("/testfileupload")
     public string testfileupload(@requestparam("desc") string desc,
      @requestparam("file") multipartfile file) throws ioexception{

      system.out.println("desc:"+desc);
      system.out.println("originalfilename"+file.getoriginalfilename());
      system.out.println("inputstream"+file.getinputstream());
      return "success";
   }
 } 

jsp页面:index.jsp:

<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>insert title here</title>
</head>
<body>
  
  <center> 
  <!-- 文件上传的表单 -->
   <form action="testfileupload" method="post" enctype="multipart/form-data">
    file:<input type="file" name="file"/>
    desc:<input type="text" name="desc"/>
    <input type="submit" value="submit"/>
   </form>
   <br><br>
  
  <!-- 文件的下载 -->
  <a href="testresponseentity" rel="external nofollow" >test responseentity</a>
  
  </center>
  
</body>
</html> 

success.jsp页面:显示文件上传成功

 <%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>insert title here</title>
</head>
<body>
  
  <h3>success page</h3>
  
</body>
</html>

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