package test;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) throws Exception {
String pathString1 = "D:/test/test1.png";
String pathString2 = "D:/test/test2.png";
String acceptUrl= "http://192.168.1.108:8080/chenei/app/uploadFiles.do";
RandomAccessFile raf1 = new RandomAccessFile(pathString1, "r");
raf1.seek(0);
RandomAccessFile raf2 = new RandomAccessFile(pathString2, "r");
raf2.seek(0);
byte[] buffer1 = new byte[1024*1024];//128k
byte[] buffer2 = new byte[1024*1024];//128k
raf1.read(buffer1);
raf2.read(buffer2);
uploadFile(acceptUrl,buffer1, buffer2);
}
private static void uploadFile(String acceptUrl, byte[] data1, byte[] data2) {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try{
URL url = new URL(acceptUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestProperty("sid", "03d47cd0cff845f1ae1bc3b27b298432");
httpURLConnection.setRequestProperty("type", "imgLicense");
// 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
// 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
httpURLConnection.setChunkedStreamingMode(128*1024);// 128*1024 是128k
// 允许输入输出流
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
// 使用POST方法
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);//application/octet-stream multipart/form-data
DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
pushStream(data1, end, twoHyphens, boundary, dos);
pushStream(data2, end, twoHyphens, boundary, dos);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
if(httpURLConnection.getResponseCode() == 200 ){
InputStream is = httpURLConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String result = br.readLine();
System.out.println(result);
is.close();
}
dos.close();
} catch (Exception e)
{
e.printStackTrace();
System.out.println("MediaActivity uploadFil Exception:"+e.getMessage());
}
}
private static void pushStream(byte[] data, String end, String twoHyphens, String boundary,
DataOutputStream dos) throws IOException {
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+"myfilename.jpg"
+"\""
+end);
dos.writeBytes(end);
dos.write(data,0,data.length);
dos.writeBytes(end);
}
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) throws Exception {
String pathString1 = "D:/test/test1.png";
String pathString2 = "D:/test/test2.png";
String acceptUrl= "http://192.168.1.108:8080/chenei/app/uploadFiles.do";
RandomAccessFile raf1 = new RandomAccessFile(pathString1, "r");
raf1.seek(0);
RandomAccessFile raf2 = new RandomAccessFile(pathString2, "r");
raf2.seek(0);
byte[] buffer1 = new byte[1024*1024];//128k
byte[] buffer2 = new byte[1024*1024];//128k
raf1.read(buffer1);
raf2.read(buffer2);
uploadFile(acceptUrl,buffer1, buffer2);
}
private static void uploadFile(String acceptUrl, byte[] data1, byte[] data2) {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try{
URL url = new URL(acceptUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestProperty("sid", "03d47cd0cff845f1ae1bc3b27b298432");
httpURLConnection.setRequestProperty("type", "imgLicense");
// 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
// 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
httpURLConnection.setChunkedStreamingMode(128*1024);// 128*1024 是128k
// 允许输入输出流
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
// 使用POST方法
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);//application/octet-stream multipart/form-data
DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
pushStream(data1, end, twoHyphens, boundary, dos);
pushStream(data2, end, twoHyphens, boundary, dos);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
if(httpURLConnection.getResponseCode() == 200 ){
InputStream is = httpURLConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String result = br.readLine();
System.out.println(result);
is.close();
}
dos.close();
} catch (Exception e)
{
e.printStackTrace();
System.out.println("MediaActivity uploadFil Exception:"+e.getMessage());
}
}
private static void pushStream(byte[] data, String end, String twoHyphens, String boundary,
DataOutputStream dos) throws IOException {
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+"myfilename.jpg"
+"\""
+end);
dos.writeBytes(end);
dos.write(data,0,data.length);
dos.writeBytes(end);
}
}