• 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

  • 相关阅读:
    SpringBoot学习历程
    日期和时间库Joda Time
    apache commons validator
    apache commons fileupload
    apache commons io
    apache commons compress
    apache commons codec
    apache commons email
    Http协议介绍
    Java原生Socket API
  • 原文地址:https://www.cnblogs.com/MedivhQ/p/3801390.html
Copyright © 2020-2023  润新知