用maven创建基于wink的rest服务-传送文件(字符串和字节数组)(一)

时间:2023-01-11 18:40:45
Apache Wink是一个用于帮助开发者快速高效的开发RESTful Web services服务的java框架,他包括一个服务器端模块和一个客户端模块.
1.创建基于wink的web服务.
  a.用maven构建rest服务器端项目,直接看代码pom.xml.
  b.将cmd窗口切换到rest-server目录下执行mvn eclipse:myeclipse-clean eclipse:myeclipse命令下载pom.xml文件中配置的类库.
  c.在web.xml中将所有rest服务映射到RestServerApplication中,rest通过Application管理所有服务,直接看代码RestServerApplication.

  d.创建rest服务,直接看代码RestServerService.

<span style="font-size:12px;">package com.iluck.rest.server;

/**
* @author IluckySi
* @date 20140528
*/
public class RestServerServiceImpl implements RestServerService {

private static RestServerServiceImpl instance = null;;

public synchronized static RestServerServiceImpl getInstance() {
if(instance == null) {
instance = new RestServerServiceImpl();
}
return instance;
}

public String upload(String message) {
System.out.println(message);
return "success";
}

public String upload(byte[] bytes) {
System.out.println(new String(bytes));
return "success";
}
}
/*
输出结果:
我是司冬雪123sdx
我是司冬雪123sdx
w速度速度123sdx
w速度速度123sdx
*/</span>

2.创建基于wink的java客户端.
  a.用maven构建rest客户端项目,直接看代码pom.xml.
  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.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;

import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;

/**
* @author IluckySi
* @date 20140528
*/
public class RestClientService {

public static void main(String[] args) {
try {
RestClient restClient = new RestClient();
Resource resource = restClient.resource("http://127.0.0.1:8080/rest-server/rest/file/upload");
//传输字符串.
String message = "我是司冬雪123sdx";
final ClientResponse response1 = resource.post(message);
System.out.println(response1.getEntity(String.class));
//传输二进制数.
byte[] bytes = message.getBytes("UTF-8");
final ClientResponse response2 = resource.post(bytes);
System.out.println(response2.getEntity(String.class));
//用字节流从本地读取文件,将文件内容转换为二进制数传输.
File file = new File("E:/client2.txt");
byte[] bytes2 = new byte[(int) file.length()];
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
byte[] b = new byte[2048];
int length;
length = bis.read(b);
while (length != -1) {
bytestream.write(b, 0, length);
length = bis.read(b);
}
bis.close();
is.close();
bytestream.close();
bytes2 = bytestream.toByteArray();
final ClientResponse response3 = resource.post(bytes2);
System.out.println(response3.getEntity(String.class));
//用字符流从本地读取文件,将文件内容转换为字符串传输.
StringBuffer sb = new StringBuffer();
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = bufferedReader.readLine();
while (line != null) {
sb.append(line + "\r\n");
line = bufferedReader.readLine();
}
final ClientResponse response4 = resource.post(sb.toString());
System.out.println(response4.getEntity(String.class));
//注意:上述两种情况可能出现乱码,原因是txt文件采用的是系统默认编码方式ANSI,只需要将其另存并同时将编码方式改为UTF-8就可以了.
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
输出结果:
success
success
success
success
*/</span>