boundary, problem with sending several files as attachment
I can send one or two files to remote host, but i have problem when I try send three files. I think, problem is with boundary. When I send only out and out2 or out and out3 is success, when i try send out, out2 and out3 files are not send.
How have I transform this code?
Code:
String destionationPath = "upload.php";
String fileFieldName = "file";
String fileFieldName2 = "file2";
String fileFieldName3 = "file3";
File f = new File("dio_tmp/cennik_" +saveTime+ ".pdf");
File f2 = new File("dio_tmp/spis_roslin_" +saveTime+ ".pdf");
File f3 = new File("dio_tmp/plan_ogrodu_" +saveTime+ ".png");
//OuputStram
FileInputStream in = null, in2= null, in3= null;
DataOutputStream out = null, out2 = null, out3 = null;
HttpURLConnection con = null;
try
{
in = new FileInputStream(f);
in2 = new FileInputStream(f2);
in3 = new FileInputStream(f3);
String path = getCodeBase().toString()+destionationPath;
URL url = new URL(path);
con = (HttpURLConnection) url.openConnection();
//konfigurujemy połączenie
con.setUseCaches(false);
con.setDefaultUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
String boundary = "JakisSeparator", boundary2 = "JakisSeparator2", boundary3 = "JakisSeparator3";
//String boundary = MultipartFormOutputStream.createBoundary();
con.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
con.setDoOutput(true);
con.setDoInput(true);
String endl = "\r\n";
out = new DataOutputStream(con.getOutputStream());
//wpisuję odpowiednie ciało żądania
out.writeBytes("--"+boundary+endl);
out.writeBytes("Content-Disposition: form-data; name=\""+fileFieldName+"\"; filename=\"" + URLEncoder.encode(f.getName()) +"\"" + endl);
out.writeBytes("Content-Type: application/octet-stream"+endl);
out.writeBytes(endl);
//zapisuję plik w ciele żądania
byte[] bytes = new byte[1024];
int read = 0;
while((read = in.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
out.writeBytes(endl+"--"+boundary+"--"+endl);
out.flush();
out2 = new DataOutputStream(con.getOutputStream());
out2.writeBytes("--"+boundary+endl);
out2.writeBytes("Content-Disposition: form-data; name=\""+fileFieldName2+"\"; filename=\"" + URLEncoder.encode(f2.getName()) +"\"" + endl);
out2.writeBytes("Content-Type: application/octet-stream"+endl);
out2.writeBytes(endl);
byte[] bytes2 = new byte[1024];
int read2 = 0;
while((read2 = in2.read(bytes2)) != -1)
{
out2.write(bytes2, 0, read2);
}
out2.writeBytes(endl+"--"+boundary+"--"+endl);
out2.flush();
out3 = new DataOutputStream(con.getOutputStream());
out3.writeBytes("--"+boundary+endl);
out3.writeBytes("Content-Disposition: form-data; name=\""+fileFieldName3+"\"; filename=\"" + URLEncoder.encode(f3.getName()) +"\"" + endl);
out3.writeBytes("Content-Type: application/octet-stream"+endl);
out3.writeBytes(endl);
byte[] bytes3 = new byte[1024];
int read3 = 0;
while((read3 = in3.read(bytes3)) != -1)
{
out3.write(bytes3, 0, read3);
}
out3.writeBytes(endl+"--"+boundary+"--"+endl);
out3.flush();
//wysłanie żądania
con.connect();