[問題] J2ME 傳參數問題
功能是要把圖片原本的檔名上傳到server端
server端是用php去接收
*client端
//用http去連線到php網頁 用POST方式
conn=(HttpConnection)Connector.open("http://xx.xx.xx.xx/post.php");
conn.setRequestMethod( HttpConnection.POST );
//FILE是目前選擇的檔案名稱 ex:1.jpg
FILE=fileList.getString(fileList.getSelectedIndex());
FILE = FILE.trim(); //去除多餘空白或是奇怪的東西
String message1 = ""; //宣告字串message1
//CrLf="\r\n"
message1= "-----------------------------4664151417711" + CrLf;
//FILE = "1.jpg"
message1 += "Content-Disposition: form-data; name=\"bytes\";
filename=\"" + FILE + "\"" + CrLf;
message1 += "Content-Type: image/jpeg" + CrLf; //
message1 += CrLf;
//設定傳送的type為multipart/form-data
conn.setRequestProperty("Content-Type", "multipart/form-data;
boundary=---------------------------4664151417711");
os = conn.openOutputStream(); //打開輸出流
os.write(message1.getBytes()); //把message1傳到server
**server端php的寫法
echo $_FILES['bytes']['name']; //印出原始檔名
$target_path = "./photo/"; //圖片放到photo資料夾的路徑
$today = date("Ymd"); //設定年月日
$todaytime=date('His'); //設定時分秒
//把target_path是最後完整的圖片路徑 ex: ./photo/20080801_152321.JPG
$target_path= $target_path.$today."_".$todaytime.".JPG";
if(is_uploaded_file($_FILES['bytes']['tmp_name']))
{
echo "OK";
//圖片上傳成功 把圖片複製到target_path路徑
copy($_FILES['bytes']['tmp_name'], $target_path);
}
else
{ //如果檔案傳送失敗
echo "Failed";
}
產生的問題:
1.用wtk模擬器跑 有印出原始檔名 圖片也有上傳成功
2.用sony k800i測試 沒有印出原始檔名 但是有上傳成功
3. message1 += "Content-Disposition: form-data; name=\"bytes\";
filename=\"" + FILE + "\"" + CrLf;
如果FILE換成指定的字串"1.jpg" 用手機測試server端就會印出原始檔名
嘗試好多次都找不到原因
想請大家幫忙看一下
感謝大家耐心看完問題 ^^
※ 編輯: fishryo 來自: 140.123.15.73 (08/03 16:12)