Android:使用AsyncTask上传大文件

时间:2014-05-08 17:03:31

标签: android file-upload upload android-asynctask

我尝试将图片上传到服务器。代码工作的图像大小很小,如163Ko,但当我尝试上传10Mb的大图像时,它不起作用。任何人都可以向我解释问题的根源。

此代码是否不允许我上传大文件?

提前谢谢。

public class UploadFileFroURL extends AsyncTask<String, String, String> {

    private String upLoadServerUri = "http://192.168.1.150:8080/UploadToServer.php";


    private OnTaskCompletedUpload listener;

    public UploadFileFroURL (OnTaskCompletedUpload listener){
        this.listener=listener;
    }

    public UploadFileFroURL() {
        // TODO Auto-generated constructor stub
    }
    @Override
    protected String doInBackground(String... content) {

        long size = uploadFile(content[0]);

        return " ";
    }
    public long uploadFile(String sourceFileUri)
    {   long actuaStartlTime = 0 ;
        double debutPaquet ;
        double[] tabTotalDebit= new double [10];
        String fileName = sourceFileUri;
        long TotalSize=0;
        HttpURLConnection comm = null;
        DataOutputStream dos = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead,bytesReadBis,bytesAvailable,bufferSize;
        byte []buffer;
        int maxBufferSize =   10240 ;
        sourceFile = new File(sourceFileUri);

        if(!sourceFile.isFile())
        {
            //dialog.dismiss();
            Log.e("UploadFile", "Source File not Exist: "+ imagepath);

            return 0;
        }
        else
        {
            try
            {
                FileInputStream fis = new FileInputStream(sourceFile);
                URL url = new URL(upLoadServerUri);

                comm = (HttpURLConnection)url.openConnection();
                comm.setDoInput(true);
                comm.setDoOutput(true);
                comm.setUseCaches(false);
                comm.setRequestMethod("POST");
                comm.setRequestProperty("Connection", "Keep-Alive");
                comm.setRequestProperty("ENCTYPE", "multipart/form-data");
                comm.setRequestProperty("Content-Type", "multipart/form-data;boundary=" +boundary);
                comm.setRequestProperty("uploaded_file", fileName);
                dos = new DataOutputStream(comm.getOutputStream());
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                bytesAvailable = fis.available();
                TotalSize = bytesAvailable ;
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                bytesRead = fis.read(buffer,0,bufferSize);
                long global = System.currentTimeMillis();
                debutPaquet = System.currentTimeMillis() ;
                double totalTime =0;

                while(bytesRead > 0)

                { dos.write(buffer,0,bufferSize);
                    bytesAvailable = fis.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fis.read(buffer,0,bufferSize);
                    publishProgress (bytesRead+"");

                }

                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                serverResonseCode = comm.getResponseCode();
                String serverResponseMessage = comm.getResponseMessage();
                if(serverResonseCode == 200)
                { String msg = "File Upload Completed. \n\n See uploaded file here : /var/www/uploads";
                }

                fis.close();
                dos.flush();
                dos.close();
            }
            catch(MalformedURLException e)
            {

                e.printStackTrace();

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

                e.printStackTrace();

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

            return TotalSize;
        }
    }
    protected void onProgressUpdate(String... progress) {
    }
    protected void onPostExecute(String reslut) {
        Log.i("msg","Upload comlete. \n\n See Upload file here : : /var/www/uploads ");
    }
}

修改

这是服务器中的php代码:

    <?php

$file_path = "uploads/";

    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }



?>

0 个答案:

没有答案