android - 当使用 conn.setChunkedStreamingMode(1024) 时,服务器端未收到任何数据

标签 android

当我使用 conn.setChunkedStreamingMode(1024) 时,程序会运行并上传文件,但上传的文件在上传文件夹中不可用(未找到)。

当我删除 setchunk.... 行时,文件上传成功并保存在服务器文件夹中。


使用 conn.setChunkedStreamingMode 的主要目的是让我的 android 应用上传大文件大于 16MB

有人请帮我解决这个 OUTOFMEMORY 错误 要么 建议使用其他方式从 Android 应用程序上传大文件。

try {           
            // open a URL connection to the Servlet
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL(upLoadServerUri);

            // Open a HTTP  connection to  the URL
            conn = (HttpURLConnection) url.openConnection(); 

            //conn.setChunkedStreamingMode(0);


            //conn.setFixedLengthStreamingMode((int)sourceFile.length());
            //conn.setFixedLengthStreamingMode(maxBufferSize+headerlength);
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            //conn.setRequestProperty("Content-Encoding","chunked");

            conn.setUseCaches(false); // Don't use a Cached Copy
            conn.setRequestProperty("Transfer-Encoding","chunked");
            conn.setChunkedStreamingMode(1024);
            conn.setRequestMethod("POST");                 


            //conn.setFixedLengthStreamingMode(sourceFile.length());
            //conn.setChunkedStreamingMode((int)sourceFileUri.length() /100);


            //conn.setRequestProperty("Upgrade","HTTP/1.1");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("uploaded_file", fileName); 



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

            dos.writeBytes(twoHyphens + boundary + lineEnd); 
            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                    + fileName + "\"" + lineEnd);

            dos.writeBytes(lineEnd);                               

            // create a buffer of  maximum size
            bytesAvailable = fileInputStream.available(); 

            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            //bufferSize=(int)sourceFile.length();
            buffer = new byte[bufferSize];

            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);  

            while (bytesRead > 0) {   

                dos.write(buffer, 0, bufferSize);
                dialog.incrementProgressBy(1);
                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);

            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();                
            String serverResponseMessage = conn.getResponseMessage();
            String a = conn.getContentEncoding();
            String b = conn.getContentType();
            int c = conn.getContentLength();

            Log.i("uploadFile", "HTTP Response is : " 
                    + serverResponseMessage + ": " + serverResponseCode +"a="+a+b+c);

            if(serverResponseCode == 200){

                runOnUiThread(new Runnable() {
                    public void run() {

                        String msg = "File Upload Completed.";

                        String sharemsg = "http://my url /files/";

                        String linkmsg = uploadFileName;


                        Toast.makeText(MainActivity.this, "File Upload Complete.", 
                                Toast.LENGTH_SHORT).show();

                        Intent i=new Intent(getApplicationContext(),ShareActivity.class);

                        i.putExtra("msg", msg);
                        i.putExtra("sharemsg", sharemsg);
                        i.putExtra("linkmsg", linkmsg);

                        startActivity(i);
                    }
                });                
            }    

            //close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {

            dialog.dismiss();  
            ex.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    messageText.setText("MalformedURLException Exception : check script url.");
                    Toast.makeText(MainActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
                }
            });

            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);  
        } catch (Exception e) {

            dialog.dismiss();  
            e.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    messageText.setText("Opps! no internet connectivity");
                    Toast.makeText(MainActivity.this, "Got Exception : see logcat ", 
                            Toast.LENGTH_SHORT).show();
                }
            });
            Log.e("Upload file to server Exception", "Exception : " 
                    + e.getMessage(), e);  
        }
        dialog.dismiss();       
        return serverResponseCode; 

    } // End else block 

最佳答案

如果您使用自己的服务器端选项(例如 PHP),请确保您设置了正确的选项,例如 max_execution_time 和 max_upload_size

post_max_size
upload_max_filesize
max_execution_time

关于android - 当使用 conn.setChunkedStreamingMode(1024) 时,服务器端未收到任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22037352/

相关文章:

android - 当我滚动浏览时,列表中的复选框会随机选中/取消选中。安卓2.3

android - Android "Active Installs"中的 "Developer Console"计数/百分比是怎么计算出来的?

java - 如何从 reSTLet ClientResource 获取响应?

java - 使用 Android 应用程序运行 JAR MainActivity

android - 在 Android 中以编程方式添加到 RadioGroup 时,RadioButtons 具有不同的样式

android - "No enclosing instance of type"在Android中从另一个类调用方法时出错

android - 如何将具有 ACTION_SEND Intent 的 url 中的图像发送到其他应用程序?

android - 没有陀螺仪的VR

android - 使用 ArrayList<Bitmap> 创建 GIF 图像

java - Android 使用运行时策略来支持不同的 API 级别?