HttpURLConnection! Connection.getInputStream is java.io.FileNotFoundException

Posted by user3643283 on Stack Overflow See other posts from Stack Overflow or by user3643283
Published on 2014-06-12T03:11:55Z Indexed on 2014/06/12 3:25 UTC
Read the original article Hit count: 225

I created a method "UPLPAD2" to upload file to server. Splitting my file to packets(10MB). It's OK (100%). But when i call getInputStream, i get FileNotFoundException. I think, in loop, i make new HttpURLConnection to set "setRequestProperty". This is a problem. Here's my code:

@SuppressLint("NewApi")
public int upload2(URL url, String filePath,
        OnProgressUpdate progressCallBack, AtomicInteger cancelHandle)
        throws IOException {
    HttpURLConnection connection = null;
    InputStream fileStream = null;  
    OutputStream out = null;
    InputStream in = null;
    HttpResponse response = new HttpResponse();
    Log.e("Upload_Url_Util", url.getFile());
    Log.e("Upload_FilePath_Util", filePath);
    long total = 0;
    try {
        // Write the request.
        // Read from filePath and upload to server (url)
        byte[] buf = new byte[1024];
        fileStream = new FileInputStream(filePath); 
        long lenghtOfFile = (new java.io.File(filePath)).length();
        Log.e("LENGHT_Of_File", lenghtOfFile + "");

        int totalPacket = 5 * 1024 * 1024; // 10 MB
        int totalChunk = (int) ((lenghtOfFile + (totalPacket - 1)) / totalPacket);
        String headerValue = "";
        String contentLenght = "";

        for (int i = 0; i < totalChunk; i++) {
            long from = i * totalPacket;
            long to = 0;
            if ((from + totalPacket) > lenghtOfFile) {
                to = lenghtOfFile;
            } else {
                to = (totalPacket * (i + 1));
            }
            to = to - 1;
            headerValue = "bytes " + from + "-" + to + "/" + lenghtOfFile;
            contentLenght = "Content-Length:" + (to - from + 1);
            Log.e("Conten_LENGHT", contentLenght);
            connection = client.open(url);

            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Range", headerValue);
            connection.setRequestProperty("Content-Length",
                    Long.toString(to - from + 1));  
            out = connection.getOutputStream();
            Log.e("Lenght_Of_File", lenghtOfFile + "");
            Log.e("Total_Packet", totalPacket + "");
            Log.e("Total_Chunk", totalChunk + "");
            Log.e("Header_Valure", headerValue);

            int read = 1;

            while (read > 0 && cancelHandle.intValue() == 0
                    && total < totalPacket * (i + 1)) {
                read = fileStream.read(buf);
                if (read > 0) {
                    out.write(buf, 0, read);
                    total += read;
                    progressCallBack
                            .onProgressUpdate((int) ((total * 100) / lenghtOfFile));
                }
            }
            Log.e("TOTAL_", total + "------" + totalPacket * (i + 1));
            Log.e("I_", i + "");
            Log.e("LENGHT_Of_File", lenghtOfFile + "");
            if (i < totalChunk - 1) {
                connection.disconnect();
            }
            out.close();
        }
        // Read the response.
        response.setHttpCode(connection.getResponseCode());
        in = connection.getInputStream(); // I GET ERROR HERE.
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new IOException("Unexpected HTTP response: "
                    + connection.getResponseCode() + " "
                    + connection.getResponseMessage());
        }
        byte[] body = readFully(in);
        response.setBody(body);
        response.setHeaderFields(connection.getHeaderFields());
        if (cancelHandle.intValue() != 0) {
            return 1;
        }
        JSONObject jo = new JSONObject(response.getBodyAsString());
        Log.e("Upload_Body_res_", response.getBodyAsString());
        if (jo.has("error")) {
            if (jo.has("code")) {
                int errCode = jo.getInt("code");
                Log.e("Upload_Had_errcode", errCode + "");
                return errCode;
            } else {
                return 504;
            }
        }
        Log.e("RESPONE_BODY_UPLOAD", response.getBodyAsString() + "");
        return 0;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("Http_UpLoad_Response_Exception", e.toString());
        response.setHttpCode(connection.getResponseCode());
        Log.e("ErrorCode_Upload_Util_Return", response.getHttpCode() + "");
        if (connection.getResponseCode() == 200) {
            return 1;
        } else if (connection.getResponseCode() == 0) {
            return 1;
        } else {
            return response.getHttpCode();
        }
        // Log.e("ErrorCode_Upload_Util_Return", response.getHttpCode()+"");
    } finally {
        if (fileStream != null)
            fileStream.close();
        if (out != null)
            out.close();
        if (in != null)
            in.close();
    }
}

And Logcat

06-12 09:39:29.558: W/System.err(30740): java.io.FileNotFoundException: http://download-f77c.fshare.vn/upload/NRHAwh+bUCxjUtcD4cn9xqkADpdL32AT9pZm7zaboHLwJHLxOPxUX9CQxOeBRgelkjeNM5XcK11M1V-x
06-12 09:39:29.558: W/System.err(30740):    at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:187)
06-12 09:39:29.563: W/System.err(30740):    at com.fsharemobile.client.HttpUtil.upload2(HttpUtil.java:383)
06-12 09:39:29.563: W/System.err(30740):    at com.fsharemobile.fragments.ExplorerFragment$7$1.run(ExplorerFragment.java:992)
06-12 09:39:29.568: W/System.err(30740):    at java.lang.Thread.run(Thread.java:856)
06-12 09:39:29.568: E/Http_UpLoad_Response_Exception(30740): java.io.FileNotFoundException: http://download-f77c.fshare.vn/upload/NRHAwh+bUCxjUtcD4cn9xqkADpdL32AT9pZm7zaboHLwJHLxOPxUX9CQxOeBRgelkjeNM5XcK11M1V-x

© Stack Overflow or respective owner

Related posts about java

Related posts about android