web-dev-qa-db-ja.com

マルチパートフォームデータと画像をAndroidのサーバーにアップロードする方法は?

Android codeでサーバーにマルチパートエンティティイメージをアップロード中のステータスコード500

Html形式:(サーバーに正常に画像を追加できます)

 <form method="post" action="http://xyz/upload_picture" enctype="multipart/form-data">

      Sample Picture Upload Form Submit

      <br/><br/>

      API key: <input type="text" name="key" value="abc"><br/><br/>
      Login: <input type="text" name="login" value="text"><br/>
      Password: <input type="password" name="password" value="text"><br/><br/>

      Property ID:<input type="text" name="property_id" value="111"><br/>
      Picture File:<input type="file" name="picture"><br/><br/>

      <br/><br/>
      <input type="submit" name="" value="Upload Picture"><br/>

    </form>

Androidコード:(ステータスコード500を返します)

   HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://xyz/upload_picture");

            try {
                MultipartEntity entity = new MultipartEntity();

                entity.addPart("key", new StringBody("abc"));
                entity.addPart("login", new StringBody("abc"));
                entity.addPart("password", new StringBody("test"));
                entity.addPart("property_id", new StringBody("111"));


                File file = new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_DCIM).toString()
                        + "/Camera/Test.jpg");
                entity.addPart("picture", new FileBody(file));

                httppost.setEntity(entity);
                HttpResponse response = httpclient.execute(httppost);

                Log.e("test", "SC:" + response.getStatusLine().getStatusCode());

                HttpEntity resEntity = response.getEntity();

                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent(), "UTF-8"));
                String sResponse;
                StringBuilder s = new StringBuilder();

                while ((sResponse = reader.readLine()) != null) {
                    s = s.append(sResponse);
                }
                Log.e("test", "Response: " + s);
} catch (ClientProtocolException e) {
        } catch (IOException e) {
        }
12
PrvN

私のように、マルチパートアップロードに苦労していました。 this Android snippet。

public String multipartRequest(String urlTo, Map<String, String> parmas, String filepath, String filefield, String fileMimeType) throws CustomException {
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        InputStream inputStream = null;

        String twoHyphens = "--";
        String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
        String lineEnd = "\r\n";

        String result = "";

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;

        String[] q = filepath.split("/");
        int idx = q.length - 1;

        try {
            File file = new File(filepath);
            FileInputStream fileInputStream = new FileInputStream(file);

            URL url = new URL(urlTo);
            connection = (HttpURLConnection) url.openConnection();

            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
            connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

            outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\"" + q[idx] + "\"" + lineEnd);
            outputStream.writeBytes("Content-Type: " + fileMimeType + lineEnd);
            outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);

            outputStream.writeBytes(lineEnd);

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

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

            outputStream.writeBytes(lineEnd);

            // Upload POST Data
            Iterator<String> keys = parmas.keySet().iterator();
            while (keys.hasNext()) {
                String key = keys.next();
                String value = parmas.get(key);

                outputStream.writeBytes(twoHyphens + boundary + lineEnd);
                outputStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd);
                outputStream.writeBytes("Content-Type: text/plain" + lineEnd);
                outputStream.writeBytes(lineEnd);
                outputStream.writeBytes(value);
                outputStream.writeBytes(lineEnd);
            }

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


            if (200 != connection.getResponseCode()) {
                throw new CustomException("Failed to upload code:" + connection.getResponseCode() + " " + connection.getResponseMessage());
            }

            inputStream = connection.getInputStream();

            result = this.convertStreamToString(inputStream);

            fileInputStream.close();
            inputStream.close();
            outputStream.flush();
            outputStream.close();

            return result;
        } catch (Exception e) {
            logger.error(e);
            throw new CustomException(e);
        }

    }

    private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

呼び出しコード:

//setup params
Map<String, String> params = new HashMap<String, String>(2);
        params.put("foo", hash);
        params.put("bar", caption);

String result = multipartRequest(URL_UPLOAD_VIDEO, params, pathToVideoFile, "video", "video/mp4");
//next parse result string
34
scottyab

