批量上传文件

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);
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值