Upload and POST file to PHP page with Java

前端 未结 6 1450
臣服心动
臣服心动 2020-12-01 10:14

I need a way to upload a file and POST it into php page...

My php page is:



        
6条回答
  •  失恋的感觉
    2020-12-01 10:28

    This is an old thread, but for the benefit of others, here is a fully working example of exactly what the op asks for:

    PHP server code:

     
    

    Java client code:

    import java.io.OutputStream;
    import java.io.InputStream;
    import java.net.URLConnection;
    import java.net.URL;
    import java.net.Socket;
    
    public class Main {
        private final String CrLf = "\r\n";
    
        public static void main(String[] args) {
            Main main = new Main();
            main.httpConn();
        }
    
        private void httpConn() {
            URLConnection conn = null;
            OutputStream os = null;
            InputStream is = null;
    
            try {
                URL url = new URL("http://localhost/test/post.php");
                System.out.println("url:" + url);
                conn = url.openConnection();
                conn.setDoOutput(true);
    
                String postData = "";
    
                InputStream imgIs = getClass().getResourceAsStream("/test.jpg");
                byte[] imgData = new byte[imgIs.available()];
                imgIs.read(imgData);
    
                String message1 = "";
                message1 += "-----------------------------4664151417711" + CrLf;
                message1 += "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.jpg\""
                        + CrLf;
                message1 += "Content-Type: image/jpeg" + CrLf;
                message1 += CrLf;
    
                // the image is sent between the messages in the multipart message.
    
                String message2 = "";
                message2 += CrLf + "-----------------------------4664151417711--"
                        + CrLf;
    
                conn.setRequestProperty("Content-Type",
                        "multipart/form-data; boundary=---------------------------4664151417711");
                // might not need to specify the content-length when sending chunked
                // data.
                conn.setRequestProperty("Content-Length", String.valueOf((message1
                        .length() + message2.length() + imgData.length)));
    
                System.out.println("open os");
                os = conn.getOutputStream();
    
                System.out.println(message1);
                os.write(message1.getBytes());
    
                // SEND THE IMAGE
                int index = 0;
                int size = 1024;
                do {
                    System.out.println("write:" + index);
                    if ((index + size) > imgData.length) {
                        size = imgData.length - index;
                    }
                    os.write(imgData, index, size);
                    index += size;
                } while (index < imgData.length);
                System.out.println("written:" + index);
    
                System.out.println(message2);
                os.write(message2.getBytes());
                os.flush();
    
                System.out.println("open is");
                is = conn.getInputStream();
    
                char buff = 512;
                int len;
                byte[] data = new byte[buff];
                do {
                    System.out.println("READ");
                    len = is.read(data);
    
                    if (len > 0) {
                        System.out.println(new String(data, 0, len));
                    }
                } while (len > 0);
    
                System.out.println("DONE");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("Close connection");
                try {
                    os.close();
                } catch (Exception e) {
                }
                try {
                    is.close();
                } catch (Exception e) {
                }
                try {
    
                } catch (Exception e) {
                }
            }
        }
    }
    

    you can figure out how to adapt it for your site, but I have tested the above code and it works. I can't take credit for it though >> See Original Post

提交回复
热议问题