在android中将多个图像上传到服务器

时间:2017-01-25 04:51:01

标签: php android arrays json image-uploading

我需要在我的Android应用程序中一次上传多个图像到服务器。我已经从图库部分选择了多个图像,并在Arraylist中存储了所选图像的路径。成功上传后,数据以ajson格式重新检索如flollows

[{"id":"18","user_id":"136","title":"title","url":[]},{"id":"20","user_id":"136","title":"title","url":[]}]

如何将所选的图像格式的图像传递给服务器..请帮助

这是我的uploding java类

Vis_Upload.java

 public class Vis_Upload {


String uris[];
    public static final String UPLOAD_URL= "link to upload";

    private int serverResponseCode;

    public String uploadVideo(ArrayList<String> mArrayUri1, String title, String user_id) {
uris=new String[mArrayUri1.size()];
        for(int i=0;i<mArrayUri1.size();i++)
        {
            System.out.println("file URI  (in Vis_Upload class)   : "+mArrayUri1.get(i));
            uris[i]=mArrayUri1.get(i).toString();
        }

//replacing 'file' with  'uris[0]'
        String fileName =uris[0].replaceAll(" ","");

        System.out.println("filename (in Vis_Upload class)   : "+fileName);

        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;

        File sourceFile = new File(uris[0]);
        if (!sourceFile.isFile()) {
            Log.e("Huzza", "Source File Does not exist");
            return null;
        }
        else {

            try {
                FileInputStream fileInputStream = new FileInputStream(sourceFile);
                URL url = new URL(UPLOAD_URL);
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                conn.setRequestProperty("photo", fileName);
                conn.setRequestProperty("title", title);
                conn.setRequestProperty("user_id", user_id);

                dos = new DataOutputStream(conn.getOutputStream());




                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"title\""+ lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(title);
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);

                dos.writeBytes("Content-Disposition: form-data; name=\"user_id\""+ lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(user_id);
                dos.writeBytes(lineEnd);

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"photo\";filename=\"" + fileName + "\"" + lineEnd);
                dos.writeBytes(lineEnd);


                bytesAvailable = fileInputStream.available();
                Log.i("Huzza", "Initial .available : " + bytesAvailable);

                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }

                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                serverResponseCode = conn.getResponseCode();

                fileInputStream.close();
                dos.flush();
                dos.close();
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }


            if (serverResponseCode == 200) {
                StringBuilder sb = new StringBuilder();
                try {
                    BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                            .getInputStream()));
                    String line;
                    while ((line = rd.readLine()) != null) {
                        sb.append(line);
                    }
                    rd.close();
                } catch (Exception ioex) {
                    ioex.printStackTrace();
                }

                System.out.println("(in Vis_upload)sb.toString() : " + sb.toString());
                return sb.toString();
            } else {
                return "Could not upload";
            }
        }
    }
}

0 个答案:

没有答案