关闭

java 文件传输 分块传输

标签: java文件分块传输
170人阅读 评论(0) 收藏 举报
分类:
<span style="font-size:18px;">//接收端,将接受到的字节流转换为FileBlock对象,并写入文件</span>
/**
  * 将文件块fileBlock写入文件
  * 
  * @param fileBlock 
  * @param fileBlock.getFilePath() 除了sdcard路径外的子路径(为了保证不同型号平板兼容性),类似:/qq/abc.doc
  * @throws IOException
  */
 public static void writeIntoFile(FileBlock fileBlock,String destFilePeath) throws IOException {
  File destFile = new File(destFilePeath);




  RandomAccessFile raf;
  if (destFile.exists()) {
   raf = new RandomAccessFile(destFile, "rw");
  } else {
   // 如果文件夹不存在,则创建
   File parentDir = new File(destFile.getParent());
   if (!parentDir.exists()) {
    parentDir.mkdirs();
   }
   raf = new RandomAccessFile(destFile, "rw");
   raf.setLength(fileBlock.getFileSize());
  }




  raf.seek(fileBlock.getOffset());
  raf.write(fileBlock.getBlockContent(), 0,
    fileBlock.getBlockContent().length);
  raf.close();
 }


 
// 发送端 将文件转换为字节流,并以块的形式发送,每次发送Constants.FILE_BLOCKSIZE=512*1024 也就是512个字节
long fileSize = file.length();
				int blockNum = (int) (fileSize / Constants.FILE_BLOCKSIZE) + 1;
				raf = new RandomAccessFile(file, "rw");
				String desFilePath = file.getAbsolutePath().substring(rootPath.length());
				desFilePath = desFilePath.replaceAll("\\"+File.separator, "/");
				for (int i = 0; i < blockNum; i++) {
					FileBlock tempFileBlock = new FileBlock();
 
					if (i == blockNum - 1) {
						tempFileBlock = FileAndByteTranslate.readRandomAccessFile(raf,
								desFilePath, i * Constants.FILE_BLOCKSIZE,
								(int) (fileSize % Constants.FILE_BLOCKSIZE));
					} else {
						tempFileBlock = FileAndByteTranslate.readRandomAccessFile(raf,
								desFilePath, i * Constants.FILE_BLOCKSIZE,
								Constants.FILE_BLOCKSIZE);
					}
					Map<String,Object> mHeaders = new HashMap<String,Object>();
					mHeaders.put("userid", userId);
					sendHelper.setmHeaders(mHeaders);
					sendHelper.send(ObjectToIO.objectToBytes(tempFileBlock));
}



 
/**
  * 随机读文件
  * 
  * @param raf
  * @param destFilePath
  * @param offset
  * @param size
  * @return
  * @throws IOException
  */
 public static synchronized FileBlock readRandomAccessFile(RandomAccessFile raf,
   String destFilePath, int offset, int size) throws IOException {
  if (raf == null) {
   return null;
  }
  FileBlock fileBlock = new FileBlock();




  byte[] blockContent = new byte[size];
  raf.seek(offset);
  int length = raf.read(blockContent, 0, size);




  fileBlock.setBlockContent(blockContent);
  fileBlock.setBlockLength(length);
  fileBlock.setFilePath(destFilePath);
  fileBlock.setFileSize(raf.length());
  fileBlock.setOffset(offset);
  return fileBlock;




 }
FileBlock类:
<pre class="java" name="code">package com.bcts.tools;

import java.io.Serializable;

public class FileBlock implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -15871877163300278L;
	private String filePath;//文件保存的相对路径
	private long fileSize;//整个文件大小
	private int offset;//偏移量
	private int blockLength;//块中实际内容长度, 默认512
	private byte[] blockContent;//块内容
	
	public FileBlock() {
		// TODO Auto-generated constructor stub
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getBlockLength() {
		return blockLength;
	}

	public void setBlockLength(int blockLength) {
		this.blockLength = blockLength;
	}

	public byte[] getBlockContent() {
		return blockContent;
	}

	public void setBlockContent(byte[] blockContent) {
		this.blockContent = blockContent;
	}

	@Override
	public String toString() {
		return "FileBlock [filePath=" + filePath + ", fileSize=" + fileSize
				+ ", offset=" + offset + ", blockLength=" + blockLength + "]";
	}

	
	

}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2559次
    • 积分:90
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类