0
A
답변
1
기본적으로 양면이 필요합니다. 첫 번째는 Android에 있습니다. 데이터를 웹 서비스에 보내야합니다 (ASyncTask e.x에서 수행). 이제, 우리는 다른면을 가지고있다
private boolean handleFile(String filePath, String mimeType) {
HttpURLConnection connection = null;
DataOutputStream outStream = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String urlString = "http://your.domain.com/webservice.php";
try {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(new File(filePath));
} catch(FileNotFoundException e) { }
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outStream = new DataOutputStream(connection.getOutputStream());
// Some POST values
outStream.writeBytes(addParam("additional_param", "some value");
outStream.writeBytes(addParam("additional_param 2", "some other value");
// The file with the name "uploadedfile"
outStream.writeBytes(twoHyphens + boundary + lineEnd);
outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + filePath +"\"" + lineEnd + "Content-Type: " + mimeType + lineEnd + "Content-Transfer-Encoding: binary" + lineEnd);
outStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outStream.writeBytes(lineEnd);
outStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
outStream.flush();
outStream.close();
} catch (MalformedURLException e) {
Log.e("APP", "MalformedURLException while sending\n" + e.getMessage());
} catch (IOException e) {
Log.e("APP", "IOException while sending\n" + e.getMessage());
}
// This part checks the response of the server. If its "UPLOAD OK" the method returns true
try {
inStream = new DataInputStream(connection.getInputStream());
String str;
while ((str = inStream.readLine()) != null) {
if(str=="UPLOAD OK") {
return true;
} else {
return false;
}
}
inStream.close();
} catch (IOException e){
Log.e("APP", "IOException while sending\n" + e.getMessage());
}
return false;
}
: http://your.domain.com/webservice.php 서버 여기에는 URL에 파일과 몇 가지 추가 POST 값을 보내는 당신을위한 약간의 방법을했다. 보내진 POST 요청을 처리하려면 PHP와 같은 일부 논리가 필요합니다. 이 같은 뭔가 작동합니다 :
<?php
$target_path = "videos/";
$target_path = $target_path.'somename.3gp';
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
exit("UPLOAD OK");
} else {
exit("UPLOAD NOT OK");
}
?>
관련 문제
- 1. 백그라운드 서비스로 작업하는 작업
- 2. Asp.Net 3.5 웹 서비스로 라우팅 하시겠습니까?
- 3. 프로그램을 서비스로 실행 하시겠습니까?
- 4. XML을 웹 서비스로 보내기
- 5. DAO를 웹 서비스로 노출
- 6. 웹 서비스로 xtify
- 7. iis의 대역 외 Windows 서비스로 작업 실행
- 8. HttpPost를 사용하여 Android 앱에서 웹 서비스로 String []을 전달 하시겠습니까?
- 9. 원시 XML을 웹 서비스로 보내기
- 10. 데이터를 웹 서비스로 전송하기위한 아이디어
- 11. 기존 API를 웹 서비스로 노출
- 12. C++ 프로그램을 웹 서비스로 노출
- 13. 클래스 인스턴스를 웹 서비스로 구현
- 14. awk 스크립트를 웹 서비스로 사용
- 15. 웹 서비스로 대용량 파일 전송
- 16. iPhone에서 웹 서비스로 푸시와 폴링
- 17. 웹 응용 프로그램을 웹 서비스로 변환
- 18. exp를 얻는 간단한 API. 웹 서비스로
- 19. 큰 웹 프로젝트를 만들거나 일부 기능을 웹 서비스로 분할 했습니까?
- 20. 결제 처리 서비스로 nopCommerce를 사용 하시겠습니까?
- 21. Windows 응용 프로그램을 서비스로 실행 하시겠습니까?
- 22. C++ 프로그램을 Windows 서비스로 변환 하시겠습니까?
- 23. MongoDB 서버를 서비스로 실행 (터미널에서 분리) 하시겠습니까?
- 24. 다른 사용자로 cron 작업 하시겠습니까?
- 25. android에서 데이터베이스로 작업 하시겠습니까?
- 26. asp.net에서 Datalist로 작업 하시겠습니까?
- 27. JAXB가 WSDL 웹 서비스로 클래스를 생성했습니다.
- 28. 웹 서비스로 SharePoint에서 위키 페이지 만들기
- 29. SOAP 웹 서비스로 Spring 서비스 공개하기
- 30. 바이트 [] 크기가 웹 서비스로 전송되는 문제가 있습니까?
이봐 감사 톤! 그것은 나에게 차기 시작을 제공하는 것이 꽤 좋다. 나는 그걸로 일할 것이고 어떤 이슈라도 다시 만날 수 있다면 – NickHalden
고마워, 아무런 문제가 없다. ASyncTask가있는 것이 중요하다. 상상해보십시오. 업로드 할 5MBi 파일이 있고 그 화면에 euser가 꽂혀 있고 아무것도로드 아이콘이 없어도 아무 일도 일어나지 않는다고 상상해보십시오. –