2011-05-10 3 views
0

안녕하세요 안드로이드에있는 sdcard에서 php server로 이미지 파일을 업로드하려고합니다. 업로드 할 때 다음 코드를 사용합니다. 예외는 표시되지 않지만 이미지는 서버에 업로드되지 않습니다. 나는 그 문제가 뭔지 모른다. 제발 도와주세요. 내 코드와 Logcat 정보를 첨부했습니다. 코드 :안드로이드에서 PHP 서버에 이미지를 업로드 할 때 문제가 발생했습니다.

public class UploadImage extends Activity { 
InputStream is; 
private int serverResponseCode; 
private String serverResponseMessage; 
@Override 
public void onCreate(Bundle icicle) { 
super.onCreate(icicle); 
setContentView(R.layout.main); 
HttpURLConnection connection = null; 
DataOutputStream outputStream = null; 
DataInputStream inputStream = null; 
String pathToOurFile = "//sdcard//chsevtoneta.png"; 
String urlServer = "http://xxxxxxxxxxxxxxxxxxxxxxxx/upload.php"; 
String lineEnd = "\r\n"; 
String twoHyphens = "--"; 
String boundary = "*****"; 

int bytesRead, bytesAvailable, bufferSize; 
byte[] buffer; 
int maxBufferSize = 1*1024*1024; 

try 
{ 
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); 

URL url = new URL(urlServer); 
connection = (HttpURLConnection) url.openConnection(); 

// Allow Inputs & Outputs 
connection.setDoInput(true); 
connection.setDoOutput(true); 
connection.setUseCaches(false); 

// Enable POST method 
connection.setRequestMethod("POST"); 

connection.setRequestProperty("Connection", "Keep-Alive"); 
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

outputStream = new DataOutputStream(connection.getOutputStream()); 
outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); 
outputStream.writeBytes(lineEnd); 

bytesAvailable = fileInputStream.available(); 
bufferSize = Math.min(bytesAvailable, maxBufferSize); 
buffer = new byte[bufferSize]; 

// Read file 
bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

while (bytesRead > 0) 
{ 
outputStream.write(buffer, 0, bufferSize); 
bytesAvailable = fileInputStream.available(); 
bufferSize = Math.min(bytesAvailable, maxBufferSize); 
bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
} 

outputStream.writeBytes(lineEnd); 
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

// Responses from the server (code and message) 

serverResponseCode = connection.getResponseCode(); 
serverResponseMessage = connection.getResponseMessage(); 
Log.e("response",""+serverResponseCode); 
Log.e("serverResponseMessage",""+serverResponseMessage); 

fileInputStream.close(); 
outputStream.flush(); 
outputStream.close(); 
} 
catch (Exception ex) 
{ 
//Exception handling 
} 
} 
} 

및 로그 캣 정보 : 성공 (200) 메시지

05-10 11:04:59.762: ERROR/response(2203): 200 
05-10 11:04:59.762: ERROR/serverResponseMessage(2203): OK 
+1

서버가 성공 메시지를 반환합니다. PHP 코드는 어떻게 생깁니 까? – Femi

+0

감사하지만 서버 코딩을 사용할 수 없습니다. 하지만 다른 서버는 올바른 응답 [서버 반환 URL]을 반환합니다. 응답을 얻는 방법은 무엇입니까? –

+0

그래, 다음 디버그하기 힘들 것입니다 : 다른 사람의 작업 코드가 있습니까? – Femi

답변

1

소스를 보면 손으로 멀티 파트/폼 업로드를하고있는 것처럼 보입니다.이 경우 쉽게 오류가 발생하고 줄 바꿈을 추가하거나 잘못된 위치에 여분의 공간을 추가하면 눈으로 볼 수 없게됩니다 그러나 사용중인 HTTP 파서의 엄격함에 따라 서버를 쉽게 혼동 할 수 있습니다.

예를 들어 http://indiwiz.com/2009/02/11/multi-part-content-upload-in-apache-http-components-http-client/에 Android의 기본 제공 HTTP 구성 요소 조각을 사용하는 것이 좋습니다.

2

원격 서버의 응답. 나는이 오류가 서버 측에있을 것이라고 생각하지만이 부분에 대한 코드를 제공하지는 않는다.

+0

감사하지만 서버 코딩을 사용할 수 없습니다. 하지만 다른 서버는 올바른 응답 [서버 반환 URL]을 반환합니다. 응답을 얻는 방법은 무엇입니까? –

관련 문제