springMVC配置环境实现文件上传和下载

网友投稿 215 2023-07-16


springMVC配置环境实现文件上传和下载

最近的项目中用到了文件的上传和下载功能,我觉着这个功能比较重要,因此特意把它提取出来自己进行了尝试。

下面就是springMVC配置环境实现文件上传和下载的具体步骤,供大家参考,具体内容如下

一、 基础配置:

maven导包及配置pom.xml,导包时除开springmvc的基础依赖外,需要导入文件上传下载时用到的commons-io.jsr和commons-fileupload.jar:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

filLoadTest

filLoadTest

war

0.0.1-SNAPSHOT

filLoadTest Maven Webapp

http://maven.apache.org

filLoadTest

maven-compiler-plugin

2.3.2

1.7

1.7

UTF-8

${java.home}\lib\rt.jar

junit

junit

3.8.1

test

org.springframework

spring-webmvc

4.0.6.RELEASE

com.fasterxml.jackson.core

jackson-annotations

2.2.3

com.fasterxml.jackson.core

jackson-core

2.2.3

com.fasterxml.jackson.core

jackson-databind

2.2.3

commons-fileupload

commons-fileupload

1.3.1

commons-io

commons-io

2.4

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

filLoadTest

filLoadTest

war

0.0.1-SNAPSHOT

filLoadTest Maven Webapp

http://maven.apache.org

filLoadTest

maven-compiler-plugin

2.3.2

1.7

1.7

UTF-8

${java.home}\lib\rt.jar

junit

junit

3.8.1

test

org.springframework

spring-webmvc

4.0.6.RELEASE

com.fasterxml.jackson.core

jackson-annotations

2.2.3

com.fasterxml.jackson.core

jackson-core

2.2.3

com.fasterxml.jackson.core

jackson-databind

2.2.3

commons-fileupload

commons-fileupload

1.3.1

commons-io

commons-io

2.4

web.xml基础配置:

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">

字符集过滤器

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

字符集编码

encoding

UTF-8

encodingFilter

/*

springMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring.xml

springMVC

*.do

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

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">

字符集过滤器

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

字符集编码

encoding

UTF-8

encodingFilter

/*

springMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring.xml

springMVC

*.do

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

spring基础配置spring.xml:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://wwwhttp://.springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd">

class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

text/html;charset=utf-8

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://wwwhttp://.springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd">

class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

text/html;charset=utf-8

class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

text/html;charset=utf-8

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >

class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >

二、文件上传的代码

上传的简单页面index.html,要注意form表单提交时的entype的设置:

选择文件:

上传对应的后台java代码,具体问题见注释:

package controllers;

import java.io.File;

import java.io.IOException;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpStatus;

import org.springframework.http.MediaType;

import org.springframework.http.ResponseEntity;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.multipart.MultipartHttpServletRequest;

import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@Controller

public class fileController {

/**

* 文件上传

*

* @author:tuzongxun

* @Title: upLoadFile

* @param @param file

* @param @param request

* @param @throws IllegalStateException

* @param @throws IOException

* @return void

* @date Apr 28, 2016 1:22:00 PM

* @throws

*/

@RequestMapping(value = "/upLoadFile.do", method = RequestMethod.POST)

public void upLoadFile(HttpServletRequest request)

throws IllegalStateException, IOException {

// @RequestParam("file") MultipartFile file,

CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(

request.getSession().getServletContext());

// 判断 request 是否有文件上传,即多部分请求

if (multipartResolver.isMultipart(request)) {

// 转换成多部分request

MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;

// 取得request中的所有文件名

Iterator iter = multiRequest.getFileNames();

while (iter.hasNext()) {

// 取得上传文件

MultipartFile f = multiRequest.getFile(iter.next());

if (f != null) {

// 取得当前上传文件的文件名称

String myFileName = f.getOriginalFilename();

// 如果名称不为“”,说明该文件存在,否则说明该文件不存在

if (myFileName.trim() != "") {

// 定义上传路径

String path = "C:\\Users\\tuzongxun123\\Desktop\\"

+ myFileName;

File localFile = new File(path);

f.transferTo(localFile);

}

}

}

}

}

}

当选择文件提交后,便会看到选中的文件被传到了代码中指定的位置,页面效果图如下

三、文件下载

文件下载需要获取下载文件的存储路径,这里只是手动填入,如果是在具体项目中,可以把文件名和上传后的存储路径保存在数据库中。然后增加一个文件列表的页面展示文件名和文件路径,然后点击下载的时候把相应的文件名和路径传到后台操作。

/**

* 文件下载,需要文件名和文件地址

*

* @author:tuzongxun

* @Title: download

* @param@param name

* @param@param path

* @param@return

* @param@throws IOException

* @returnResponseEntity

* @date Apr 28,2016 1:21:32 PM

* @throws

*/

@RequestMapping(value = "/downLoadFile.do")

public ResponseEntity download(@RequestParam("name") String name,

@RequestParam("filePath") String path) throws IOException {

File file = new File(path);

HttpHeaders headers = new HttpHeaders();

String fileName = new String(name.getBytes("UTF-8"), "iso-8859-1");// 为了解决中文名称乱码问题

headers.setContentDispositionFormData("attachment", fileName);

headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

return new ResponseEntity(FileUtils.readFileToByteArray(file),

headers, HttpStatus.CREATED);

}

Html页面,这里只是简单的填写文件名和文件路径,用form表单提交到后台,然后后台会控制response在页面弹出保存文件路径的选择框,当然了,这里的后台我没有做什么处理,如果文件不存在是会报错的,可以进行相应的处理:

文件下载:

文件名:

文件路径:

页面视图如下:

如果文件不存在,报错如下:

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java中Calendar时间操作常用方法详解
下一篇:Java图片上传实现代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~