android 大檔案分割上傳(分塊上傳)
阿新 • • 發佈:2019-02-04
由於android自身的原因,對大檔案(如視訊檔案)的操作很容易造成OOM,即:Dalvik堆記憶體溢位,利用檔案分割將大檔案分割為小檔案可以解決問題。
檔案分割後分多次請求服務。
//檔案分割上傳 public void cutFileUpload(String fileType,String filePath) { try { FileAccessI fileAccessI = new FileAccessI(filePath, 0); Long nStartPos = 0l; Long length = fileAccessI.getFileLength(); int mBufferSize = 1024 * 100; //每次處理1024 * 100位元組 byte[] buffer = new byte[mBufferSize]; FileAccessI.Detail detail; long nRead = 0l; String vedioFileName = Usual.f_getUUID(); //分配一個檔名 long nStart = nStartPos; int i = 0; while (nStart < length) { detail = fileAccessI.getContent(nStart); nRead = detail.length; buffer = detail.b; JSONObject mInDataJson = new JSONObject(); mInDataJson.put("a", "282"); mInDataJson.put("FileName", vedioFileName); mInDataJson.put("start", nStart); //服務端獲取開始文章進行寫檔案 mInDataJson.put("filetype", fileType); nStart += nRead; nStartPos = nStart; String url = UsualA.f_getXmlSOAUrl(UsualA.mServiceFastByteUrl, "n.uploadvedio", mInDataJson.toString(), "282"); HttpFastUtil.f_httpPostByte(url, buffer, false); } } catch (Exception e) { }
檔案分割類
package ishitong.mppsp.android.util; import java.io.*; public class FileAccessI implements Serializable { RandomAccessFile oSavedFile; long nPos; public FileAccessI() throws IOException { this("", 0); } public FileAccessI(String sName, long nPos) throws IOException { oSavedFile = new RandomAccessFile(sName, "rw");//建立一個隨機訪問檔案類,可讀寫模式 this.nPos = nPos; oSavedFile.seek(nPos); } public synchronized int write(byte[] b, int nStart, int nLen) { int n = -1; try { oSavedFile.write(b, nStart, nLen); n = nLen; } catch (IOException e) { e.printStackTrace(); } return n; } //每次讀取102400位元組 public synchronized Detail getContent(long nStart) { Detail detail = new Detail(); detail.b = new byte[102400]; try { oSavedFile.seek(nStart); detail.length = oSavedFile.read(detail.b); } catch (IOException e) { e.printStackTrace(); } return detail; } public class Detail { public byte[] b; public int length; } //獲取檔案長度 public long getFileLength() { Long length = 0l; try { length = oSavedFile.length(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return length; } }
服務端獲得分割的檔案,利用RandomAccessFile進行檔案整理
/** * 音視訊圖片處理 * @param mStr * @return * @throws Exception */ public static String f_uploadVedio(String mStr) throws Exception { String mResult = Usual.mEmpty; String fileType = Usual.mEmpty; int startPosL = 0; RandomAccessFile oSavedFile = null; JSONObject jsonObject = new JSONObject(mStr); String vedioJsonStr = jsonObject.getString("VEDIO"); byte[] vedioBytes = Usual.f_fromBase64String(vedioJsonStr); startPosL = (Integer) jsonObject.get("start"); //接收客戶端的開始位置(檔案讀取到的位元組大小) fileType = (String)jsonObject.getString("filetype"); String fileName = (String)jsonObject.getString("FileName"); if(fileType.equals("picture")) { oSavedFile = new RandomAccessFile("E:\\"+fileName+".jpg","rw"); } else if(fileType.equals("photo")) { oSavedFile = new RandomAccessFile("E:\\"+fileName+".jpg","rw"); } else if(fileType.equals("voice")) { oSavedFile = new RandomAccessFile("E:\\"+fileName+".mp3","rw"); } else if(fileType.equals("video")) { oSavedFile = new RandomAccessFile("E:\\"+fileName+".mp4", "rw"); } //設定標誌位,標誌檔案儲存的位置 oSavedFile.seek(startPosL); oSavedFile.write(vedioBytes); oSavedFile.close(); mResult = "000"; return mResult; }
檔案轉為string字串參考:
http://blog.csdn.net/jdsjlzx/article/details/51194719
用HttpUrlConnection模擬post表單進行檔案上傳平時很少使用,比較麻煩。
原理是: 分析檔案上傳的資料格式,然後根據格式構造相應的傳送給伺服器的字串。
格式如下:這裡的httppost123是我自己構造的字串,可以是其他任何的字串
----------httppost123 (\r\n)
Content-Disposition: form-data; name="img"; filename="t.txt" (\r\n)
Content-Type: application/octet-stream (\r\n)
(\r\n)
sdfsdfsdfsdfsdf (\r\n)
----------httppost123 (\r\n)
Content-Disposition: form-data; name="text" (\r\n)
(\r\n)
text tttt (\r\n)
----------httppost123-- (\r\n)
(\r\n)
上面的(\r\n)表示各個資料必須以(\r\n)結尾。
具體Java程式碼如下:
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class HttpPostUtil {
URL url;
HttpURLConnection conn;
String boundary = "--------httppost123";
Map<String, String> textParams = new HashMap<String, String>();
Map<String, File> fileparams = new HashMap<String, File>();
DataOutputStream ds;
public HttpPostUtil(String url) throws Exception {
this.url = new URL(url);
}
//重新設定要請求的伺服器地址,即上傳檔案的地址。
public void setUrl(String url) throws Exception {
this.url = new URL(url);
}
//增加一個普通字串資料到form表單資料中
public void addTextParameter(String name, String value) {
textParams.put(name, value);
}
//增加一個檔案到form表單資料中
public void addFileParameter(String name, File value) {
fileparams.put(name, value);
}
// 清空所有已新增的form表單資料
public void clearAllParameters() {
textParams.clear();
fileparams.clear();
}
// 傳送資料到伺服器,返回一個位元組包含伺服器的返回結果的陣列
public byte[] send() throws Exception {
initConnection();
try {
conn.connect();
} catch (SocketTimeoutException e) {
// something
throw new RuntimeException();
}
ds = new DataOutputStream(conn.getOutputStream());
writeFileParams();
writeStringParams();
paramsEnd();
InputStream in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
conn.disconnect();
return out.toByteArray();
}
//檔案上傳的connection的一些必須設定
private void initConnection() throws Exception {
conn = (HttpURLConnection) this.url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(10000); //連線超時為10秒
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
}
//普通字串資料
private void writeStringParams() throws Exception {
Set<String> keySet = textParams.keySet();
for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
String name = it.next();
String value = textParams.get(name);
ds.writeBytes("--" + boundary + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"\r\n");
ds.writeBytes("\r\n");
ds.writeBytes(encode(value) + "\r\n");
}
}
//檔案資料
private void writeFileParams() throws Exception {
Set<String> keySet = fileparams.keySet();
for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
String name = it.next();
File value = fileparams.get(name);
ds.writeBytes("--" + boundary + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"; filename=\"" + encode(value.getName()) + "\"\r\n");
ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n");
ds.writeBytes("\r\n");
ds.write(getBytes(value));
ds.writeBytes("\r\n");
}
}
//獲取檔案的上傳型別,圖片格式為image/png,image/jpg等。非圖片為application/octet-stream
private String getContentType(File f) throws Exception {
// return "application/octet-stream"; // 此行不再細分是否為圖片,全部作為application/octet-stream 型別
ImageInputStream imagein = ImageIO.createImageInputStream(f);
if (imagein == null) {
return "application/octet-stream";
}
Iterator<ImageReader> it = ImageIO.getImageReaders(imagein);
if (!it.hasNext()) {
imagein.close();
return "application/octet-stream";
}
imagein.close();
return "image/" + it.next().getFormatName().toLowerCase();//將FormatName返回的值轉換成小寫,預設為大寫
}
//把檔案轉換成位元組陣列
private byte[] getBytes(File f) throws Exception {
FileInputStream in = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
in.close();
return out.toByteArray();
}
//新增結尾資料
private void paramsEnd() throws Exception {
ds.writeBytes("--" + boundary + "--" + "\r\n");
ds.writeBytes("\r\n");
}
// 對包含中文的字串進行轉碼,此為UTF-8。伺服器那邊要進行一次解碼
private String encode(String value) throws Exception{
return URLEncoder.encode(value, "UTF-8");
}
public static void main(String[] args) throws Exception {
HttpPostUtil u = new HttpPostUtil("http://localhost:3000/up_load");
u.addFileParameter("img", new File(
"D:\\素材\\圓月.jpg"));
u.addTextParameter("text", "中文");
byte[] b = u.send();
String result = new String(b);
System.out.println(result);
}
}
如果不把中文轉成UTF-8的格式進行傳輸,則後臺顯示中文亂碼。
同樣,如果其他引數包含中文,則也應當先轉碼。
當然,具體什麼編碼要和後臺接收的編碼一致。