文件上传与下载的简单案例

文件上传

同时构建这个controller的过程中遇到最大的一个bug是:

扫描二维码关注公众号,回复: 4308270 查看本文章

得构建一个视图资源管理器,因为controller中必须得认识ModelAndView,String类型

没有ModelAndView对象,不知道如何去找视图,会自动给你默认的视图/upload,最后会使用视图解析器帮我们返回的地址添加前缀信息和后缀信息,/WEB-INF/upload.jsp.

配置视图解析器:(可以再spring-webmvc-4.1.2.RELEASE-sources.jar/org/springframework/web/servlet/DispatcherServlet.properties中的org.springframework.web.servlet.ViewResolver)

文件下载

文件具体格式

具体代码:

appilication.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:p="http://www.springframework.org/schema/p"

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

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"

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

xsi:schemaLocation="

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee.xsd

http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd

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

<context:component-scan base-package="uploadfile"></context:component-scan>

<mvc:annotation-driven></mvc:annotation-driven>

<mvc:default-servlet-handler></mvc:default-servlet-handler>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="maxUploadSize" value="1048576"></property>

</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="suffix" value=".jsp"></property>

<property name="prefix" value="/WEB-INF/"></property>

</bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

<servlet>

<servlet-name>uploadfile</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:application.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>uploadfile</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

uploadfile.jsp

<%--

Created by IntelliJ IDEA.

User: Lenovo

Date: 2018/11/23

Time: 13:05

To change this template use File | Settings | File Templates.

--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

<title>Title</title>

</head>

<body>

<fieldset>

<legend>文件上传</legend>

<form enctype="multipart/form-data" method="post" action="/upload">

<input type="file" name="file"/><br><br>

<input type="submit" name="btn" value="文件上传">

</form>

</fieldset>

</body>

</html>

upload控制器

package uploadfile;

import org.apache.commons.io.IOUtils;

import org.springframework.stereotype.Controller;

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

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;

import java.io.*;

@Controller

public class upload {

@RequestMapping("/upload")

public void upload(MultipartFile file){

InputStream is=null;

OutputStream os=null;

try {

is=file.getInputStream();

os=new FileOutputStream(new File("D:/b.jsp"));

IOUtils.copy(is,os);

}catch (Exception e){

e.printStackTrace();

}finally {

if(is!=null){

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}finally {

if(os!=null){

try {

os.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

}

@RequestMapping("/download")

public void download(HttpServletResponse resp){

FileInputStream is=null;

try {

resp.setHeader("Content-Disposition","attachment;filename=a.jpg");

is=new FileInputStream(new File("D:/a.jpg"));

ServletOutputStream os=resp.getOutputStream();

IOUtils.copy(is,os);

}catch (Exception e){

e.printStackTrace();

}finally {

if(is!=null){

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

};

}

}

}

运行结果为:

猜你喜欢

转载自blog.csdn.net/zhouzhou_98/article/details/84398126