2017-03-15 113 views
0

我想上传图片和视频到网络服务器的块 但上传一个块后,我不得不再次调用outputstream传输下一个块。是否有任何方式,我有呼吁outstream仅once.If没有,那么为什么它是neccassary上传块之后调用的OutputStream每次。 我的代码当前的代码是如何从Android上传大文件到网络服务器

header('Content-type:bitmap;charset=utf-8'); 
$super_parent_dir=$_POST["spd"];//super parent dic 
$parent_dir=$_POST["pd"];//parent dic 
$child_dir=$_POST["cd"];//super_child dic 
$host_no=$_POST["queue_num"];//child_dic 
$image_name=$_POST["image_name"];//file 

$spd_path=$super_parent_dir; 
$pd_path=$spd_path."/".$parent_dir; 
$ch_path=$pd_path."/".$child_dir; 
$f_folder=$ch_path."/".$host_no; 

if(!is_dir($f_folder)) 
mkdir($f_folder, 0777);//echo $f_folder; 

if(isset($_POST["Image_data"])){ 

$econded_string=$_POST["Image_data"]; 
$decode_string=base64_decode($econded_string); 
$path=$f_folder.'/'.$image_name; 
$file=fopen($path,'a'); 
$is_written=fwrite($file,$decode_string); 
fclose($file); 

if($is_written>0){ 
    $connection=mysqli_connect("localhost","root","","imgae_db"); 
    $query="insert into photos values('','$path','$image_name')"; 
    $result= mysqli_query($connection,$query); 

}} 

公共类Media_uploader扩展的AsyncTask {

private String image_file; 
public Media_uploader(File image){ 
    this.image_file= String.valueOf(image); 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
} 

@Override 
protected Void doInBackground(Void... params) { 
    Bitmap bitmap= BitmapFactory.decodeFile(image_file); 
    ByteArrayOutputStream stream=new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream); 

    byte[] array=stream.toByteArray(); 
    String a= Base64.encodeToString(array,Base64.DEFAULT); 


    try { 
     URL url=new URL("http://192.168.1.1**/db_mager/Medaia_Downloader.php"); 
     HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection(); 

     httpURLConnection.setDoOutput(true); 
     OutputStream stream1=httpURLConnection.getOutputStream(); 
     BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(stream1,"UTF-8")); 

     String data= URLEncoder.encode("spd","UTF-8")+"="+URLEncoder.encode(Session_Data.getMyDatabase(),"UTF-8")+"&"+ 
        URLEncoder.encode("pd","UTF-8")+"="+URLEncoder.encode(Session_Data.getMyCity(),"UTF-8")+"&"+ 
        URLEncoder.encode("cd","UTF-8")+"="+URLEncoder.encode(Session_Data.getMyID(),"UTF-8")+"&"+ 
        URLEncoder.encode("queue_num","UTF-8")+"="+URLEncoder.encode(String.valueOf(Session_Data.getActivity_Record()),"UTF-8")+"&"+ 
        URLEncoder.encode("image_name","UTF-8")+"="+URLEncoder.encode(image_file,"UTF-8")+"&"+ 
        URLEncoder.encode("Image_data","UTF-8")+"="+URLEncoder.encode(a,"UTF-8"); 

     bufferedWriter.write(data); 
     bufferedWriter.flush(); 
     bufferedWriter.close(); 
     stream1.close(); 

     InputStream inputStream=httpURLConnection.getInputStream(); 
     inputStream.close(); 

    } catch (java.io.IOException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 
+0

听说多数据上传? –

+0

是与多更好的选择 – Saveen

+0

尝试请提供您对此到目前为止的代码。访问http://stackoverflow.com/help/how-to-ask – Olaia

回答

0

这个代码是利用Android的多部分上传到服务器的一个例子 - 有不同的库,将也支持这一点,所以它是值得你做一些研究,以确定哪些是最适合你,但下面是测试和工作。

public class VideoUploadTask extends AsyncTask<String, String, Integer> { 
    /* This Class is an AsynchTask to upload a video to a server on a background thread 
    * 
    */ 

    private VideoUploadTaskListener thisTaskListener; 
    private String serverURL; 
    private String videoPath; 

    public VideoUploadTask(VideoUploadTaskListener ourListener) { 
     //Constructor 
     Log.d("VideoUploadTask","constructor"); 

     //Set the listener 
     thisTaskListener = ourListener; 
    } 

    @Override 
    protected Integer doInBackground(String... params) { 
     //Upload the video in the background 
     Log.d("VideoUploadTask","doInBackground"); 

     //Get the Server URL and the local video path from the parameters 
     if (params.length == 2) { 
      serverURL = params[0]; 
      videoPath = params[1]; 
     } else { 
      //One or all of the params are not present - log an error and return 
      Log.d("VideoUploadTask doInBackground","One or all of the params are not present"); 
      return -1; 
     } 


     //Create a new Multipart HTTP request to upload the video 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(serverURL); 

     //Create a Multipart entity and add the parts to it 
     try { 
      Log.d("VideoUploadTask doInBackground","Building the request for file: " + videoPath); 
      FileBody filebodyVideo = new FileBody(new File(videoPath)); 
      StringBody title = new StringBody("Filename:" + videoPath); 
      StringBody description = new StringBody("test Video"); 
      MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
      reqEntity.addPart("videoFile", filebodyVideo); 
      reqEntity.addPart("title", title); 
      reqEntity.addPart("description", description); 
      httppost.setEntity(reqEntity); 
     } catch (UnsupportedEncodingException e1) { 
      //Log the error 
      Log.d("VideoUploadTask doInBackground","UnsupportedEncodingException error when setting StringBody for title or description"); 
      e1.printStackTrace(); 
      return -1; 
     } 

     //Send the request to the server 
     HttpResponse serverResponse = null; 
     try { 
      Log.d("VideoUploadTask doInBackground","Sending the Request"); 
      serverResponse = httpclient.execute(httppost); 
     } catch (ClientProtocolException e) { 
      //Log the error 
      Log.d("VideoUploadTask doInBackground","ClientProtocolException"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      //Log the error 
      Log.d("VideoUploadTask doInBackground","IOException"); 
      e.printStackTrace(); 
     } 

     //Check the response code 
     Log.d("VideoUploadTask doInBackground","Checking the response code"); 
     if (serverResponse != null) { 
      Log.d("VideoUploadTask doInBackground","ServerRespone" + serverResponse.getStatusLine()); 
      HttpEntity responseEntity = serverResponse.getEntity(); 
      if (responseEntity != null) { 
       //log the response code and consume the content 
       Log.d("VideoUploadTask doInBackground","responseEntity is not null"); 
       try { 
        responseEntity.consumeContent(); 
       } catch (IOException e) { 
        //Log the (further...) error... 
        Log.d("VideoUploadTask doInBackground","IOexception consuming content"); 
        e.printStackTrace(); 
       } 
      } 
     } else { 
      //Log that response code was null 
      Log.d("VideoUploadTask doInBackground","serverResponse = null"); 
      return -1; 
     } 

     //Shut down the connection manager 
     httpclient.getConnectionManager().shutdown(); 
     return 1; 
    } 

    @Override 
    protected void onPostExecute(Integer result) { 
     //Check the return code and update the listener 
     Log.d("VideoUploadTask onPostExecute","updating listener after execution"); 
     thisTaskListener.onUploadFinished(result); 
    } 

} 
相关问题