用maven创建基于wink的rest服务-下载zip文件的post请求(五)
来源:互联网 发布:mac上截取视频的软件 编辑:程序博客网 时间:2024/05/22 03:09
Apache Wink是一个用于帮助开发者快速高效的开发RESTful Web services服务的java框架,他包括一个服务器端模块和一个客户端模块.
1.创建基于wink的web服务.
重点:通过如下两行代码可以返回给客户端一个流文件.
ResponseBuilder responseBuilder = Response.ok(fis, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Response response = responseBuilder.header("test", "test").build();
4.在java客户端,其中contentType("application/xml;charset=UTF-8").accept("application/xml;charset=UTF-8")分别代表客户端发送的请求
媒体类型和可接受请求的媒体类型.
重点:通过如下两行代码可以获取服务器返回流文件的头信息和流文件.
Map<String, List<String>> header = response.getHeaders();
InputStream is = response.getEntity(new EntityType<InputStream>(){});
1.创建基于wink的web服务.
a.用maven构建rest服务器端项目,直接看代码pom.xml.
<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.ilucky.rest</groupId><artifactId>rest-server</artifactId><version>1.0.0</version><packaging>war</packaging><dependencies><dependency><groupId>org.apache.wink</groupId><artifactId>wink-server</artifactId><version>1.3.0</version></dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.3</version> </dependency></dependencies></project></span>b.将cmd窗口切换到rest-server目录下执行mvn eclipse:myeclipse-clean eclipse:myeclipse命令下载pom.xml文件中配置的类库.
c.在web.xml中将所有rest服务映射到RestServerApplication中,rest通过Application管理所有服务,直接看代码RestServerApplication.
<span style="font-size:12px;">package com.iluck.rest.server;import java.util.HashSet;import java.util.Set;import javax.ws.rs.core.Application;/** * @author IluckySi * @date 20140802 */public class RestServerApplication extends Application {private Set<Object> svc_singletons = new HashSet<Object>();private Set<Class<?>> svc_classes = new HashSet<Class<?>>();public RestServerApplication() {svc_singletons.add(RestServerServiceImpl.getInstance());}@Overridepublic Set<Object> getSingletons() {return svc_singletons;}@Overridepublic Set<Class<?>> getClasses() {return svc_classes;}}</span>
d.创建rest服务,直接看代码RestServerService.
<span style="font-size:12px;">package com.iluck.rest.server;import javax.ws.rs.Consumes;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.Response;/** * @author IluckySi * @date 20140802 */@Path("/download")public interface RestServerService {@POST@Produces("*;charset=UTF-8")@Consumes("*;charset=UTF-8")public Response download(String userInfo);}</span>
<span style="font-size:12px;">package com.iluck.rest.server;import java.io.File;import java.io.FileInputStream;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import javax.ws.rs.core.Response.ResponseBuilder;/** * @author IluckySi * @date 20140802 */public class RestServerServiceImpl implements RestServerService {private static RestServerServiceImpl instance = null;;public synchronized static RestServerServiceImpl getInstance() {if(instance == null) {instance = new RestServerServiceImpl();}return instance;}public Response download(String message) {System.out.println("rest服务接收指令:" + message);Response response = null;FileInputStream fis = null;try {File file = new File("E:/sdx.zip");fis = new FileInputStream(file);ResponseBuilder responseBuilder = Response.ok(fis, MediaType.APPLICATION_OCTET_STREAM_TYPE); response = responseBuilder.header("test", "test").build();} catch (Exception e) {System.out.println("下载文件失败!");} return response;}}/**输出结果:rest服务接收指令:download*/</span>2.创建基于wink的java客户端.
a.用maven构建rest客户端项目,直接看代码pom.xml.
<span style="font-size:12px;"><?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.ilucky.rest</groupId><artifactId>rest-client</artifactId><version>1.0.0</version><dependencies><dependency><groupId>org.apache.wink</groupId><artifactId>wink-client</artifactId><version>1.3.0</version></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.2.4</version></dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.3</version> </dependency> </dependencies></project></span>b.将cmd窗口切换到rest-client目录下执行mvn eclipse:myeclipse-clean eclipse:myeclipse命令下载pom.xml文件中配置的类库.
c.创建rest客户端,直接看代码RestClientService
.
<span style="font-size:12px;">package com.ilucky.rest.client;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.util.List;import java.util.Map;import org.apache.wink.client.ClientResponse;import org.apache.wink.client.EntityType;import org.apache.wink.client.Resource;import org.apache.wink.client.RestClient;/** * @author IluckySi * @date 20140802 */public class RestClientService {public static void main(String[] args) {//创建rest客户端.RestClient restClient = new RestClient(); Resource resource = restClient.resource("http://127.0.0.1:8080/rest-server/rest/download");final ClientResponse response = resource.post("download"); //接收服务器返回媒体的头信息.Map<String, List<String>> header = response.getHeaders();System.out.println("服务器返回状态: " + response.getStatusCode());System.out.println("服务器返回文件头信息-测试: " + header.get("test"));System.out.println("服务器返回文件头信息-返回大小: " + header.get("Content-Length"));System.out.println("服务器返回文件头信息: " + header);//接收服务器返回媒体流.InputStream is = response.getEntity(new EntityType<InputStream>(){});//将流写入指定文件.save(is);}public static void save(InputStream is) {try {File file = new File("E:/temp/ilucky.zip");if(!file.exists()) {file.createNewFile();} else {return;}int buffer = 10240;BufferedInputStream bis = null;FileOutputStream fos = null;BufferedOutputStream bos = null;try {bis = new BufferedInputStream(is);fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos, buffer);byte[] buf = new byte[buffer];int length = 0;while ((length = bis.read(buf, 0, buffer)) != -1) {bos.write(buf, 0, length);bos.flush();}} catch (Exception e) {e.printStackTrace();} finally {try { if(bos != null) { bos.close(); bos = null; } if(fos != null) { fos.close(); fos = null; } if(bis != null) { bis.close(); bis = null; } if(is != null) { is.close(); is = null; } } catch (Exception e) { System.out.println("关闭流发生问题!"); }}} catch (Exception e) {System.out.println("下载文件发生问题!");}}}/**输出结果:服务器返回状态: 200服务器返回文件头信息-测试: [test]服务器返回文件头信息-返回大小: [672]服务器返回文件头信息: CaseInsensitiveMultivaluedMap [map=[null=HTTP/1.1 200 OK,Content-Length=672,Content-Type=application/octet-stream,Date=Sat, 02 Aug 2014 07:07:01 GMT,Server=Apache-Coyote/1.1,test=test]]*/</span>3.在web服务端,其中@Produces("*;charset=UTF-8")和@Consumes("*;charset=UTF-8")分别代表服务返回的媒体类型和可接受请求的媒体类型.
重点:通过如下两行代码可以返回给客户端一个流文件.
ResponseBuilder responseBuilder = Response.ok(fis, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Response response = responseBuilder.header("test", "test").build();
4.在java客户端,其中contentType("application/xml;charset=UTF-8").accept("application/xml;charset=UTF-8")分别代表客户端发送的请求
媒体类型和可接受请求的媒体类型.
重点:通过如下两行代码可以获取服务器返回流文件的头信息和流文件.
Map<String, List<String>> header = response.getHeaders();
InputStream is = response.getEntity(new EntityType<InputStream>(){});
0 0
- 用maven创建基于wink的rest服务-下载zip文件的post请求(五)
- 用maven创建基于wink的rest服务(三)-携带纯文本文件的post请求
- 用maven创建基于wink的rest服务(四)-携带xml和gson的post请求
- 用maven创建基于wink的rest服务(二)-携带参数的get请求
- 用maven创建基于wink的rest服务(一)-get请求
- 用maven创建基于wink的rest服务-传送文件(字符串和字节数组)(一)
- Spring中应用Apache Wink创建REST服务
- 利用Maven创建基于原型项目的Java REST项目
- 基于REST的Web服务
- 基于WCF的REST服务
- 基于 REST 的 Web 服务
- 基于REST的Web服务
- 基于 REST 的 Web 服务
- 基于REST的Web服务
- 基于 REST 的 Web 服务
- 基于REST的Web服务
- 基于 REST 的 Web 服务
- 基于HTTP 协议的GET和POST请求服务
- 补缴2个月社保折腾1年多 刁钻政策玩死人
- 在Linux下生成GUID的程序,及编译错误 “uuid/uuid.h: No such file or directory” 的解决办法。
- Codeforces 453 A. Little Pony and Expected Maximum
- hdu1702 队列与栈
- 浙大超厉害计算机硕士生导师
- 用maven创建基于wink的rest服务-下载zip文件的post请求(五)
- Go语言-Windows开发环境搭建
- java 字符串替换处理方法
- Google搜索技巧
- 对于一个数字,我们可以直观的了解到它所含有的数字,例如: 3243中包含1个2、2个3、1个4。 那么,给你一个数n,求从1到n之间的所有这些数字中,总共包含多少个m。
- Android: 文件操作大全
- Google高级搜索技巧十则
- LZMA ERROR 1 - must RESET board to recover错误分析
- 竖式问题