使用httpurlconnection多个上传图像文件

时间:2017-07-03 18:44:01

标签: java php httpurlconnection

我在文件夹中有很多图像,我想使用httpurlconnection将该文件在该文件夹中上传到php服务器上。

到现在为止,我只用一张照片制作了它,就像我给你看的代码一样。

    public class SendImage {
    private final String CrLf = "\r\n";
    public static void main(String[] args) {

        SendImage image = new SendImage();
        image.httpConn();
    }
    private void httpConn(){
        URLConnection lig = null;
        OutputStream os = null;
        InputStream is = null;

        try{
            String urlParameters =  "subPasta=sandro&nomepc=Sandro-PC&printPasta=Printscreens";;
            byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
            int postDataLength = postData.length;
            URL url = new URL("http://192.168.0.105/dashboard3/uploadImg.php");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setInstanceFollowRedirects(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("charset", "utf-8");
            conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
            conn.setUseCaches(false);

            System.out.println("url: "+ url);
            lig = url.openConnection();
            lig.setDoOutput(true);

            FileInputStream imgIs = new FileInputStream(new File("C:\\Users\\Sandro\\workspace\\testeLogin\\screenshot\\2017_3_16_3_59.png"));

            byte[] imgData = new byte[imgIs.available()];
            imgIs.read(imgData);

            String message1 = "";
            message1 += "-----------------------------4664151417711" + CrLf;
            message1 += "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"image.png\"" + 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;

            lig.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
            // might not need to specify the content-length when sending chunked
            // data.
            lig.setRequestProperty("Content-Length", String.valueOf((message1
             .length() + message2.length() + imgData.length)));

            System.out.println("open os");
            os = lig.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 = lig.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) {
            }
        }
    }

}

但我想发送该文件夹中的所有照片

如果可以,请告诉我php中的代码。

0 个答案:

没有答案