これは、外部HTTPCoreとそのようなライブラリを使用せずに機能したソリューションです。私は64Kメソッドの問題に直面していたので、HTTPCoreライブラリを回避するオプションが残っていません

import Java.util.List;

import Java.io.BufferedReader;

import Java.io.File;
import Java.io.FileInputStream;
import Java.io.IOException;
import Java.io.InputStreamReader;
import Java.io.OutputStream;
import Java.io.OutputStreamWriter;
import Java.io.PrintWriter;
import Java.net.HttpURLConnection;
import Java.net.URL;
import Java.net.URLConnection;
import Java.util.ArrayList;
import Java.util.List;

/**
 * This utility class provides an abstraction layer for sending multipart HTTP
 * POST requests to a web server.
 *
 * @author www.codejava.net
 */
public class MultipartUtility {
    private final String boundary;
    private static final String LINE_FEED = "\r\n";
    private HttpURLConnection httpConn;
    private String charset;
    private OutputStream outputStream;
    private PrintWriter writer;

    /**
     * This constructor initializes a new HTTP POST request with content type
     * is set to multipart/form-data
     *
     * @param requestURL
     * @param charset
     * @throws IOException
     */
    public MultipartUtility(String requestURL, String charset)
            throws IOException {
        this.charset = charset;

        // creates a unique boundary based on time stamp
        boundary = "===" + System.currentTimeMillis() + "===";

        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true); // indicates POST method
        httpConn.setDoInput(true);
        httpConn.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        httpConn.setRequestProperty("User-Agent", "CodeJava Agent");
        httpConn.setRequestProperty("Test", "Bonjour");
        outputStream = httpConn.getOutputStream();
        writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
                true);
    }

    /**
     * Adds a form field to the request
     *
     * @param name  field name
     * @param value field value
     */
    public void addFormField(String name, String value) {
        writer.append("--" + boundary).append(LINE_FEED);
        writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
                .append(LINE_FEED);
        writer.append("Content-Type: text/plain; charset=" + charset).append(
                LINE_FEED);
        writer.append(LINE_FEED);
        writer.append(value).append(LINE_FEED);
        writer.flush();
    }

    /**
     * Adds a upload file section to the request
     *
     * @param fieldName  name attribute in <input type="file" name="..." />
     * @param uploadFile a File to be uploaded
     * @throws IOException
     */
    public void addFilePart(String fieldName, File uploadFile)
            throws IOException {
        String fileName = uploadFile.getName();
        writer.append("--" + boundary).append(LINE_FEED);
        writer.append(
                "Content-Disposition: form-data; name=\"" + fieldName
                        + "\"; filename=\"" + fileName + "\"")
                .append(LINE_FEED);
        writer.append(
                "Content-Type: "
                        + URLConnection.guessContentTypeFromName(fileName))
                .append(LINE_FEED);
        writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
        writer.append(LINE_FEED);
        writer.flush();

        FileInputStream inputStream = new FileInputStream(uploadFile);
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.flush();
        inputStream.close();

        writer.append(LINE_FEED);
        writer.flush();
    }

    /**
     * Adds a header field to the request.
     *
     * @param name  - name of the header field
     * @param value - value of the header field
     */
    public void addHeaderField(String name, String value) {
        writer.append(name + ": " + value).append(LINE_FEED);
        writer.flush();
    }

    /**
     * Completes the request and receives response from the server.
     *
     * @return a list of Strings as response in case the server returned
     * status OK, otherwise an exception is thrown.
     * @throws IOException
     */
    public List<String> finish() throws IOException {
        List<String> response = new ArrayList<String>();

        writer.append(LINE_FEED).flush();
        writer.append("--" + boundary + "--").append(LINE_FEED);
        writer.close();

        // checks server's status code first
        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpConn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                response.add(line);
            }
            reader.close();
            httpConn.disconnect();
        } else {
            throw new IOException("Server returned non-OK status: " + status);
        }

        return response;
    }
}

使用法

