SpringMVC里的文件下载
* ServletAPI response
* 下载的实现 将服务器的文件写到客户端
* reponse对象可以实现文件下载
* HttpMessageConverter下实现下载
* 自定义视图的方式实现文件下载
response下载文件
@RequestMapping("/download")
@ResponseBody
public void download(Model model, HttpServletRequest request, HttpServletResponse
response) throws IOException {
String apath = request.getSession().getServletContext().getRealPath("/a.txt");
System.out.println(apath);
response.setContentType("text/plain;charset=UTF-8");
//MIME换取下载头信息什么类型文件
//request.getServletContext().getMimeType("a.jsp");
response.addHeader("Content-Disposition", "attachment;filename=a.txt");
FileInputStream fileInputStream = new FileInputStream(apath);
ServletOutputStream outputStream = response.getOutputStream();
int c = fileInputStream.read();
while (c != -1) {
outputStream.write(c);
c = fileInputStream.read();
}
}
实现VIew接口 下载
Controller
@RequestMapping("/downloadview")
public String view(Model model) {
model.addAttribute("filename", "a.txt");
return "txtView";
}
View接口的实现
package pojo;
import java.io.FileInputStream;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.View;
@Component
public class TxtView implements View {
@Override
public String getContentType() {
return "text/plain";
}
@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
String apath = request.getSession().getServletContext().getRealPath("/" + model.get("filename"));
System.out.println(apath);
response.addHeader("Content-Disposition", "attachment;filename=" + model.get("filename"));
FileInputStream fileInputStream = new FileInputStream(apath);
ServletOutputStream outputStream = response.getOutputStream();
int c = fileInputStream.read();
while (c != -1) {
outputStream.write(c);
c = fileInputStream.read();
}
}
}
配置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.3.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.3.xsd">
<context:component-scan base-package="controller,pojo"></context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="viewResolver1" class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="1"></property>
</bean>
<mvc:annotation-driven validator="validator">
<!-- <mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="FastjsonHttpMessageConverter"/>
</mvc:message-converters> -->
</mvc:annotation-driven>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
<property name="validationMessageSource" ref="messageSource"></property>
</bean>
<!-- <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> -->
<!-- <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="pojo.StringToUserConverter"/>
</set>
</property>
</bean> -->
<!--
第一种方式:
/js/vue.js
/js/jquery.min.js
/js/vue/abc.js
<mvc:resources location="/js/" mapping="/resource/**"></mvc:resources>
-->
<!--
第二种方式:
-->
<mvc:default-servlet-handler default-servlet-name="default"/>
<!-- <mvc:view-controller path="/input" view-name="input"/> -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- basename:包名+文件的前缀 -->
<!-- <property name="basename" value="i18n.input"></property> -->
<property name="basenames">
<array>
<value>i18n.input</value>
</array>
</property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<bean id="methodVPP" class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
<property name="validator" ref="validator"></property>
</bean>
<!-- 文件上传解释器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="#{10*1024*1024}"></property>
</bean>
</beans>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="viewResolver1" class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="1"></property>
</bean>
springmvc默认使用第一种视图解析器 org.springframework.web.servlet.view.InternalResourceViewResolver
springmvc可以定义多个视图解析器 通过追踪源码可以发现 DispatcherServlet调用哪个视图解析器的原理是 通过遍历的方式查询视图解析器当发现有视图解析器的时候就立即返回 所以我们应该把org.springframework.web.servlet.view.BeanNameViewResolver这个视图解析器首先被加载进去 所以设置<property name="order" value="1"></property>即可实现
推荐使用第二种方式