<?php
try{
$title = "";
$img = "";
//앱에서 보내는 데이터를 post 방식으로 취득
$title = $_POST["title"];
$img = $_POST["img"];
//현재 서버 시간을 이용
$date = date("YmdHis");
//기본 서버 접속 정보 취득
require("../db/db.inc");
//취득한 서버 정보로 서버 접속
$connect = dbConnect();
// 데이터 입력 쿼리
$sql = "
INSERT INTO `bokgoon`.`T_Brd`
(`brd_title` ,`brd_img` )
VALUES ( '$title', '$img');";
//쿼리 실행 및 실패시 처리
$raw = mysql_query($sql) or die("Query error : ".iconv("euc-kr","utf-8",mysql_error()));
//쿼리 결과 화면 표시
echo "<?xml version='1.0' encoding='utf-8'?>\n";
echo "<root>";
echo "<item>";
if($raw){
//쿼리 처리 결과가 성공일 경우
echo "<Result><![CDATA[0]]></Result>";
echo "<ErrMsg><![CDATA['success']]></ErrMsg>";
}else{
//쿼리 처리 결과가 실패일 경우
echo "<Result><![CDATA[2001]]></Result>";
echo "<ErrMsg><![CDATA['query error']]></ErrMsg>";
}
echo "</item>";
echo "</root>";
}catch(Exception $e){
//쿼리 실행 중 실패시 메시지 처리
echo "<?xml version='1.0' encoding='utf-8'?>\n";
echo "<root>";
echo "<item>";
echo "<Result><![CDATA[2000]]></Result>";
echo "<ErrMsg><![CDATA['".iconv("euc-kr","utf-8",mysql_error())."']]></ErrMsg>";
echo "</item>";
echo "</root>";
}
?>
밑에는 코드 입니다
public class Main extends Activity implements OnClickListener {
private FileInputStream mFileInputStream=null;
private URL connectUrl=null;
Button get_image_btn;
Button transmit_Image_btn;
String lineEnd="\r\n";
String twoHyphens="--";
String boundary="*****";
String myld;
String myResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(android.os.Build.VERSION.SDK_INT>9){
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
initial();
}
public void initial(){
get_image_btn=(Button)findViewById(R.id.get_image_btn);
get_image_btn.setOnClickListener(this);
transmit_Image_btn=(Button)findViewById(R.id.transmit_image_btn);
transmit_Image_btn.setOnClickListener(this);
/*myld=((EditText)(findViewById(R.id.editText1))).getText().toString();*/
}@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.get_image_btn:
Intent intent=new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/+");
startActivityForResult(intent,0);
break;
case R.id.transmit_image_btn:
System.out.println("ab");
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
Uri selPhotoUri=intent.getData();
try {
Bitmap selPhoto=Images.Media.getBitmap(getContentResolver(),selPhotoUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
String urlString="http://bokgoon.zc.bz/greenalmond/insert_post";
Cursor c=getContentResolver().query(Uri.parse(selPhotoUri.toString()),null,null,null,null);
c.moveToNext();
String absolutePath=c.getString(c.getColumnIndex(MediaColumns.DATA));
System.out.println("절대경로"+absolutePath);
DoFileUpload(urlString,absolutePath);
String path=intent.getDataString();
System.out.println(path);
Toast.makeText(this,path,Toast.LENGTH_LONG).show();
}
public void DoFileUpload(String apiUrl, String absolutePath) {
// TODO Auto-generated method stub
HttpFileUpload(apiUrl,"",absolutePath);
}
public void HttpFileUpload(String urlString,String params, String fileName) {
// TODO Auto-generated method stub
try{
mFileInputStream=new FileInputStream(fileName);
connectUrl=new URL(urlString);
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);
DataOutputStream dos=new DataOutputStream(conn.getOutputStream());
for(int i=0;i<1;i++){
dos.writeBytes(twoHyphens+boundary+lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"title"+lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes("abcde");
dos.writeBytes(lineEnd);
}
dos.writeBytes(twoHyphens+boundary+lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"image"+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);
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);
mFileInputStream.close();
dos.flush();
int ch;
InputStream is=conn.getInputStream();
StringBuffer b=new StringBuffer();
while((ch=is.read())!=-1){
b.append((char)ch);
}
String s=b.toString();
dos.close();
}catch(Exception e){
System.out.println(e);
System.out.println(e.getMessage());
}
}
}
글과 이미지를 php 이용하여 서버로 저장하는 부분 입니다이렇게 구성이 되어 있는데
문제점이 있습니다
1. 갤러리로 이동이 안되서 사진 선택 이 안됩니다
2.php 파일 을 보면 title 에는 글이 저장 되고 img 에는 갤러리에서 선택된 사진이 저장되어야합니다
코드가 맞나요?