zoukankan      html  css  js  c++  java
  • jersey实现文件下载示例

    项目名称:jersey,路径前缀:jersey

    只通过一个类即可实现该示例:

    <span style="font-size:14px;">package com.medivh.control;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.MediaType;
    
    import org.springframework.stereotype.Component;
    @Component
    @Path("/down")
    public class DownService
    {
    	    @GET
    	    @Path("/self")
    	    @Produces(MediaType.APPLICATION_OCTET_STREAM)//返回方式为流
    	    public byte[] getStudentJl( @Context HttpServletRequest request,@Context HttpServletResponse response) 
    	    {
    				FileInputStream fis = null;
    				try
    				{
    					fis = new FileInputStream(new File("d:\html.pdf"));
    					byte[] b = new byte[fis.available()];
    					fis.read(b);
    					response.setHeader("Content-Disposition","attachment;filename=report.pdf");//为文件命名
    					response.addHeader("content-type","application/pdf");
    					return b;
    				}
    				catch (FileNotFoundException e) 
    				{
    					e.printStackTrace();
    					return null;
    				} 
    				catch (IOException e) 
    				{
    					e.printStackTrace();
    					return null;
    				}
    	    }
    }
    </span>
    web测试服务器为tomcat端口80;
    浏览器输入url:http://localhost/jersey/jersey/down/self  即可完成下载。

    源代码0分下载地址:http://download.csdn.net/detail/wow4464/7401715

  • 相关阅读:
    xadmin修改list_export_fields不生效以及添加exclude_export_fields功能
    Python正则总结
    Ubuntu安装Gitlab简记
    pytest使用总结笔记
    unittest使用数据驱动ddt
    unittest使用总结
    HTTP知识点总结
    HTTPS加密流程理解
    Requests库使用总结
    leetcode 76:最小字符串
  • 原文地址:https://www.cnblogs.com/MedivhQ/p/4074953.html
Copyright © 2011-2022 走看看