private void uploadMedia() {
        try {

            String charset = "UTF-8";
            File uploadFile1 = new File("/sdcard/myvideo.mp4");
            String requestURL = Data.BASE_URL+Data.URL_UPLOAD_REACTION_TEST;

            MultipartUtility multipart = new MultipartUtility(requestURL, charset);

//            multipart.addHeaderField("User-Agent", "CodeJava");
//            multipart.addHeaderField("Test-Header", "Header-Value");

            multipart.addFormField("friend_id", "Cool Pictures");
            multipart.addFormField("userid", "Java,upload,Spring");

            multipart.addFilePart("uploadedfile", uploadFile1);

            List<String> response = multipart.finish();

            Log.v("rht", "SERVER REPLIED:");

            for (String line : response) {
                Log.v("rht", "Line : "+line);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

アップロードを受け入れるPHPコード

<?php

    $friend_id = $_REQUEST['friend_id'];
    $userid = $_REQUEST['userid'];

    echo 'friend_id : '.$friend_id. ' userid '.$userid;

    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], "./uploads/".$_FILES["uploadedfile"]["name"]);

?>
5
Rohit Mandiwal

ステータスコード500は、サーバーの設定に問題があることを意味します。コードとは関係ありません。 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html を参照してください。

0
Vikram Singh

これを試してみてください

File file = new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_DCIM).toString()
                        + "/Camera/Test.jpg");
                entity.addPart("picture", new FileBody(file,"image/jpg")); //Update here
0
Biraj Zalavadia

私はこの答えを投稿するのに遅れていることを知っていますが、それでも誰かがAndroidのサーバーに複数の画像をアップロードしたい場合

String uploadMultipleFiles(Api api, HashMap<String, ArrayList<File>> fileListMap)
            throws IOException {

        String responseS;
        OkHttpClient timeOut = getOkHttpClient(api.getTimeOut());

        MultipartBody.Builder multipartBuilder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        int j = 0;
        ArrayList<File> fileList;
        for (Map.Entry<String, ArrayList<File>> entry : fileListMap.entrySet()) {
            String key = entry.getKey();
            fileList = entry.getValue();

            multipartBuilder.addFormDataPart("data[" + j + "].name", key);

            if (fileList != null) {
                for (int i = 0; i < fileList.size(); i++) {

                    File fileListRequest = fileList.get(i);

                    if (fileListRequest.exists()) {
                        String ext = CommonUtils.getExtension(fileListRequest.getName());
                        /* Changing Media Type whether JPEG or PNG **/
                        final MediaType MEDIA_TYPE = ext.endsWith("png") ? MEDIA_TYPE_PNG :
                                ext.endsWith("jpg") ? MEDIA_TYPE_JPG : MEDIA_TYPE_JPEG;

                        multipartBuilder.addFormDataPart("data[" + j + "].files[" + i + "]",
                                fileListRequest.getName(),
                                RequestBody.create(MEDIA_TYPE, fileListRequest));

                    }
                }
            }
            j++;
        }
        RequestBody requestBody = multipartBuilder.build();
        Request.Builder requestBuilder = new Request.Builder()
                .url(Objects.requireNonNull(api.getUrl()))
                .post(requestBody);
        buildHeaders(requestBuilder);
        Response response = timeOut.newCall(requestBuilder.build()).execute();
        responseS = response.body().string();
        Log.i(api.getName().name(), responseS);
        return responseS;
    } 

上記のコードは、複数の画像をサーバーにアップロードするためのものであり、単一の画像については、以下の単一行のみを追加する必要があります

multipartBuilder.addFormDataPart("fileName",fileListRequest.getName(),RequestBody create(MEDIA_TYPE,fileListRequest));
0
Rahul

こんにちは、たくさんの検索試行錯誤の後、ついにこの友達ができました

//グラフAPIを使用して画像をFacebookにアップロードする方法について

                Bundle params = new Bundle();

                    params.putByteArray("multipart/form-data",byteArray);

                    params.putString("caption",txtcaption.getText().toString());
                    /* make the API call */
                    new GraphRequest(
                            AccessToken.getCurrentAccessToken(),
                            "/me/photos",
                            params,
                            HttpMethod.POST,
                            new GraphRequest.Callback() {
                                public void onCompleted(GraphResponse response) {
                                    /* handle the result */
                                    Log.e("responseImagedata---", response.toString());

                                }
                            }
                    ).executeAsync();
0
yogeshwar