我成功从sdcard获取文件,从那里我能够获得输出流,在这里我正在检查它的可用大小,我正在获取大小,现在在使用byteoutputstream上传时,只有1Kb上传到服务器?它可能存在什么问题。 这是我的代码片段,
public String PostData(String path) {
try {
SharedPreferences preferences = getSharedPreferences(
LoginPageActivity.MYPREF, 0);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(
"http://www.idolgreeting.com/app/movie_done.php" /* + postStr */);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
Log.d("System out", "path is "+path);
FileInputStream fileInputStream = new FileInputStream(
new File(path));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int bytesavailable = fileInputStream.available();
Log.d("System out", "bytes available "+bytesavailable);
byte[] buffer = new byte[bytesavailable];
int test = fileInputStream.read(buffer, 0, bytesavailable);
Log.d("System out", "test "+test);
// bos.write(test);
//
int maxBufferSize =1000;
while (test > 0) {
bos.write(buffer, 0, bytesavailable);
bytesavailable = fileInputStream.available();
bytesavailable = Math.min(bytesavailable, maxBufferSize);
test= fileInputStream.read(buffer, 0, bytesavailable);
Log.d("System out", "test in while loop"+test);
}
byte[] data = bos.toByteArray();
Log.d("System out", "Length is "+data.length);
if (imagePath.contains(".mp4")) {
entity.addPart("_filename", new StringBody("newFile.mp4"));
Log.d("System out", "MP4 file ");
} else {
entity.addPart("_filename", new StringBody("newFile.3gp"));
Log.d("System out", "3GP file ");
}
String[] userID = preferences.getString(LoginPageActivity.USER,
null).split("#");
Log.d("System out", "user id " + userID[0]);
Log.d("System out", "user id " + userID[1]);
// entity.addPart("userid", new StringBody(preferences.getString(
// LoginPageActivity.USER, null)));
entity.addPart("userid", new StringBody(userID[0]));
entity.addPart("taskid", new StringBody(bundle.getString("id")));
entity.addPart("_Get", new StringBody("&android=1"));
if (imagePath.contains(".mp4")) {
entity.addPart("uploaded", new ByteArrayBody(data,
"newFile.mp4"));
Log.d("System out", "MP4 file ");
} else {
entity.addPart("uploaded", new ByteArrayBody(data,
"newFile.3gp"));
Log.d("System out", "3GP file ");
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse = reader.readLine();
Log.d("System out", "response " + sResponse);
if (sResponse.equalsIgnoreCase("true")) {
dataFlag = true;
} else {
dataFlag = false;
}
handler.sendEmptyMessage(0);
return sResponse;
} catch (Exception e) {
// if (dialog.isShowing())
// dialog.dismiss();
// Toast.makeText(getApplicationContext(),
// e.getMessage(),
// Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
dataFlag = false;
handler.sendEmptyMessage(0);
return null;
}
}