我想以图块的形式将图片和视频上传到网络服务器 但是在上传一个块后我必须再次调用outputstream来传输下一个chunk。有什么方法我只需要一次调用outstream。如果不是那么为什么每次上传一个块后调用outputstream是必要的。 我的代码当前代码是
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)
此代码是使用从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);
}
}