我想将.mp3文件(仅)从设备上传到我的服务器.
我想浏览媒体数据的路径并选择任何mp3文件并上传.
我怎样才能做到这一点?
解决方法:
我的最终工作JAVA和PHP代码,用于将Android的SD卡上传文件到我自己的Web服务器.
Java / AndroID代码:
private voID dofileUpload() { httpURLConnection conn = null; DataOutputStream dos = null; DatainputStream inStream = null; String existingfilename = Environment.getExternalStorageDirectory().getabsolutePath() + "/mypic.png"; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; String responseFromServer = ""; String urlString = "http://mywebsite.com/directory/upload.PHP"; try { //------------------ CLIENT REQUEST fileinputStream fileinputStream = new fileinputStream(new file(existingfilename)); // open a URL connection to the Servlet URL url = new URL(urlString); // Open a http connection to the URL conn = (httpURLConnection) url.openConnection(); // Allow inputs conn.setDoinput(true); // Allow Outputs conn.setDoOutput(true); // Don't use a cached copy. conn.setUseCaches(false); // Use a post method. conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); dos = new DataOutputStream(conn.getoutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-disposition: form-data; name=\"uploadedfile\";filename=\"" + existingfilename + "\"" + lineEnd); dos.writeBytes(lineEnd); // create a buffer of maximum size bytesAvailable = fileinputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileinputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileinputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileinputStream.read(buffer, 0, bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // close streams Log.e("DeBUG", "file is written"); fileinputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { Log.e("DeBUG", "error: " + ex.getMessage(), ex); } catch (IOException ioe) { Log.e("DeBUG", "error: " + ioe.getMessage(), ioe); } //------------------ read the SERVER RESPONSE try { inStream = new DatainputStream(conn.getinputStream()); String str; while ((str = inStream.readline()) != null) { Log.e("DeBUG", "Server Response " + str); } inStream.close(); } catch (IOException ioex) { Log.e("DeBUG", "error: " + ioex.getMessage(), ioex); }}
要在服务器上运行的相关PHP代码(upload.PHP):
<?PHP// Where the file is going to be placed $target_path = "uploads/";/* Add the original filename to our target path. Result is "uploads/filename.extension" */$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"; chmod ("uploads/".basename( $_fileS['uploadedfile']['name']), 0644);} else{ echo "There was an error uploading the file, please try again!"; echo "filename: " . basename( $_fileS['uploadedfile']['name']); echo "target_path: " .$target_path;}?>
注意事项.
1)我在SD卡的根目录中有“mypic.png”.如果您通过Mass Storage USB视图查看AndroID设备,则会将该文件放在您遇到的第一个目录中.
2)必须在手机上关闭USB大容量存储!或者只是从正在编写代码的计算机上完全拔掉它,以确保是这种情况.
3)我必须在与我的PHP文件相同的目录中创建一个“uploads”文件夹.
4)您显然必须将我写为http://mywebsite.com/directory/upload.php的网址更改为您自己的网站.
总结以上是内存溢出为你收集整理的Android:如何将.mp3文件上传到http服务器?全部内容,希望文章能够帮你解决Android:如何将.mp3文件上传到http服务器?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)