사용자가 미디어 파일 (예 : 이미지, 사진, 비디오)을 서버에 업로드 할 수있는 앱을 개발 중이며 큰 파일에는 약간의 문제가 있습니다. 업로드 및 다른 문제가 진행되는 동안 진행 상황 표시 줄을보고 기다리십시오. 앱에서 업로드가 종료 될 경우 업로드 코드를 전송해야합니다. 그래서 여기 내 고민이 시작됩니다 - 업로드 코드를 서버로 전송하면 진행률 업데이트 (%)를 어떻게 활동에 보낼 수 있습니까? 당신의 앱이 닫힌 경우백그라운드로 서버에 파일 업로드하기
가public static void uploadMovie(final HashMap<String, String> dataSource, final OnResponseListener finishedListener, final ProgressListener progressListener) {
if (finishedListener != null) {
new Thread(new Runnable() {
public void run() {
try {
//Prepare data-->
String boundary = getMD5(dataSource.size() + String.valueOf(System.currentTimeMillis()));
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.setCharset(Charset.forName("UTF-8"));
for (String key : dataSource.keySet()) {
if (key.equals(MoviesFragmentAdd.USERFILE)) {
FileBody userFile = new FileBody(new File(dataSource.get(key)));
multipartEntity.addPart(key, userFile);
continue;
}
multipartEntity.addPart(key, new StringBody(dataSource.get(key), ContentType.APPLICATION_JSON));
}
HttpEntity entity = multipartEntity.build();
//<--
//Prepare Connection-->
trustAllHosts();
HttpsURLConnection conn = (HttpsURLConnection) new URL(SAKH_URL_API + "/video/addForm/").openConnection();
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("Content-length", entity.getContentLength() + "");
conn.setRequestProperty(entity.getContentType().getName(), entity.getContentType().getValue());
conn.connect();
//<--
// Upload-->
OutputStream os = conn.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
entity.writeTo(baos);
baos.close();
byte[] payload = baos.toByteArray();
baos = null;
int totalSize = payload.length;
int bytesTransferred = 0;
int chunkSize = 2000;
while (bytesTransferred < totalSize) {
int nextChunkSize = totalSize - bytesTransferred;
if (nextChunkSize > chunkSize) {
nextChunkSize = chunkSize;
}
os.write(payload, bytesTransferred, nextChunkSize);
bytesTransferred += nextChunkSize;
//Progress update-->
if (progressListener != null) {
progressListener.onProgressUpdate((100 * bytesTransferred/totalSize));
}
//<--
}
os.flush();
//<--
//Get server response-->
int status = conn.getResponseCode();
if (conn.getResponseCode() == HttpsURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
JsonObject request = (JsonObject) gparser.parse(in.readLine());
if (!request.get("error").getAsBoolean()) {
finishedListener.onLoadFinished(new Object());
}
} else {
throw new IOException("Server returned non-OK status: " + status);
}
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
finishedListener.onNotConnected();
}
}
}).start();
}
}
큰 감사를 보냅니다. – whizzzkey
반갑습니다. 또한 서비스가 물건을 다운로드하거나 장치가 절전 모드로 들어갈 수있을 때 전원 잠금 장치를 사용해야합니다. – Gusman