亲身实测可用的java实现wordxlsxpdf文件下载功能

本文参考原文-http://bjbsair.com/2020-03-22/tech-info/3621.html
在SpringMVC的开发过程中,有时需要实现文档的下载功能。word,excel,pdf作为大家最常用的办公程序,它的文件传输就显得尤为重要,本文通过使用spring 提供的Resource封装来实现实现word/xlsx/pdf文件下载功能。话不多说。

亲身实测可用的java实现word/xlsx/pdf文件下载功能

亲身实测可用的java实现word/xlsx/pdf文件下载功能

package com.davidwang456;  
  
import java.io.File;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.springframework.core.io.FileSystemResource;  
import org.springframework.core.io.Resource;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.bind.annotation.ResponseBody;  
import org.springframework.web.bind.annotation.RestController;  
@RestController  
public class DownloadController {  
	private static final String APPLICATION_PDF="application/pfd";  
	private static final String APPLICATION_WORD="application/msword";  
	private static final String APPLICATION_EXCEL="application/vnd.ms-excel;charset=utf-8";	  
	@RequestMapping(value="/download",method=RequestMethod.GET,produces=APPLICATION_PDF)  
	@ResponseBody  
	public Resource download(HttpServletRequest req,HttpServletResponse resp) {  
		String file);  
		File file=getFile(fileName);  
		String suffix=fileName.substring(fileName.lastIndexOf(".")+1);  
		if(suffix.equalsIgnoreCase(APPLICATION_PDF)) {  
			resp.setContentType(APPLICATION_PDF);  
			resp.setHeader("Content-Disposition", "inline;filename="+file.getName());  
		}else if(suffix.equalsIgnoreCase(APPLICATION_WORD)) {  
			resp.setContentType(APPLICATION_WORD);  
			resp.setHeader("Content-Disposition", "attachment;filename="+file.getName());  
		}else if(suffix.equalsIgnoreCase(APPLICATION_EXCEL)) {  
			resp.setContentType(APPLICATION_EXCEL);  
			resp.setHeader("Content-Disposition", "attachment;filename="+file.getName());  
		}	  
		resp.setHeader("Content-Length", String.valueOf(file.length()));  
		return new FileSystemResource(file);  
	}  
	private static File getFile(String fileName) {  
		File file=new File( "path"+fileName);  
		if(!file.exists()) {  
			System.out.println("指定路径下的文件不存在!");  
		}  
		return file;  
	}  
}
```本文参考原文-http://bjbsair.com/2020-03-22/tech-info/3621/
在SpringMVC的开发过程中,有时需要实现文档的下载功能。word,excel,pdf作为大家最常用的办公程序,它的文件传输就显得尤为重要,本文通过使用spring 提供的Resource封装来实现实现word/xlsx/pdf文件下载功能。话不多说。

![亲身实测可用的java实现word/xlsx/pdf文件下载功能](http://p9.pstatp.com/large/pgc-image/e80ae01c55d74d2384de2bb0c4f1fcd9)

![亲身实测可用的java实现word/xlsx/pdf文件下载功能](http://p3.pstatp.com/large/pgc-image/46d9badf9ec5422eaa7aca78d5c8b0f8)

package com.davidwang456;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DownloadController {
private static final String APPLICATION_PDF="application/pfd";
private static final String APPLICATION_WORD="application/msword";
private static final String APPLICATION_EXCEL="application/vnd.ms-excel;charset=utf-8";
@RequestMapping(value="/download",method=RequestMethod.GET,produces=APPLICATION_PDF)
@ResponseBody
public Resource download(HttpServletRequest req,HttpServletResponse resp) {
String file);
File file=getFile(fileName);
String suffix=fileName.substring(fileName.lastIndexOf(".")+1);
if(suffix.equalsIgnoreCase(APPLICATION_PDF)) {
resp.setContentType(APPLICATION_PDF);
resp.setHeader("Content-Disposition", "inline;filename="+file.getName());
}else if(suffix.equalsIgnoreCase(APPLICATION_WORD)) {
resp.setContentType(APPLICATION_WORD);
resp.setHeader("Content-Disposition", "attachment;filename="+file.getName());
}else if(suffix.equalsIgnoreCase(APPLICATION_EXCEL)) {
resp.setContentType(APPLICATION_EXCEL);
resp.setHeader("Content-Disposition", "attachment;filename="+file.getName());
}
resp.setHeader("Content-Length", String.valueOf(file.length()));
return new FileSystemResource(file);
}
private static File getFile(String fileName) {
File file=new File( "path"+fileName);
if(!file.exists()) {
System.out.println("指定路径下的文件不存在!");
}
return file;
}
}

在SpringMVC的开发过程中,有时需要实现文档的下载功能。word,excel,pdf作为大家最常用的办公程序,它的文件传输就显得尤为重要,本文通过使用spring 提供的Resource封装来实现实现word/xlsx/pdf文件下载功能。话不多说。

![亲身实测可用的java实现word/xlsx/pdf文件下载功能](http://p9.pstatp.com/large/pgc-image/e80ae01c55d74d2384de2bb0c4f1fcd9)

![亲身实测可用的java实现word/xlsx/pdf文件下载功能](http://p3.pstatp.com/large/pgc-image/46d9badf9ec5422eaa7aca78d5c8b0f8)

package com.davidwang456;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DownloadController {
private static final String APPLICATION_PDF="application/pfd";
private static final String APPLICATION_WORD="application/msword";
private static final String APPLICATION_EXCEL="application/vnd.ms-excel;charset=utf-8";
@RequestMapping(value="/download",method=RequestMethod.GET,produces=APPLICATION_PDF)
@ResponseBody
public Resource download(HttpServletRequest req,HttpServletResponse resp) {
String file);
File file=getFile(fileName);
String suffix=fileName.substring(fileName.lastIndexOf(".")+1);
if(suffix.equalsIgnoreCase(APPLICATION_PDF)) {
resp.setContentType(APPLICATION_PDF);
resp.setHeader("Content-Disposition", "inline;filename="+file.getName());
}else if(suffix.equalsIgnoreCase(APPLICATION_WORD)) {
resp.setContentType(APPLICATION_WORD);
resp.setHeader("Content-Disposition", "attachment;filename="+file.getName());
}else if(suffix.equalsIgnoreCase(APPLICATION_EXCEL)) {
resp.setContentType(APPLICATION_EXCEL);
resp.setHeader("Content-Disposition", "attachment;filename="+file.getName());
}
resp.setHeader("Content-Length", String.valueOf(file.length()));
return new FileSystemResource(file);
}
private static File getFile(String fileName) {
File file=new File( "path"+fileName);
if(!file.exists()) {
System.out.println("指定路径下的文件不存在!");
}
return file;
}
}

在SpringMVC的开发过程中,有时需要实现文档的下载功能。word,excel,pdf作为大家最常用的办公程序,它的文件传输就显得尤为重要,本文通过使用spring 提供的Resource封装来实现实现word/xlsx/pdf文件下载功能。话不多说。

![亲身实测可用的java实现word/xlsx/pdf文件下载功能](http://p9.pstatp.com/large/pgc-image/e80ae01c55d74d2384de2bb0c4f1fcd9)

![亲身实测可用的java实现word/xlsx/pdf文件下载功能](http://p3.pstatp.com/large/pgc-image/46d9badf9ec5422eaa7aca78d5c8b0f8)

package com.davidwang456;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DownloadController {
private static final String APPLICATION_PDF="application/pfd";
private static final String APPLICATION_WORD="application/msword";
private static final String APPLICATION_EXCEL="application/vnd.ms-excel;charset=utf-8";
@RequestMapping(value="/download",method=RequestMethod.GET,produces=APPLICATION_PDF)
@ResponseBody
public Resource download(HttpServletRequest req,HttpServletResponse resp) {
String file);
File file=getFile(fileName);
String suffix=fileName.substring(fileName.lastIndexOf(".")+1);
if(suffix.equalsIgnoreCase(APPLICATION_PDF)) {
resp.setContentType(APPLICATION_PDF);
resp.setHeader("Content-Disposition", "inline;filename="+file.getName());
}else if(suffix.equalsIgnoreCase(APPLICATION_WORD)) {
resp.setContentType(APPLICATION_WORD);
resp.setHeader("Content-Disposition", "attachment;filename="+file.getName());
}else if(suffix.equalsIgnoreCase(APPLICATION_EXCEL)) {
resp.setContentType(APPLICATION_EXCEL);
resp.setHeader("Content-Disposition", "attachment;filename="+file.getName());
}
resp.setHeader("Content-Length", String.valueOf(file.length()));
return new FileSystemResource(file);
}
private static File getFile(String fileName) {
File file=new File( "path"+fileName);
if(!file.exists()) {
System.out.println("指定路径下的文件不存在!");
}
return file;
}
}

在SpringMVC的开发过程中,有时需要实现文档的下载功能。word,excel,pdf作为大家最常用的办公程序,它的文件传输就显得尤为重要,本文通过使用spring 提供的Resource封装来实现实现word/xlsx/pdf文件下载功能。话不多说。

![亲身实测可用的java实现word/xlsx/pdf文件下载功能](http://p9.pstatp.com/large/pgc-image/e80ae01c55d74d2384de2bb0c4f1fcd9)

![亲身实测可用的java实现word/xlsx/pdf文件下载功能](http://p3.pstatp.com/large/pgc-image/46d9badf9ec5422eaa7aca78d5c8b0f8)

package com.davidwang456;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DownloadController {
private static final String APPLICATION_PDF="application/pfd";
private static final String APPLICATION_WORD="application/msword";
private static final String APPLICATION_EXCEL="application/vnd.ms-excel;charset=utf-8";
@RequestMapping(value="/download",method=RequestMethod.GET,produces=APPLICATION_PDF)
@ResponseBody
public Resource download(HttpServletRequest req,HttpServletResponse resp) {
String file);
File file=getFile(fileName);
String suffix=fileName.substring(fileName.lastIndexOf(".")+1);
if(suffix.equalsIgnoreCase(APPLICATION_PDF)) {
resp.setContentType(APPLICATION_PDF);
resp.setHeader("Content-Disposition", "inline;filename="+file.getName());
}else if(suffix.equalsIgnoreCase(APPLICATION_WORD)) {
resp.setContentType(APPLICATION_WORD);
resp.setHeader("Content-Disposition", "attachment;filename="+file.getName());
}else if(suffix.equalsIgnoreCase(APPLICATION_EXCEL)) {
resp.setContentType(APPLICATION_EXCEL);
resp.setHeader("Content-Disposition", "attachment;filename="+file.getName());
}
resp.setHeader("Content-Length", String.valueOf(file.length()));
return new FileSystemResource(file);
}
private static File getFile(String fileName) {
File file=new File( "path"+fileName);
if(!file.exists()) {
System.out.println("指定路径下的文件不存在!");
}
return file;
}
}

原文地址:https://www.cnblogs.com/lihanlin/p/12556006.html