Подтвердить что ты не робот

Отправка изображений на веб-сервер

Мне нужно создать приложение, которое должно отправлять изображения с телефона на веб-сервер. К сожалению, я действительно не знаю, как это сделать. Может кто-нибудь помочь мне, пожалуйста?

4b9b3361

Ответ 1

Используйте веб-службу для достижения этой задачи.

Чтобы использовать веб-службу в android, перейдите по этим ссылкам.

Ответ 2

Вот код, который я использую для загрузки изображений на удаленный сервер с использованием сырых сокетов. Плюсы сырого сокета над httpclient - это то, что вы можете отобразить индикатор выполнения загрузки.

Отказ от ответственности: большинство из этого кода в основном происходит из stackoverflow.

/**
 * Asynchronous task to upload file to server
 */
class UploadImageTask extends AsyncTask<File, Integer, Boolean> {

    /** Upload file to this url */
    private static final String UPLOAD_URL = "http://thibault-laptop:8080/report";

    /** Send the file with this form name */
    private static final String FIELD_FILE = "file";
    private static final String FIELD_LATITUDE = "latitude";
    private static final String FIELD_LONGITUDE = "longitude";

    /**
     * Prepare activity before upload
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        setProgressBarIndeterminateVisibility(true);
        mConfirm.setEnabled(false);
        mCancel.setEnabled(false);
        showDialog(UPLOAD_PROGRESS_DIALOG);
    }

    /**
     * Clean app state after upload is completed
     */
    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        setProgressBarIndeterminateVisibility(false);
        mConfirm.setEnabled(true);
        mDialog.dismiss();

        if (result) {
            showDialog(UPLOAD_SUCCESS_DIALOG);
        } else {
            showDialog(UPLOAD_ERROR_DIALOG);
        }
    }

    @Override
    protected Boolean doInBackground(File... image) {
        return doFileUpload(image[0], UPLOAD_URL);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

        if (values[0] == 0) {
            mDialog.setTitle(getString(R.string.progress_dialog_title_uploading));
        }

        mDialog.setProgress(values[0]);
    }

    /**
     * Upload given file to given url, using raw socket
     * @see http://stackoverflow.com/questions/4966910/androidhow-to-upload-mp3-file-to-http-server
     *
     * @param file The file to upload
     * @param uploadUrl The uri the file is to be uploaded
     *
     * @return boolean true is the upload succeeded
     */
    private boolean doFileUpload(File file, String uploadUrl) {
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        String separator = twoHyphens + boundary + lineEnd;
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        int sentBytes = 0;
        long fileSize = file.length();

        // The definitive url is of the kind:
        // http://host/report/latitude,longitude
        uploadUrl += "/" + mLocation.getLatitude() + "," + mLocation.getLongitude();

        // Send request
        try {
            // Configure connection
            URL url = new URL(uploadUrl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("PUT");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            publishProgress(0);

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

            // Send location params
            writeFormField(dos, separator, FIELD_LATITUDE, "" + mLocation.getLatitude());
            writeFormField(dos, separator, FIELD_LONGITUDE, "" + mLocation.getLongitude());

            // Send multipart headers
            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"" + FIELD_FILE + "\";filename=\""
                    + file.getName() + "\"" + lineEnd);
            dos.writeBytes(lineEnd);

            // Read file and create buffer
            FileInputStream fileInputStream = new FileInputStream(file);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // Send file data
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                // Write buffer to socket
                dos.write(buffer, 0, bufferSize);

                // Update progress dialog
                sentBytes += bufferSize;
                publishProgress((int)(sentBytes * 100 / fileSize));

                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            // send multipart form data necesssary after file data
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            dos.flush();
            dos.close();
            fileInputStream.close();
        } catch (IOException ioe) {
            Log.e(TAG, "Cannot upload file: " + ioe.getMessage(), ioe);
            return false;
        }

        // Read response
        try {
            int responseCode = conn.getResponseCode();
            return responseCode == 200;
        } catch (IOException ioex) {
            Log.e(TAG, "Upload file failed: " + ioex.getMessage(), ioex);
            return false;
        } catch (Exception e) {
            Log.e(TAG, "Upload file failed: " + e.getMessage(), e);
            return false;
        }
    }

    private void writeFormField(DataOutputStream dos, String separator, String fieldName, String fieldValue) throws IOException
    {
        dos.writeBytes(separator);
        dos.writeBytes("Content-Disposition: form-data; name=\"" + fieldName + "\"\r\n");
        dos.writeBytes("\r\n");
        dos.writeBytes(fieldValue);
        dos.writeBytes("\r\n");
    }
}

Чтобы начать загрузку, используйте следующую команду:

new UploadImageTask().execute(new File(imagePath));

Ответ 3

Я использовал веб-сервис Rest и класс DefaultHttpClient в Android. Чтобы создать образец веб-сервиса REST и развернуть его в Apache Tomcat, следуйте руководству Vogella

Чтобы сделать услугу остальной службы приемлемой, требуется многостраничный тип содержимого на стороне сервера.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json")
public String uploadFile(@FormDataParam("image") InputStream uploadedInputStream,
        @FormDataParam("image") FormDataContentDisposition fileDetail) {

    String uploadedFileLocation = "e://game/" + fileDetail.getFileName();
    boolean response=false;
    // save it
    try{
        OutputStream out = null;
        int read = 0;
        byte[] bytes = new byte[1024]; 
        out = new FileOutputStream(new File(uploadedFileLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
        return response=true;
    }catch(IOException e){
        e.printStackTrace();
    }
    return response;

}

В стороне Android для отправки изображения (я сделал в doInBackground AsyncTask)

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("http://"+ip+":8080/MiniJarvisFaceServer/image");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("image", new FileBody(file));
            postRequest.setEntity(reqEntity);
            ResponseHandler<String> handler = new BasicResponseHandler();           
            String response = httpClient.execute(postRequest,handler);
            Log.d("Response", response);
            httpClient.getConnectionManager().shutdown();

Ответ 4

Чтобы выполнить HTTP-запрос, вы можете использовать DefaultHttpClient класс и HttpPost класс

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://your.site/your/service");
// set some headers if needed
post.addHeader(....);
// and an eclosed entity to send
post.setEntity(....);
// send a request and get response (if needed)
InputStream responseStream = client.execute(post)..getEntity().getContent();

Метод добавления объекта для запроса зависит от того, как работает ваш удаленный serivice.

Ответ 5

Следуйте этому руководству. Он использует PHP на стороне сервера. Я использовал Android Studio и httpmime.4.3.6 и работал как шарм http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/

Он также поддерживает видео, и он показывает, как вы можете ответить с некоторыми результатами с сервера. Единственный сложный бит - убедиться, что вы используете HttClient для Android и правильную версию HttpMime. Прямо сейчас HttpMime 4.4.x не работает, и он потратил неделю на мое время. используйте 4.3.6