webService分段传输视频文件

最近在做webService分段传输视频文件的需求,有点忙,看能不能坚持下来把这个技术点搞明白吧!

简单的来一个domo吧

分段上传  核心是 输入流的skip()方法,每一次都从上一次的指针位置开始读取,追加到上一次复制的文件里面.

所以每一次传输前需要查询一下已经传输的文件字节长度,作为传输的指针

使用@WebService标签,发布服务,根据生成的xml文件可以在eclipse中自动生成web service client的代码

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Logger;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class VideoUpload {
    private Logger logger = Logger.getLogger("videoUpload");

    public ReturnCode uploadFile(String fileName, byte[] intoBuffer, long offSet, String flag, String md5)
            throws IOException {
        ReturnCode returnCode = new ReturnCode();
        if (fileName != null) {
            File file = new File(fileName);
            if (!file.exists()) {
                returnCode.setType("E");
                return returnCode;
            }
            File newFile = new File("E:/temp/file2/" + file.getName());
            File parentFile = newFile.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile, true));
            // 从指针位置开始读
            bis.skip(offSet);
            // 读指定字节数组
            byte[] bt = intoBuffer;
            int len = 0;
            len = bis.read(bt);
            bos.write(bt, 0, len);
            bis.close();
            bos.close();
            if ("1".equals(flag)) {
                String fileMd5 = FileMD5Util.getFileMd5(newFile);
                if (!fileMd5.equals(md5)) {
                    logger.info(fileMd5);
                    logger.info(md5);
                    logger.info("文件传输有误!");
                    returnCode.setType("E");
                    return returnCode;
                } else {
                    logger.info(fileMd5);
                    logger.info(md5);
                    logger.info("文件传输完成!");
                }

            }
            returnCode.setType("S");
            return returnCode;
        } else {
    

    public ReturnCode getFileLength(String fileName) {
        ReturnCode returnCode = new ReturnCode();
        if (fileName != null) {
            File file = new File(fileName);
            File newFile = new File("E:/temp/file2/" + file.getName());
            if (newFile.exists()) {
                returnCode.setType("S");
                returnCode.setDateObj(newFile.length());
                return returnCode;
            } else {
                returnCode.setType("E");
                return returnCode;
            }
        } else {
            returnCode.setType("E");
            return returnCode;
        }
    }

    public static void main(String[] args) {
        Endpoint.publish("http://127.0.0.1:9001/Service/VideoUpload", new VideoUpload());
        System.out.println("OK");
    }
}

这是基本的ReturnCode

package com.gua.demo;

import java.io.Serializable;

public class ReturnCode implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2788504149083434920L;
	private String type;
	private String code;
	private String desc;
	private Object dateObj;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public Object getDateObj() {
		return dateObj;
	}
	public void setDateObj(Object dateObj) {
		this.dateObj = dateObj;
	}
	
} 
// 这是简单的测试  
package com.gua.demo.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.rmi.RemoteException;
import java.util.logging.Logger;

import com.gua.demo.ReturnCode;
import com.gua.demo.VideoUpload;
import com.gua.demo.VideoUploadProxy;

public class Test {

	private static VideoUpload videoUpload = new VideoUploadProxy().getVideoUpload();
	private static Logger logger = Logger.getLogger("Test");

	public static void main(String[] args) {
		File file = new File("E:\\temp\\file1\\1.txt");
		upload(file);
	}

	private static boolean upload(File file) {
		String fileName = file.getAbsolutePath();
		long length = file.length();
		while (true) {
			try {
				byte[] intoBuffer = new byte[5]; // 先默认一次传5个字节
				long offSet = 0L;
				String flag = "0";
				String md5 = null;
				ReturnCode lengthCode = videoUpload.getFileLength(fileName);
				if ("S".equals(lengthCode.getType())) {
					long fileLength = (Long) lengthCode.getDateObj();
					offSet = fileLength;
					if ((length - fileLength) <= 5) {
						intoBuffer = new byte[(int) (length - fileLength)];
						flag = "1";
						md5 = FileMD5Util.getFileMd5(file);
					}
					if (length == fileLength) {
						logger.info("文件传输完成!");
						return true;
					}
				} else {
					logger.info("获取文件长度出错!");
				}
				ReturnCode uploadFile = videoUpload.uploadFile(fileName, intoBuffer, offSet, flag, md5);
				if ("S".equals(uploadFile.getType())) {
					logger.info("单次传输无误!");
				} else {
					logger.info("单次传输出错!");
				}
			} catch (RemoteException e) {
				e.printStackTrace();
				logger.info("获取文件长度失败!" + e.getLocalizedMessage());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				logger.info("获取文件md5码失败" + e.getLocalizedMessage());
			}
		}
	}
}

再附上一个获取文件MD5码的方法

package com.gua.demo.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;

public class FileMD5Util {
	public static String getFileMd5(File file) throws FileNotFoundException {  
        String value = null;  
        FileInputStream in = new FileInputStream(file);  
    try {  
        MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());  
        MessageDigest md5 = MessageDigest.getInstance("MD5");  
        md5.update(byteBuffer);  
        BigInteger bi = new BigInteger(1, md5.digest());  
        value = bi.toString(16);  
    } catch (Exception e) {  
        e.printStackTrace();  
    } finally {  
            if(null != in) {  
                try {  
                in.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
    return value;  
    }  
	public static void main(String[] args) {
		try {
			String m1 = FileMD5Util.getFileMd5(new File("E:/temp/1.txt"));
			String m2 = FileMD5Util.getFileMd5(new File("E:/temp/2.txt"));
			System.out.println(m1);
			System.out.println(m2);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值