怎么配置webservice的客户端和服务端可以看我的上一篇文章:https://blog.youkuaiyun.com/weixin_39921821/article/details/82184206
这一篇是在上一篇的基础上写的,主要处理客户端往服务端发送大文件:
首先服务端代码:
package com.yinxin.service;
import java.io.*;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import com.yinxin.service.ServiceHello;
import com.yinxin.toolts.IPTimeStamp;
import com.yinxin.dao.GetValue;
@WebService
public class ServiceHello implements GetValue{
/*
*fileName 文件名称,fileContent 文件内容,len 文件的大小,isend 文件传输的最后一次, isfirst 文件传输的第一次
*/
@Override
public void getValue(String fileName, byte[] fileContent, int len, int isend, int isfirst) {
// TODO Auto-generated method stub
IPTimeStamp ipt=new IPTimeStamp();
File f = null;
OutputStream out = null;
fileName =ipt.getTimeStamp()+fileName;
System.out.println("文件名称:"+fileName);
String path = "E:"+f.separator+"eclipse-workspace"+f.separator+"TheService1"+f.separator+"src"+f.separator+fileName;
f = new File(path);
System.out.println("判断文件路径是否存在?");
if(f.getParentFile().exists()) {
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
f.mkdirs();
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
out = new FileOutputStream(f,true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b = fileContent;
if(len >= 0 && b.length >= 0){
try {
System.out.println("开始写入文件");
out.write(b, 0, len);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(isend==1){//1是最后一个
try {
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 目标:服务端的功能是把接收到的字符串转换成大写,并返回给客户端。
* 实现客户端与服务端的代码开发,并能调用
*/
public static void main(String[] args) {
/**
* 参数1:服务的发布地址http://localhost:8080/Service/ServiceHello
* 参数2:服务的实现者new ServiceHello()
*/
GetValue gv=new ServiceHello();// 发布对外开放的接口
Endpoint.publish("http://localhost:8080/Service/ServiceHello",gv);
System.out.println("webservice start success!");
}
}
客户端代码:
package com.yinxin.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.jws.WebService;
import com.yinxin.client.ServiceHello;
import com.yinxin.client.ServiceHelloService;
import com.yinxin.toolts.ReadConfig;
public class ServiceTest {
/*
* 客户端往服务端发送大文件
*/
public static void main(String[] args) throws IOException {
//一次读取2M的大小
int MAXLENTH=1024*1024*2;
ReadConfig.PullConfigXml();//执行配置文件的方法
String filePaths =ReadConfig.filePath+"XXXX.txt";
File file = new File(filePaths);
long totallen = file.length();
InputStream in = new FileInputStream(file);
byte b[] = new byte[MAXLENTH];
int len = 0;
float readlen = 0;
int count = 0;
boolean isfirst=true;//服务端接口参数
boolean isend = false;//服务端接口参数
while((len=in.read(b))!=-1){ //当没有读取完时,继续读取
readlen += len;
if ((totallen - readlen) == 0) {
isend = true;
}
System.out.println("文件的大小:"+len);
//调用服务端
int isend1 = isend?1:0;
int isfirst1 = isfirst?1:0;
ServiceHello hello =new ServiceHelloService().getServiceHelloPort();
//参数依次是:文件名,文件内容,文件长度,最后一次发送,第一次发送
hello.getValue(file.getName(), b, len, isend1, isfirst1);
isfirst = false;
count += 1; //用来统计总共发送了多少次
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
经过测试,3G,4G的大文件都没有问题哦