java中webService下客户端往服务端发送大文件

本文介绍了一种通过Webservice进行大文件传输的方法,包括客户端和服务端的具体实现过程。通过分段读取文件并发送,成功实现了3G、4G等大文件的有效传输。

怎么配置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的大文件都没有问题哦 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醉梦洛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值