今天想要存储一个2g多的视频文件,但是发现腾讯微云一次性上传最大可以上传1G,这时候就想写一个分批存储文件来存储。因此一口气就把分批存储文件,合并文件的代码给写出来了。写出来后想跟同事炫耀一番,没想到同事跟我说,其实 rar压缩文件可以实现这个工作,挖槽,写的代码全废了,本来想删掉的。但觉得还是上传到这里来好了。下面是所有代码,有个问题,就是文件处理很快,但是占用内存很大。还有一个,需要用到json包,可以在这里下载:http://download.csdn.net/detail/liwenpei11/8101567。
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class doMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
//合并文件
try {
//decodeFile("E:\\test\\ping1guo.avi");
combieFile("E:\\test\\");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void combieFile(String dir) throws Exception{
String tmpFilePath = dir + "\\bak_" + System.currentTimeMillis();
File tmp = new File(tmpFilePath);
if(!tmp.exists()){
tmp.createNewFile();
}
String protyPath = dir + "\\" +"property.json";
String str = null;
InputStream fis = new FileInputStream(protyPath);
str = InputStreamTOString(fis);
JSONObject jsonObj = JSONObject.fromObject(str);
//String fileName = jsonObj.getString("filename");
//tmpFilePath = dir + "\\" + fileName + "_" + System.currentTimeMillis();
long length = jsonObj.getLong("length");
JSONArray jsonArry = jsonObj.getJSONArray("data");
for(int i = 0; i < jsonArry.size(); i++){
JSONObject tmpObj = jsonArry.getJSONObject(i);
String tmpFileName = tmpObj.getString("filename");
long destStartPos = tmpObj.getLong("deststartpos");
long destEndPos = tmpObj.getLong("destendpos");
long partLength = tmpObj.getLong("length");
String path = dir + "\\" + tmpFileName;
RandomAccessFile readBaf = new RandomAccessFile(path, "rw");
FileChannel readFc = readBaf.getChannel();
MappedByteBuffer readMbb = readFc.map(MapMode.READ_WRITE, 0, partLength);
RandomAccessFile writeBaf = new RandomAccessFile(tmpFilePath, "rw");
FileChannel writeFc = writeBaf.getChannel();
MappedByteBuffer writeMbb = writeFc.map(MapMode.READ_WRITE, destStartPos, destEndPos - destStartPos + 1);
writeMbb.put(readMbb);
readFc.close();
readBaf.close();
writeFc.close();
writeBaf.close();
}
}
/**
* 将InputStream转换成String
*
* @param in
* InputStream
* @return String
* @throws Exception
*
*/
public static String InputStreamTOString(InputStream in) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int count = -1;
while ((count = in.read(data, 0, 1024)) != -1)
outStream.write(data, 0, count);
data = null;
return new String(outStream.toByteArray(), "UTF-8");
}
static void decodeFile(String path) throws IOException{
long eachLength = 1 * 200 * 1024 * 1024;//1G
File file = new File(path);
String dir = file.getParent();
long length = file.length();
long partNum = length / eachLength;
long lastLength = length % eachLength;
JSONObject jsonObj = JSONObject.fromObject("{}");
JSONArray jsonArry = new JSONArray();
MappedByteBuffer readMbb = null;
RandomAccessFile writeBaf = null;
RandomAccessFile readBaf = new RandomAccessFile(path, "rw");
FileChannel readFc = readBaf.getChannel();
for(int i = 0; i < partNum ; i ++){
String tmpFileName = "part" + "-" + i;
String tmpPath = dir + "\\" +tmpFileName;
readMbb = readFc.map(MapMode.READ_WRITE, i * eachLength, eachLength);
writeBaf = new RandomAccessFile(tmpPath, "rw");
FileChannel writeFc = writeBaf.getChannel();
MappedByteBuffer writeMbb = writeFc.map(MapMode.READ_WRITE, 0, eachLength);
writeMbb.put(readMbb);
//readFc.close();
//readBaf.close();
writeFc.close();
writeBaf.close();
readMbb = null;
writeBaf = null;
JSONObject jsonTmp = new JSONObject();
jsonTmp.put("filename", tmpFileName);
jsonTmp.put("deststartpos", i * eachLength);
jsonTmp.put("destendpos", i * eachLength + eachLength -1);
jsonTmp.put("length", eachLength );
jsonArry.add(jsonTmp);
}
if(lastLength > 0){
String tmpFileName = "part" + "-" + partNum;
String tmpPath = dir + "\\" +tmpFileName;
//RandomAccessFile readBaf = new RandomAccessFile(path, "rw");
//FileChannel readFc = readBaf.getChannel();
readMbb = readFc.map(MapMode.READ_WRITE, partNum * eachLength, lastLength);
writeBaf = new RandomAccessFile(tmpPath, "rw");
FileChannel writeFc = writeBaf.getChannel();
MappedByteBuffer writeMbb = writeFc.map(MapMode.READ_WRITE, 0, lastLength);
writeMbb.put(readMbb);
readFc.close();
readBaf.close();
writeFc.close();
writeBaf.close();
readMbb = null;
writeBaf = null;
JSONObject jsonTmp = new JSONObject();
jsonTmp.put("filename", tmpFileName);
jsonTmp.put("deststartpos", partNum * eachLength);
jsonTmp.put("destendpos", partNum * eachLength + lastLength -1);
jsonTmp.put("length", lastLength );
jsonArry.add(jsonTmp);
}
jsonObj.put("data", jsonArry);
jsonObj.put("filename", file.getName());
jsonObj.put("length", file.length());
String protyPath = dir + "\\" +"property.json";
writeBaf = new RandomAccessFile(protyPath, "rw");
FileChannel writeFc = writeBaf.getChannel();
MappedByteBuffer writeMbb = writeFc.map(MapMode.READ_WRITE, 0, jsonObj.toString().getBytes("UTF-8").length);
writeMbb.put(jsonObj.toString().getBytes("UTF-8"));
writeFc.close();
writeBaf.close();
}
}