php安卓传输图片到mysql_php – 将图像从Android上传到MySQL数据库

在客户端,您可以这样做.

HttpURLConnection connection = null;

DataOutputStream outputStream = null;

DataInputStream inputStream = null;

String pathToOurFile = "path of the image.jpeg";

String urlServer = "http://xxx.xxx.xxx.xxx/uploader.php";

String lineEnd = "\r\n";

String twoHyphens = "--";

String boundary = "*****";

int bytesRead, bytesAvailable, bufferSize;

byte[] buffer;

int maxBufferSize = 1*1024*1024;

try {

FileInputStream fileInStream = new FileInputStream(new File(pathToOurFile) );

URL url = new URL(urlServer);

connection = (HttpURLConnection) url.openConnection();

// Allow Inputs & Outputs

connection.setDoInput(true);

connection.setDoOutput(true);

connection.setUseCaches(false);

// Enable POST method

connection.setRequestMethod("POST");

connection.setRequestProperty("Connection", "Keep-Alive");

connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

outputStream = new DataOutputStream( connection.getOutputStream() );

outputStream.writeBytes(twoHyphens + boundary + lineEnd);

outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);

outputStream.writeBytes(lineEnd);

bytesAvailable = fileInStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);

buffer = new byte[bufferSize];

// Read file

bytesRead = fileInStream.read(buffer, 0, bufferSize);

while (bytesRead > 0)

{

outputStream.write(buffer, 0, bufferSize);

bytesAvailable = fileInStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);

bytesRead = fileInStream.read(buffer, 0, bufferSize);

}

outputStream.writeBytes(lineEnd);

outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)

serverResponseCode = connection.getResponseCode();

serverResponseMessage = connection.getResponseMessage();

fileInputStream.close();

outputStream.flush();

outputStream.close();

}

catch (Exception ex)

{

//Exception handling

}

服务器端

$target_path = "./";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

echo "Success";

} else{

echo "Error";

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值