批量上传文件
2015-07-20 17:40
627 查看
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);
}
}
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- 基于Spring框架的Shiro配置方法
- Spring MVC中上传文件实例
- 实例讲解Java的Spring框架中的AOP实现
- 解析Java中如何获取Spring中配置的bean
- Spring的注解配置与XML配置之间的比较
- java Spring整合Freemarker的详细步骤
- Java 二维码,QR码,J4L-QRCode 的资料整理
- java当中的定时器的4种使用方式
- java中 spring 定时任务 实现代码