首先声明我知道有个协议叫ftp,也知道有种编程叫sock编程,但我就是碰到了server对外只开放80端口,并且还需要提供文件上传和下载功能的应用,那好吧,开始干活。
1. 首先是一个封装了服务器端文件路径,客户端文件路径和要传输的字节数组的MyFile类。
- packagecom.googlecode.garbagecan.cxfstudy.filetransfer;
- publicclassMyFile{
- privateStringclientFile;
- privateStringserverFile;
- privatelongposition;
- privatebyte[]bytes;
- publicStringgetClientFile(){
- returnclientFile;
- }
- publicvoidsetClientFile(StringclientFile){
- this.clientFile=clientFile;
- }
- publicStringgetServerFile(){
- returnserverFile;
- }
- publicvoidsetServerFile(StringserverFile){
- this.serverFile=serverFile;
- }
- publiclonggetPosition(){
- returnposition;
- }
- publicvoidsetPosition(longposition){
- this.position=position;
- }
- publicbyte[]getBytes(){
- returnbytes;
- }
- publicvoidsetBytes(byte[]bytes){
- this.bytes=bytes;
- }
- }
2. 文件传输的Web Service接口
- packagecom.googlecode.garbagecan.cxfstudy.filetransfer;
- importjavax.jws.WebMethod;
- importjavax.jws.WebService;
- @WebService
- publicinterfaceFileTransferService{
- @WebMethod
- voiduploadFile(MyFilemyFile)throwsFileTransferException;
- @WebMethod
- MyFiledownloadFile(MyFilemyFile)throwsFileTransferException;
- }
3. 文件传输的Web Service接口实现类,主要是一些流的操作
- packagecom.googlecode.garbagecan.cxfstudy.filetransfer;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.OutputStream;
- importjava.util.Arrays;
- importorg.apache.commons.io.FileUtils;
- importorg.apache.commons.io.IOUtils;
- publicclassFileTransferServiceImplimplementsFileTransferService{
- publicvoiduploadFile(MyFilemyFile)throwsFileTransferException{
- OutputStreamos=null;
- try{
- if(myFile.getPosition()!=0){
- os=FileUtils.openOutputStream(newFile(myFile.getServerFile()),true);
- }else{
- os=FileUtils.openOutputStream(newFile(myFile.getServerFile()),false);
- }
- os.write(myFile.getBytes());
- }catch(IOExceptione){
- thrownewFileTransferException(e.getMessage(),e);
- }finally{
- IOUtils.closeQuietly(os);
- }
- }
- publicMyFiledownloadFile(MyFilemyFile)throwsFileTransferException{
- InputStreamis=null;
- try{
- is=newFileInputStream(myFile.getServerFile());
- is.skip(myFile.getPosition());
- byte[]bytes=newbyte[1024*1024];
- intsize=is.read(bytes);
- if(size>0){
- byte[]fixedBytes=Arrays.copyOfRange(bytes,0,size);
- myFile.setBytes(fixedBytes);
- }else{
- myFile.setBytes(newbyte[0]);
- }
- }catch(IOExceptione){
- thrownewFileTransferException(e.getMessage(),e);
- }finally{
- IOUtils.closeQuietly(is);
- }
- returnmyFile;
- }
- }
4. 一个简单的文件传输异常类
- packagecom.googlecode.garbagecan.cxfstudy.filetransfer;
- publicclassFileTransferExceptionextendsException{
- privatestaticfinallongserialVersionUID=1L;
- publicFileTransferException(){
- super();
- }
- publicFileTransferException(Stringmessage,Throwablecause){
- super(message,cause);
- }
- publicFileTransferException(Stringmessage){
- super(message);
- }
- publicFileTransferException(Throwablecause){
- super(cause);
- }
- }
5. 下面是Server类用来发布web service
- packagecom.googlecode.garbagecan.cxfstudy.filetransfer;
- importjavax.xml.ws.Endpoint;
- publicclassFileTransferServer{
- publicstaticvoidmain(String[]args)throwsException{
- Endpoint.publish("http://localhost:9000/ws/jaxws/fileTransferService",newFileTransferServiceImpl());
- }
- }
6. 最后是Client类,用来发送文件上传和下载请求。
- packagecom.googlecode.garbagecan.cxfstudy.filetransfer;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.OutputStream;
- importjava.util.Arrays;
- importorg.apache.commons.io.FileUtils;
- importorg.apache.commons.io.IOUtils;
- importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- publicclassFileTransferClient{
- privatestaticfinalStringaddress="http://localhost:9000/ws/jaxws/fileTransferService";
- privatestaticfinalStringclientFile="/home/fkong/temp/client/test.zip";
- privatestaticfinalStringserverFile="/home/fkong/temp/server/test.zip";
- publicstaticvoidmain(String[]args)throwsException{
- longstart=System.currentTimeMillis();
- //uploadFile();
- //downloadFile();
- longstop=System.currentTimeMillis();
- System.out.println("Time:"+(stop-start));
- }
- privatestaticvoiduploadFile()throwsFileTransferException{
- InputStreamis=null;
- try{
- MyFilemyFile=newMyFile();
- is=newFileInputStream(clientFile);
- byte[]bytes=newbyte[1024*1024];
- while(true){
- intsize=is.read(bytes);
- if(size<=0){
- break;
- }
- byte[]fixedBytes=Arrays.copyOfRange(bytes,0,size);
- myFile.setClientFile(clientFile);
- myFile.setServerFile(serverFile);
- myFile.setBytes(fixedBytes);
- uploadFile(myFile);
- myFile.setPosition(myFile.getPosition()+fixedBytes.length);
- }
- }catch(IOExceptione){
- thrownewFileTransferException(e.getMessage(),e);
- }finally{
- IOUtils.closeQuietly(is);
- }
- }
- privatestaticvoiduploadFile(MyFilemyFile)throwsFileTransferException{
- JaxWsProxyFactoryBeanfactoryBean=newJaxWsProxyFactoryBean();
- factoryBean.setAddress(address);
- factoryBean.setServiceClass(FileTransferService.class);
- Objectobj=factoryBean.create();
- FileTransferServiceservice=(FileTransferService)obj;
- service.uploadFile(myFile);
- }
- privatestaticvoiddownloadFile()throwsFileTransferException{
- MyFilemyFile=newMyFile();
- myFile.setServerFile(serverFile);
- longposition=0;
- while(true){
- myFile.setPosition(position);
- myFile=downloadFile(myFile);
- if(myFile.getBytes().length<=0){
- break;
- }
- OutputStreamos=null;
- try{
- if(position!=0){
- os=FileUtils.openOutputStream(newFile(clientFile),true);
- }else{
- os=FileUtils.openOutputStream(newFile(clientFile),false);
- }
- os.write(myFile.getBytes());
- }catch(IOExceptione){
- thrownewFileTransferException(e.getMessage(),e);
- }finally{
- IOUtils.closeQuietly(os);
- }
- position+=myFile.getBytes().length;
- }
- }
- privatestaticMyFiledownloadFile(MyFilemyFile)throwsFileTransferException{
- JaxWsProxyFactoryBeanfactoryBean=newJaxWsProxyFactoryBean();
- factoryBean.setAddress(address);
- factoryBean.setServiceClass(FileTransferService.class);
- Objectobj=factoryBean.create();
- FileTransferServiceservice=(FileTransferService)obj;
- returnservice.downloadFile(myFile);
- }
- }
首先需要准备一个大一点的文件,然后修改代码中的clientFile和serverFile路径,然后分别打开uploadFile和downloadFile注释,运行程序,检查目标文件查看结果。
这个程序还是比较简单的,但基本生完成了文件上传下载功能,如果需要,也可以对这个程序再做点修改使其支持断点续传。
原文链接:http://blog.csdn.net/kongxx/article/details/7540930
【系列文章】