通过DataOutputStream上传文件不起作用

时间:2014-02-23 15:05:26

标签: java android post upload dataoutputstream

我在写一个Android应用程序。此应用程序应通过POST将一些图片上传到我的服务器。 如果我使用小于1MB的图片,它会工作。 但是,如果我使用更大的图像,日志会告诉我图像已上传但图像不在服务器上。 post_max_size是4MB

我的方法来自stackoverflow解决方案,应该可以工作,但它没有,我不知道为什么。

// http post
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("c_user_id", params[1]));
        nameValuePairs.add(new BasicNameValuePair("c_task_id", params[2]));
        nameValuePairs.add(new BasicNameValuePair("c_comment", params[3]));
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(phpPath + "insert_task.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection" + e.toString());
            this.cancel(true);

        }

        String Tag = "UPLOADER";
        String urlString = phpPath + "upload_task.php?user_id=" + params[1]
                + "&task_id=" + params[2];
        HttpURLConnection conn = null;

        String exsistingFileName = params[0];
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        try {
            // ------------------ CLIENT REQUEST

            Log.e(Tag, "Inside second Method");

            FileInputStream fileInputStream = new FileInputStream(new File(
                    exsistingFileName));

            // open a URL connection to the Servlet

            URL url = new URL(urlString);

            // Open a HTTP connection to the URL

            conn = (HttpURLConnection) url.openConnection();

            // Allow Inputs
            conn.setDoInput(true);

            // Allow Outputs
            conn.setDoOutput(true);

            // Don't use a cached copy.
            conn.setUseCaches(false);

            // Use a post method.
            conn.setRequestMethod("POST");

            conn.setRequestProperty("Connection", "Keep-Alive");

            conn.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);

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

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
                    + exsistingFileName + "" + lineEnd);
            dos.writeBytes(lineEnd);

            Log.e(Tag, "Headers are written");

            // create a buffer of maximum size
            int bytesAvailable = fileInputStream.available();   
            int maxBufferSize = 1 * 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
            byte[] buffer = new byte[bufferSize];
            buffer = new byte[bufferSize];
            int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            // Read file

            Log.e("Image length", bytesAvailable + "");
            try {
                while (bytesRead > 0) {
                    try {
                        dos.write(buffer, 0, bufferSize);
                    } catch (OutOfMemoryError e) {
                        e.printStackTrace();

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

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



            Log.i("File Name in Server : ", exsistingFileName);

            fileInputStream.close();
            dos.flush();
            dos.close();
            dos = null;
        } catch (Exception ex) {
            // Exception handling

            Log.e("Send file Exception", ex.getMessage() + "");
            ex.printStackTrace();
        }

0 个答案:

没有答案