java 图片分段上传_java 分段读取文件 并通过HTTP上传
1、首先將文件分段,用RandomAccessFile
2、分段后將分出的內(nèi)容上傳到http
1.[代碼][Java]代碼
URL url = new URL(actionUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
/** 允許Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/** 設(shè)定傳送的method=POST */
con.setRequestMethod("POST");
/** setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
/** 設(shè)定DataOutputStream */
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; "
+ "name=\"file1\";filename=\"" + newName + "\"" + end);
ds.writeBytes(end);
/** 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/** 設(shè)定每次寫入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/** 從文件讀取數(shù)據(jù)到緩沖區(qū) */
while ((length = fStream.read(buffer)) != -1)
{
/** 將數(shù)據(jù)寫入DataOutputStream中 */
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
/** close streams */
fStream.close();
ds.flush();
總結(jié)
以上是生活随笔為你收集整理的java 图片分段上传_java 分段读取文件 并通过HTTP上传的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3调用java_Python
- 下一篇: 幼儿园 c语言,【资源学习】c语言程序代