私はphpにローカルホストを使用しています。
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.util.Log;
public class ImageUpload {
HttpURLConnection _httpurlconnection;
DataOutputStream _dos;
DataInputStream _dis;
String _serverurl="http://192.168.1.11/project/imageupload.php";
String _lineend= "\r\n";
String _twohyphens= "--";
String _boundry="*****";
int _bytesread, _bytesavailable, _buffersize;
byte[] _buffer;
int _maxbuffersize = 1 * 1024 * 1024;
public void uploadImage(String _filepath) {
try {
FileInputStream _fis = new FileInputStream(new File(_filepath));
URL _url = new URL(_serverurl);
_httpurlconnection = (HttpURLConnection) _url.openConnection();
_httpurlconnection.setDoInput(true);
_httpurlconnection.setDoOutput(true);
_httpurlconnection.setUseCaches(false);
_httpurlconnection.setRequestMethod("POST");
_httpurlconnection.setRequestProperty("Connection", "Keep-Alive");
_httpurlconnection.setRequestProperty("Connection-Type",
"multipart/form-data;boundry=" + _boundry);
_dos = new DataOutputStream(_httpurlconnection.getOutputStream());
_dos.writeBytes(_twohyphens + _boundry + _lineend);
_dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ _filepath + "\"" + _lineend);
_dos.writeBytes(_lineend);
_bytesavailable = _fis.available();
_buffersize = Math.min(_bytesavailable, _maxbuffersize);
_buffer = new byte[_buffersize];
_bytesread = _fis.read(_buffer, 0, _buffersize);
while (_bytesread > 0) {
_dos.write(_buffer, 0, _buffersize);
_bytesavailable = _fis.available();
_buffersize = Math.min(_bytesavailable, _maxbuffersize);
_bytesread = _fis.read(_buffer, 0, _buffersize);
}
_dos.writeBytes(_lineend);
_dos.writeBytes(_twohyphens + _boundry + _twohyphens + _lineend);
int responsecode = _httpurlconnection.getResponseCode();
String responsemessage = _httpurlconnection.getResponseMessage();
_fis.close();
_dos.flush();
_dos.close();
BufferedReader br=new BufferedReader(new InputStreamReader(_httpurlconnection.getInputStream()));
String line;
while((line=br.readLine())!=null)
{
Log.i("----------------",">>"+line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
これが私のphpコードです。
<?php
$target_path = "/var/www/project";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
しかし、私は通知を受け取っています:未定義のインデックス:5行目のuploadedfile誰かがこの問題を解決するのを手伝ってくれますか?