美好的一天!我正在尝试搜索从Android上传图像文件到在线MysqL数据库的基本教程,但我找不到任何.
我现在正在开展一项活动,可以将用户的个人资料图片从AndroID上传到在线服务器.
我需要的是显示一个按钮,当它被点击时,用户可以从文件中选择一个图像.有人可以指导我这样做吗?提前致谢!
解决方法:
在客户端,您可以这样做.
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 }
服务器端
<?PHP $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"; }?>
总结 以上是内存溢出为你收集整理的php – 将图像从Android上传到MySQL数据库全部内容,希望文章能够帮你解决php – 将图像从Android上传到MySQL数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)