我向服务器发送了byte[]
。我可以计算我要发送的byte[]
大小,但是如何获得已发送的byte[]
大小?我希望实现它,以便向用户显示%{。}}中的progressBar
。
这是我的代码:
public static JSONObject sentJsonToServer(final URL url, final byte[] data, final String newValue) {
ExecutorService ex = Executors.newCachedThreadPool();
Future<JSONObject> objectFuture = ex.submit(new Callable<JSONObject>() {
@Override
public JSONObject call() throws Exception {
BufferedOutputStream bos = null;
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", newValue);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
bos = new BufferedOutputStream(urlConnection.getOutputStream());
bos.write(data);
bos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// returns POST request
if (urlConnection != null) {
return getJSONFromUrl(urlConnection);
} else {
throw new NullPointerException();
}
}
});
JSONObject responseJson = null;
try {
responseJson = objectFuture.get();
} catch (InterruptedException | ExecutionException | NullPointerException e) {
e.printStackTrace();
}
System.out.println("LAST JSOOOOOOOOOOON!!!!!!!!!!!!!!! " + responseJson);
return responseJson;
}
我尝试实现类似ProgressListener()
的smth但没有成功(
我是如何做到的?