项目名称: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