사진을 찍었을떼,
byte[]값을 받아서 php서버로 전송하는 부분을 구현해보고 있는데요;
test.jpg파일이 생성은 되는데, 크기가 0이네요;;
어디가 잘못된걸까요?;;ㅠㅜㅜ
public void doFileUpload(byte[] data) {
try {
URL url = new URL("http://oculus.dothome.co.kr/imgserv.php");
Log.i(TAG, "http://oculus.dothome.co.kr/imgserv.php" );
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
// open connection
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true); //input 허용
con.setDoOutput(true); // output 허용
con.setUseCaches(false); // cache copy를 허용하지 않는다.
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// write data
DataOutputStream dos =
new DataOutputStream(con.getOutputStream());
Log.i(TAG, "Open OutputStream" );
dos.writeBytes(twoHyphens + boundary + lineEnd);
// 파일 전송시 파라메터명은 file1 파일명은 camera.jpg로 설정하여 전송
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + FileName+"\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.write(data,0,data.length);
Log.i(TAG, data.length+"bytes written" );
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.flush(); // finish upload...
} catch (Exception e) {
Log.i(TAG, "exception " + e.getMessage());
// TODO: handle exception
}
Log.i(TAG, data.length+"bytes written successed ... finish!!" );
}
그리고 php 서버를 닷홈에서 호스팅을 무료로 받아서 이용하고 있는데요
처음엔 초기에 있는 폴더나 파일들을 냅두고 걍 사용을 했엇는데, 파일업로드가 잘 안되서
기존에 있는 폴더나 파일들을 다 지운 상태로 imgserv.php만 남겨놨더니
아에 접근을 못하게 되버렸네요 ㅠㅠ
<?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!";
}
?>
서버에서는 이렇게 받아줍니다.