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

  • 相关阅读:
    HDU-3622 Bomb Game 2sat
    HDU-4115 Eliminate the Conflict 2sat
    POJ-3678 Katu Puzzle 2sat
    [转]2-SAT问题及其算法
    Ros学习——roslaunch
    Ros学习调试——rqt_console
    Ros学习——创建程序包
    Ros学习service——小海龟
    Ros学习topic——小海龟
    旋转矩阵相关变换
  • 原文地址:https://www.cnblogs.com/MedivhQ/p/4074953.html
Copyright © 2011-2022 走看看