网站充值链接怎么做,企业网站建设cms系统,wordpress里面的附件如何导出,网站建设与用户需求分析这篇文章将向您展示如何使用Spring MVC4执行文件下载#xff0c;我们将看到应用程序从文件系统内部以及外部文件下载文件。本教程的主要亮点#xff1a; 下载文件是相当简单的#xff0c;涉及以下步骤。 创建一个InputStream到文件用于下载。查找MIME类型下载文件的内容。–… 这篇文章将向您展示如何使用Spring MVC4执行文件下载我们将看到应用程序从文件系统内部以及外部文件下载文件。 本教程的主要亮点 下载文件是相当简单的涉及以下步骤。 创建一个InputStream到文件用于下载。查找MIME类型下载文件的内容。–可以是application/pdf, text/html,application/xml,image/png等等。将内容类型与上述发现的MIME类型响应HttpServletResponse。response.setContentType(mimeType);针对以上找到MIME类型设置内容长度。response.setContentLength(file.getLength());//length in bytes为响应设置内容处理标头。response.setHeader(“Content-Disposition”, “attachment; filename” fileName); //随着“附件”文件将下载。可能会显示一个“另存为”基于浏览器的设置对话框。 response.setHeader(“Content-Disposition”, “inline; filename” fileName);//通过“内联”浏览器将尝试显示内容到浏览器中图片PDF文本...。对于其他内容类型文件将直接下载。 从InputStream中复制字节响应到 OutputStream。一旦复制完成后关闭输入输出流。完整实施例在下面讨论。 使用到以下技术 Spring 4.2.0.RELEASEBootstrap v3.3.2Maven 3JDK 1.7Tomcat 8.0.21Eclipse JUNO Service Release 2现在让我们开始 项目结构 在pom.xml中声明依赖关系 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.yiibai.springmvc/groupIdartifactIdSpring4MVCFileDownloadExample/artifactIdpackagingwar/packagingversion1.0.0/versionnameSpring4MVCFileDownloadExample Maven Webapp/namepropertiesspringframework.version4.2.0.RELEASE/springframework.version/propertiesdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion${springframework.version}/version/dependencydependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion3.1.0/version/dependencydependencygroupIdjavax.servlet/groupIdartifactIdjstl/artifactIdversion1.2/version/dependency/dependenciesbuildpluginManagementpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-war-plugin/artifactIdversion2.4/versionconfigurationwarSourceDirectorysrc/main/webapp/warSourceDirectorywarNameSpring4MVCFileDownloadExample/warNamefailOnMissingWebXmlfalse/failOnMissingWebXml/configuration/plugin/plugins/pluginManagementfinalNameSpring4MVCFileDownloadExample/finalName/build
/project 创建控制器 package com.yiibai.springmvc.controller;import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.nio.charset.Charset;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;Controller
public class FileDownloadController {private static final String INTERNAL_FILEirregular-verbs-list.pdf;private static final String EXTERNAL_FILE_PATHC:/mytemp/SpringMVCHibernateManyToManyCRUDExample.zip;RequestMapping(value{/,/welcome}, method RequestMethod.GET)public String getHomePage(ModelMap model) {return welcome;}/** Download a file from * - inside project, located in resources folder.* - outside project, located in File system somewhere. */RequestMapping(value/download/{type}, method RequestMethod.GET)public void downloadFile(HttpServletResponse response, PathVariable(type) String type) throws IOException {File file null;if(type.equalsIgnoreCase(internal)){ClassLoader classloader Thread.currentThread().getContextClassLoader();file new File(classloader.getResource(INTERNAL_FILE).getFile());}else{file new File(EXTERNAL_FILE_PATH);}if(!file.exists()){String errorMessage Sorry. The file you are looking for does not exist;System.out.println(errorMessage);OutputStream outputStream response.getOutputStream();outputStream.write(errorMessage.getBytes(Charset.forName(UTF-8)));outputStream.close();return;}String mimeType URLConnection.guessContentTypeFromName(file.getName());if(mimeTypenull){System.out.println(mimetype is not detectable, will take default);mimeType application/octet-stream;}System.out.println(mimetype : mimeType);response.setContentType(mimeType);/* Content-Disposition : inline will show viewable types [like images/text/pdf/anything viewable by browser] right on browser while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/response.setHeader(Content-Disposition, String.format(inline; filename\ file.getName() \));/* Content-Disposition : attachment will be directly download, may provide save as popup, based on your browser setting*///response.setHeader(Content-Disposition, String.format(attachment; filename\%s\, file.getName()));response.setContentLength((int)file.length());InputStream inputStream new BufferedInputStream(new FileInputStream(file));//Copy bytes from source to destination(outputstream in this example), closes both streams.FileCopyUtils.copy(inputStream, response.getOutputStream());}}该控制器包括两个文件。一个文件是内部应用内部资源和其他文件位于外部的应用程序的文件系统。您的项目一定要改变外部文件的路径。仅用于演示的目的我们已在路径一个额外的路径变量内部/外部。我们正在使用Spring FileCopyUtils工具类流从源复制到目的地。 配置 package com.yiibai.springmvc.configuration;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;Configuration
EnableWebMvc
ComponentScan(basePackages com.yiibai.springmvc)
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter{Overridepublic void configureViewResolvers(ViewResolverRegistry registry) {InternalResourceViewResolver viewResolver new InternalResourceViewResolver();viewResolver.setViewClass(JstlView.class);viewResolver.setPrefix(/WEB-INF/views/);viewResolver.setSuffix(.jsp);registry.viewResolver(viewResolver);}Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler(/static/**).addResourceLocations(/static/);}}初始化 package com.yiibai.springmvc.configuration;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {Overrideprotected Class?[] getRootConfigClasses() {return new Class[] { HelloWorldConfiguration.class };}Overrideprotected Class?[] getServletConfigClasses() {return null;}Overrideprotected String[] getServletMappings() {return new String[] { / };}}添加视图 % taglib prefixform urihttp://www.springframework.org/tags/form%
% taglib prefixc urihttp://java.sun.com/jsp/jstl/core %
htmlheadmeta http-equivContent-Type contenttext/html; charsetISO-8859-1titleSpring 4 MVC File Download Example/titlelink hrefc:url value/static/css/bootstrap.css / relstylesheet/linklink hrefc:url value/static/css/app.css / relstylesheet/link
/head
bodydiv classform-containerh1Welcome to FileDownloader Example/h1Click on below links to see FileDownload in action.br/br/a hrefc:url value/download/internal /Download This File (located inside project)/a br/a hrefc:url value/download/external /Download This File (located outside project, on file system)/a/div
/body
/html构建部署和运行应用程序 现在构建war在前面的Eclipse教程或通过Maven的命令行( mvn clean install)。部署 war 到Servlet3.0容器。或 打开浏览器浏览 http://localhost:8080/Spring4MVCFileDownloadExample 点击第二个链接。外部文件应被下载。 点击第一个链接。内部文件[这是一个PDF]应该显示在浏览器中这是由于 Content-Disposition: inline. 通过内联如果内容可以通过浏览器显示它会显示它在浏览器中。 现在从内联更改内容处置备注。构建并部署。点击第一个链接。这个时候您应该看到 PDF文件被下载。 就这样完成 下载代码http://pan.baidu.com/s/1c1lmeL6转载于:https://www.cnblogs.com/jxldjsn/p/5671582.html