配置文件中配置扫描包,以便创建各个类的bean对象

    <context:component-scan base-package="com.neuedu.spring_mvc"></context:component-scan>

一、文件的上传

  Spring MVC 上下文中默认没有为文件上传提供了直接的支持,因此默认情况下不能处理文件的上传工作

    如果想使用 Spring 的文件上传功能,需现在上下文中配置 CommonsMultipartResovler:

  1、加入jar包:
      commons-fileupload-1.3.1.jar
      commons-io-2.4.jar

    Maven项目通过在pom.xml文件中配置jar包依赖

     <dependency>
    <groupId>commons-fileupload</groupId>
3     <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
  </dependency>
  <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
10   </dependency>

  2、在SpringMVC配置文件中配置CommonsMultipartResovler

   <!-- 配置CommonsMultipartResolver,必须配置id值为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>

  3、表单的设置

    POST请求,file类型,enctype="multipart/form-data"

   <form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file"/>
描述:<input type="text" name="desc"/>
<input type="submit" value="上传"/>
</form>

  4、上传的实现

 @Controller
public class TestController {
@RequestMapping(value="/upload",method=RequestMethod.POST)
public String upload(@RequestParam(value="desc") String desc,
@RequestParam(value="file") CommonsMultipartFile file,HttpServletRequest request) throws IOException{
ServletContext context = request.getSession().getServletContext();
//获取真实路径,使用tomcat插件,默认路径为webapp下
String realPath = context.getRealPath("/upload"); //判断upload文件夹是否存在
File file1=new File(realPath);
if(!file1.exists()){
file1.mkdir();
} //文件名添加uuid,防止重复
String uuid=UUID.randomUUID().toString().replace("-", "");
String fileName=uuid+"_"+file.getOriginalFilename();
//获取输入流
InputStream in=file.getInputStream();
//获取输出流,指定输出路径及文件名
FileOutputStream out=new FileOutputStream(new File(realPath+"\\"+fileName));
IOUtils.copy(in, out);
out.close();
in.close();
return "success";
}
}

二、文件的下载

  用ResponseEntity<byte[]> 返回值完成文件下载

 @Controller
public class TestController {
@RequestMapping(value="/download")
public ResponseEntity<byte[]> download(HttpSession session) throws IOException{
byte[] body=null;
ServletContext context = session.getServletContext();
String fileName="d5b9b61dc7154f5c9df4c844348ef6df_fennu.jpg"; //获取文件路径
String filePath = context.getRealPath("/upload/"+fileName);
//读取文件内容
InputStream in=new FileInputStream(new File(filePath));
//创建文件字节数组,数组长度为文件的总大小
body=new byte[in.available()];
//将文件内容保存到字节数组中
in.read(body); //创建响应头信息的MultiValueMap
MultiValueMap<String, String> headers=new HttpHeaders();
//设置文件名重新编码,以gbk格式读取再编码为iso8859-1
fileName=new String(fileName.getBytes("gbk"), "iso8859-1");
//设置响应信息
headers.add("Content-Disposition", "attachment;filename="+fileName); HttpStatus statusCode=HttpStatus.OK;
ResponseEntity<byte[]> responseEntity=new ResponseEntity<byte[]>(body, headers, statusCode);
in.close();
return responseEntity;
}
}

基于Spring MVC的文件上传和下载功能的实现的更多相关文章

  1. Java Web 学习(8) —— Spring MVC 之文件上传与下载

    Spring MVC 之文件上传与下载 上传文件 表单: <form action="upload" enctype="multipart/form-data&qu ...

  2. Spring MVC的文件上传和下载

    简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...

  3. 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity

    文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...

  4. Spring MVC-学习笔记(5)spring MVC的文件上传、下载、拦截器

    1.文件上传.      spring MVC为文件上传提供了直接的支持,这种支持是即插即用的MultipartResolver(多部分解析器)实现的.spring MVC使用Apache Commo ...

  5. 使用Spring MVC实现文件上传与下载

    前段时间做毕业设计的时候,想要完成一个上传文件的功能,后来,虽然在自己本地搭建了一个ftp服务器,然后使用公司的工具完成了一个文档管理系统:但是还是没有找到自己想要的文件上传与下载的方式. 今天看到一 ...

  6. Spring MVC实现文件上传

    基础准备: Spring MVC为文件上传提供了直接支持,这种支持来自于MultipartResolver.Spring使用Jakarta Commons FileUpload技术实现了一个Multi ...

  7. Spring MVC的文件上传

    1.文件上传 文件上传是项目开发中常用的功能.为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data.只有在这种情况下,浏览器才会把用户 ...

  8. 【Spring学习笔记-MVC-13】Spring MVC之文件上传

    作者:ssslinppp       1. 摘要 Spring MVC为文件上传提供了最直接的支持,这种支持是通过即插即用的MultipartResolve实现的.Spring使用Jakarta Co ...

  9. spring mvc ajaxfileupload文件上传返回json下载问题

    问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...

随机推荐

  1. How to Change Master Page @ Run-time

    This tip will give complete knowledge of how to change master page, render controls and accessing it ...

  2. PL/SQL查询设计器

    被微软惯坏的我,在使用PL/SQL进行oracle多表连接查询操作时候经常挠头. 今天无意间发现了PL/SQL也有查询设计器,虽然没有sqlserver的强大好用,但足够用了. 在菜单栏 工具---& ...

  3. beijing

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<graphics.h> ...

  4. instanceof php

    instdnceof php5  的一个新成员 功能: 使用这个关键字可以确定一个对象是否是类的实例,是否是累的子类 ,还是实现了某个特定的接口. <?php class A{} class B ...

  5. box-shadow 内阴影

    1.盒子阴影样式单词:box-shadow 2.语法 代表边框间距靠左0 靠上0 和1px阴影范围 阴影颜色为黑色(#000),有inset 代表框内阴影 ,不带inset 代表框外阴影. 注意:bo ...

  6. 10-python中的requests应用

    使用request方便: #_*_ coding: utf-8 _*_ ''' Created on 2018年7月14日 @author: sss ''' import requests impor ...

  7. c语言交换两个变量的值

    有两个变量a 和b,想要交换它们的值 int a,b; 能不能这样操作呢? b=a; a=b; 不能啊,这样操作的意思是把a的值放到b中,然后b中的值已经被覆盖掉了,已经不是b原来的那个值了,所以是没 ...

  8. R list frame, matrix

    list是个筐,什么都可以往里装,包括它自身.数据框是个二维数组,同列必须同类型,行随意.

  9. python解释器的下载和安装

    1.python解释器的下载 python这样的语言,需要一个解释器.而且解释器还有多种语言的实现,我们介绍的是最常用的C语言的实现,称之为Cpython.Python通过在各种操作系统上都有各自的解 ...

  10. Entity Framework 6.0 Tutorials(6):Transaction support

    Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...