S
S
SevenXP2015-12-09 15:22:41
Android
SevenXP, 2015-12-09 15:22:41

Error when uploading a file by upload_url (POST) Android - HttpURLConnection, how to upload a file to classmates?

From the beginning I get the upload_url, then I try to upload the POST file with a request.
When I try to upload a file, I get an error.
{"error_msg":"one.image.server.upload.ContentUploadServerException: BAD_REQUEST","error_code":"4","error_data":"BAD_REQUEST"}
Android Code

public HttpURLConnection postImage(String upload_url, String pathFile) throws IOException {

        URL urlCon = new URL(upload_url);
        HttpURLConnection urlConnection = (HttpURLConnection) urlCon.openConnection();
        urlConnection.setRequestMethod(POST);
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setReadTimeout(35000);
        urlConnection.setConnectTimeout(35000);
        urlConnection.setRequestProperty("Accept", "*/*");
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (U; Android) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
        urlConnection.setRequestProperty("Connection", "keep-alive");
        File file = new File(pathFile);
        String fileName = file.getName();
        String extension = MimeTypeMap.getFileExtensionFromUrl(file.getAbsolutePath());
        String mime = "image/jpeg";
        if (extension != null)
            mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        Log.e("fileName", "filename = " + fileName + " exist = " + file.exists() + " mime = " + mime);
        urlConnection.setRequestProperty("Content-Type", mime);
        urlConnection.setRequestProperty("Content-Length", String.valueOf(file.length()));
        OutputStream os = urlConnection.getOutputStream();
        urlConnection.connect();
        BufferedOutputStream bos = new BufferedOutputStream(os);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        byte[] buff = new byte[1024];
        int i;
        while ((i = bis.read(buff, 0, buff.length)) > 0) bos.write(buff, 0, i);
        bis.close();
        bos.flush();
        bos.close();
        os.close();
        int responseCode = urlConnection.getResponseCode();
        Log.e(TAG, String.format("response code: %d request: %s %s", responseCode, urlConnection.getRequestMethod(), urlConnection.getURL().toString()));
        return urlConnection;
    }

The selected version of the Android SDK 6.0 is why I use HttpURLConnection
Purely on HttpUrlConnection, I tried different methods and write to the beginning of the stream of information. But another method helped, though for Android - MultipartEntity is better not to use, but what would work I use
@SuppressWarnings({"deprecation"})
    private static String multipost(String upload_url, File file) {
        try {
            URL url = new URL(upload_url);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("picture", new FileBody(file));
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.addRequestProperty("Content-length", reqEntity.getContentLength() + "");
            conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());
            OutputStream os = conn.getOutputStream();
            reqEntity.writeTo(conn.getOutputStream());
            os.close();
            conn.connect();
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                return readStream(conn.getInputStream());
            }

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

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question