package com.inculab;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class UploadTest extends Activity {

private Button mUploadBtn;
private FileInputStream mFileInputStream = null;
private URL connectUrl = null;
private EditText mEdityEntry; 
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.upload_test);
mEdityEntry = (EditText)findViewById(R.id.edit_entry);
mUploadBtn = (Button)findViewById(R.id.btn);
mUploadBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mEdityEntry.setText("Uploading...");
String mFilePath = "/storage/sdcard0/Cymera/CyMERA_20131011_142142.jpg";
DoFileUpload(mFilePath);
} catch (Exception e) {
// TODO: handle exception
}
}
});
}
private void DoFileUpload(String filePath) throws IOException {
Log.d("Test" , "file path = " + filePath);
HttpFileUpload("http://211.253.236.175/upload.php", "", filePath);
}
private void HttpFileUpload(String urlString, String params, String fileName) {
try {
mFileInputStream = new FileInputStream(fileName);
connectUrl = new URL("urlString);
Log.d("Test", "mFileInputStream  is " + mFileInputStream);
// open connection 
HttpURLConnection conn = (HttpURLConnection)connectUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// write data
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fileName+"\"" + lineEnd);
dos.writeBytes(lineEnd);
int bytesAvailable = mFileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = mFileInputStream.read(buffer, 0, bufferSize);
Log.d("Test", "image byte is " + bytesRead);
// read image
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = mFileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = mFileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e("Test" , "File is written");
mFileInputStream.close();
dos.flush(); // finish upload...
// get response
int ch;
InputStream is = conn.getInputStream();
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ){
b.append( (char)ch );
}
String s=b.toString(); 
Log.e("Test", "result = " + s);
mEdityEntry.setText(s);
dos.close();
} catch (Exception e) {
Log.d("Test", "exception " + e.getMessage());
// TODO: handle exception
}
}
}




<?php
$target_path = "files/";

$tmp_img = explode("." ,$_FILES['uploadedfile']['name']); 
$img_name = $tmp_img[0]."_".date('Y-m-d_H_i_s').".".$tmp_img[1];
$target_path = $target_path . basename($img_name);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ".$img_name." has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}

?>

안녕하세요 안드로이드 공부하는 학생입니다.

안드로이드->웹서버 예제소스를 얻어서

안드로이드 스마트폰에 소스 실행 후 

우분투 서버(id, pwd는 있지만 사용권한은 모두 열어둠)에

이미지 파일을 올리려고 합니다.


에러없이 웹서버에 올라가지 않는 현상이 나타납니다.

1. php파일은 우분투 서버 /var/www/ 경로에 저장되어있는데, 혹 php파일 경로문제인가요?

2. 우분투 서버 접근 권한은 777로 모두 열어놔도 안드로이드 소스상에 id,pwd정보가 있어야 파일 업로드가 가능한가요?

3. 1번2번 문제가 아니라면 어디서 해결책을 찾아야 할까요ㅠ