Results 1 to 2 of 2
  1. #1
    anurit is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default 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?

    Java 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();
    Last edited by anurit; 03-04-2010 at 08:10 PM.

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    366
    Rep Power
    6

    Default

    Your are victim of your own code style :)

    Don' t write code for every file you send
    what if you have 100000000 files???

    Write 1 method for file sending, then just use arguments

    public void sendFile(String fileName...)

    Pay attention on those buffers and streams and how you read/write
    and close them.

    There are many examples of POST file from java code find one that
    is good for you be sure you understand all steps from creation connection
    to closing it.

    regards !

Similar Threads

  1. Sending MTOM Attachment through SOAP message
    By suchismitasuchi in forum Advanced Java
    Replies: 0
    Last Post: 01-19-2009, 02:22 PM
  2. Sending .GIF files over the network
    By nadia in forum Networking
    Replies: 8
    Last Post: 12-30-2008, 12:57 PM
  3. Sending files over sockets!
    By rameshraj in forum Networking
    Replies: 2
    Last Post: 05-30-2008, 11:18 PM
  4. sending jar files from client to server?
    By gobinathm in forum New To Java
    Replies: 2
    Last Post: 11-13-2007, 06:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •