mardi 26 juillet 2016

Why HttpURLConnection getresponseCode spend too long time?

I'm developing file upload (multiple).

when I do file upload, this line need the time quite long time(more than 10sec) (Even I got error sometimes..)

int serverResponseCode = connection.getResponseCode();

when I got error, the responseCode is 500, but I don't know why It is error...

the error stream is(run time error) :

<!DOCTYPE html><html>    <head>        <title>런타임 오류</title>        <meta name="viewport" content="width=device-width" />        <style>         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}...

It is my source.

public String UploadJPEG(ArrayList<Bitmap> bitmap, String sPath, String sFileName) {

        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        InputStream is = null;
        InputStreamReader readStream;
        String urlServer = URL + "?PATH=" + sPath;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";

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

        String serverResponseMessage = null;
        String response = null;

        try {
            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            connection.setConnectTimeout(15000);
            connection.setReadTimeout(15000);

            ByteArrayOutputStream bos=null;
            ByteArrayInputStream bs =null;
            for (int i = 0; i < bitmap.length; i++) {
                String fileName = sFileName;
                bos = new ByteArrayOutputStream();
                bitmap.get(i).compress(CompressFormat.JPEG, 80, bos);
                byte[] bitmapdata = bos.toByteArray();
                bs = new ByteArrayInputStream(bitmapdata);
                bitmap.get(i).recycle();

                outputStream = new DataOutputStream(connection.getOutputStream());
                outputStream.writeBytes(twoHyphens + boundary + lineEnd);
                fileName = fileName.replaceAll("##", String.valueOf(i));
                outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""+ fileName + "\"" + lineEnd);
                outputStream.writeBytes(lineEnd);

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

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

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

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


            int serverResponseCode = connection.getResponseCode();
            if (serverResponseCode == HttpURLConnection.HTTP_OK) {
                is = new BufferedInputStream(connection.getInputStream());
            } else {
                is = new BufferedInputStream(connection.getErrorStream());
            }
            serverResponseMessage = connection.getResponseMessage();

            // ***

            readStream = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(readStream);
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            bs.close();
            bos.close();
            outputStream.flush();
            outputStream.close();
            connection.disconnect();

            return sb.toString();
        } catch (Exception ex) {
            return null;
        }
    }

I'm really really real wondering what it is.... why the getresponseCode is too late?? As I know It is just get a response code 400 or 500 something like this... Can anyone help?




Aucun commentaire:

Enregistrer un commentaire