关于Jersey解决文件下载的问题
2014-09-14 22:39
239 查看
一心想着如何解决jersey下载文件的问题, 在度娘上面几乎找不到对应的资料,没办法,啃文档看源码。 看到jersey的扩展MessageBodyWriter。想着应该可以通过这个去解决。
后来又想想,jersey和springmvc同是restful的规范实现, 应该能从springMVC上借鉴,折腾半天无果, 不小心看到jersey支持File, 唉, 暗叹自己走弯路, 老想着复杂的方式去解决!
上源码
简单吧, 就几行代码!效率哇效率!
为方便其他人员测试, 把pom.xml共享给大家!
如果编辑报错, 记得增加servlet.jar包!
后来又想想,jersey和springmvc同是restful的规范实现, 应该能从springMVC上借鉴,折腾半天无果, 不小心看到jersey支持File, 唉, 暗叹自己走弯路, 老想着复杂的方式去解决!
上源码
@Path("file")
public class FileResource {
@GET
@Path("pdf/{pdf}")
@Produces("pdf/*")
public Response getPDF(@PathParam("pdf") String pdf,
@Context ServletContext application) {
String realPath = application.getRealPath("/images");
realPath = "D:/books/aa.xlsx";
File file = new File(realPath);
if (!file.exists()) {
throw new WebApplicationException(404);
}
String mt = new MimetypesFileTypeMap().getContentType(file);
return Response
.ok(file, mt)
.header("Content-disposition",
"attachment;filename=" + pdf + ".xlsx")
.header("ragma", "No-cache")
.header("Cache-Control", "no-cache").build();
}
}
简单吧, 就几行代码!效率哇效率!
为方便其他人员测试, 把pom.xml共享给大家!
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>simple-service-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-service-webapp</name>
<build>
<finalName>simple-service-webapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
</dependencies>
<properties>
<jersey.version>2.12</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
如果编辑报错, 记得增加servlet.jar包!
相关文章推荐
- 解决关于php中文文件下载问题
- 关于当前安全设置不允许下载文件问题的解决
- 解决关于php中文文件下载问题
- 今天解决了两个关于.NET读取EXCEL文件的问题,记下来,很有用!
- 解决关于在jdk1.4下处理xml文件转移到jdk1.5下出现的问题
- jsp实现文件下载与中文文件名乱码问题解决
- 关于文件下载的几个问题总结
- 关于在eclipse中解决struts中ApplicationResources.properties文件的中文支持问题
- 关于html,htm文件图标问题解决方法
- 解决 IIS 中 AIR 文件无法下载的问题
- 中文名过长导致文件下载失败问题的解决
- 关于证书文件ie中不能下载的问题
- jsp实现文件下载与中文文件名乱码问题解决
- Resin3.1.6中rar文件下载乱码的问题解决方法
- 如何解决下载的CHM文件无法显示网页问题
- 关于S40从网络下载文件的问题
- 解决在Windows 2003中无法上传超过200K文件以及无法下载超过4M的文件问题
- IIS6.0下ISO 、GHO等文件不能下载问题的解决方法
- 文件下载(解决中文文件名下载乱码问题)
- JSP实现文件下载同时解决中文乱码问题