zoukankan      html  css  js  c++  java
  • 亲身实测可用的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;
    }
    }

  • 相关阅读:
    php操作zip压缩文件
    php图像处理(thinkphp框架有相对强大的图像处理功能)
    php实现希尔排序(总结)
    PHP glob() 函数详解
    php资源类型变量
    dump var_dump print print_r的区别
    php实现遍历文件目录
    php常用函数
    php全局变量的使用
    php函数按地址传递参数(php引用)
  • 原文地址:https://www.cnblogs.com/lihanlin/p/12556006.html
Copyright © 2011-2022 走看看