xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>HelloWorldService</name>
<namespace>http://localhost:8090/WebServiceTest
</namespace>
<serviceClass>service.IHelloWorld</serviceClass>
<implementationClass>service.HelloWorldImpl
</implementationClass>
</service>
</beans>
接口文件:
package service;
public interface IHelloWorld {
//sayHello 方法声明了Web服务对外暴露的接口
public long upload(String filename,int action,String datastr);
}
服务端文件:
package service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;import javax.servlet.http.HttpServletRequest;
import org.codehaus.xfire.transport.http.XFireServletController;
import org.codehaus.xfire.util.Base64;public class HelloWorldImpl implements IHelloWorld {
/**
* @filename:要下载的文件名(全名);
* @action:客户端要上传一份文件,考虑到断点续传功能,就必须要知道服务器上的文件现在的大小,才能确定从哪个位置开始传输,所以设计了这个action参数,值为0时表示询问文件大小,为1时表示传输的第三个参数datastr就是经过Base64编码的文件内容字符串;
* @datastr:经过Base64编码的内容字符串;
* @return:把文件的实时大小作为最新位置返回;
*/
public long upload(String filename,int action,String datastr){
String filepath="e://aaa//";
long filesize=0;
try{
if (!filename.equals(null) && !filename.equals("")){
File file=new File(filepath+filename);
if(action==0){
return file.length();
}else{
RandomAccessFile raf=new RandomAccessFile(file,"rw");
raf.seek(file.length());//先定位
byte[] bytes = Base64.decode(datastr);
raf.write(bytes);
//raf.skipBytes(datastr.length());//顺序写
raf.close();
//System.out.println("文件位置:"+file.length());
//System.out.println("写入长度:"+datastr.length());
filesize=file.length();
}
}
return filesize;
}catch(Exception e){
e.printStackTrace();
return 0;
}
}
}
客户端代码:
package service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
import java.net.URL;import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.transport.http.HttpTransport;
import org.codehaus.xfire.util.Base64;
public class DynamicClientTest {
public Object[] getWebService(String surl,String saction,Object[] objarr) throws MalformedURLException,Exception
{
Client client = new Client(new URL(surl));
//client.setProperty("mtom-enabled", "true");
client.setProperty(HttpTransport.CHUNKING_ENABLED,"true");
Object[] results = client.invoke(saction, objarr);
return results; }
public static void main(String[] args) throws MalformedURLException,Exception {
String surl="http://localhost:8080/WebServiceTest/services/HelloWorldService?wsdl";
DynamicClientTest web=new DynamicClientTest(); Long start=System.currentTimeMillis();
DynamicClientTest web=new DynamicClientTest();
Object[] results4=web.getWebService(surl, "upload", new Object[] { "test.rar",0,null});
long startpost=(Long)results4[0];
//System.out.println("起始位置:"+startpost);
int BUFFER_LENGTH = 1024 * 20;//一次性读入大小
int SLEEP_TIME=250;//循环读次数
long filesize=0; FileInputStream fis = null;
try {
File upfile=new File("e://GMS.rar");
filesize=upfile.length();
//System.out.println("文件大小:"+filesize);
fis=new FileInputStream(upfile);
fis.skip(startpost);//读文件前,先定位
while(startpost<filesize){
StringBuffer sb = new StringBuffer();
int time=0;
byte[] buffer = new byte[BUFFER_LENGTH];
int count=-1;
while (time<SLEEP_TIME && (count = fis.read(buffer)) != -1) {//这里要特别注意“time<SLEEP_TIME”条件一定要放在“(count = fis.read(buffer)) != -1”的前面,“&&”是在满足第一个条件的前提下才继续判断第二个条件的,否则,不管是否执行循环下面的代码,只要fis.read(buffer)一执行,读取文件的定位都会往后移,这样会造成下一次循环读取的数据就不对了。
sb.append(Base64.encode(buffer,0,count));
time++;
}
String ret= sb.toString();
results4=web.getWebService(surl, "upload", new Object[] { "test.rar",1,ret});
startpost=(Long)results4[0];
//System.out.println("位置:"+startpost);
} Long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start));
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new Exception("出错啦!", e);
} catch (IOException e) {
e.printStackTrace();
throw new Exception("出错啦!", e);
} catch (Exception e) {
throw new Exception("出错啦!", e);
} finally {
fis.close();
}
}
}
标签:WebServie,java,String,文件传输,new,io,import,JAVA,public
From: https://blog.51cto.com/u_16230604/7554600