微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用android上传文件到服务器

我一直在android上的应用程序,应该从SD卡抓取图像,然后将其上传到我的服务器。 我能够得到图片上传到服务器使用这个例子给了“ http://www.androidexample.com/media/UploadToServer.PHP uri但是图像不会上传到我使用本地主机服务器本地主机从一个wifi从一个Android设备使用URI 192.1xx.x.xxx/uploadtoserver.PHP我收到一个serverresponse 200并确定。

我已经search了答案和研究了3天,不能得到这个工作。

我的代码

Java部分:

dlopen()不能使用android-n

如何确定和设置android.process.media组?

三星galaxy S3的Linux开发/部署

android使用存储在资产文件夹中的Linux工具

使用根Android应用程序运行二进制文件

public class UploadToServer extends Activity { TextView messageText; Button uploadButton; int serverResponseCode = 0; ProgressDialog dialog = null; String upLoadServerUri = null; /********** File Path *************/ String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); final String uploadFilePath = path + "/MyApp/Temp/"; final String uploadFileName = "temp.png"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_upload_to_server); uploadButton = (Button)findViewById(R.id.uploadButton); messageText = (TextView)findViewById(R.id.messageText); messageText.setText("Uploading file path :- '/mnt/sdcard/"+uploadFileName+"'"); /************* PHP script path ****************/ upLoadServerUri = "http://192.168.1.144/UploadToServer.PHP"; uploadButton.setonClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog = ProgressDialog.show(UploadToServer.this,"","Uploading file...",true); new Thread(new Runnable() { public void run() { runOnUiThread(new Runnable() { public void run() { messageText.setText("uploading started....."); } }); uploadFile(uploadFilePath + "" + uploadFileName); } }).start(); } }); } public int uploadFile(String sourceFileUri) { String fileName = sourceFileUri; HttpURLConnection conn = null; DataOutputStream dos = null; String lineEnd = "rn"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead,bytesAvailable,bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; File sourceFile = new File(sourceFileUri); if (!sourceFile.isFile()) { dialog.dismiss(); Log.e("uploadFile","Source File not exist :" +uploadFilePath + "" + uploadFileName); runOnUiThread(new Runnable() { public void run() { messageText.setText("Source File not exist :" +uploadFilePath + "" + uploadFileName); } }); return 0; } else { try { // open a URL connection to the Servlet FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(upLoadServerUri); // Open a HTTP connection to the URL conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection","Keep-Alive"); conn.setRequestProperty("ENCTYPE","multipart/form-data"); conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary); conn.setRequestProperty("uploaded_file",fileName); dos = new DataOutputStream(conn.getoutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-disposition: form-data; name="uploaded_file";filename="" + fileName + """ + 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,bufferSize); while (bytesRead > 0) { dos.write(buffer,bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable,maxBufferSize); bytesRead = fileInputStream.read(buffer,bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn.getResponseMessage(); Log.i("uploadFile","HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode); if(serverResponseCode == 200){ runOnUiThread(new Runnable() { public void run() { String msg = "File Upload Completed.nn See uploaded file here : nn" +" http://192.168.1.144/UploadToServer.PHP/" +uploadFileName; messageText.setText(msg); Toast.makeText(UploadToServer.this,"File Upload Complete.",Toast.LENGTH_SHORT).show(); } }); } //close the streams // fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { dialog.dismiss(); ex.printstacktrace(); runOnUiThread(new Runnable() { public void run() { messageText.setText("MalformedURLException Exception : check script url."); Toast.makeText(UploadToServer.this,"MalformedURLException",Toast.LENGTH_SHORT).show(); } }); Log.e("Upload file to server","error: " + ex.getMessage(),ex); } catch (Exception e) { dialog.dismiss(); e.printstacktrace(); runOnUiThread(new Runnable() { public void run() { messageText.setText("Got Exception : see logcat "); Toast.makeText(UploadToServer.this,"Got Exception : see logcat ",Toast.LENGTH_SHORT).show(); } }); Log.e("Upload file to server Exception","Exception : " + e.getMessage(),e); } dialog.dismiss(); return serverResponseCode; } // End else block }

}

由于它在一台服务器上工作,我认为这是一个服务器端问题

PHP的一部分

<?PHP $target_path1 = "var/www/images/" $target_path1 = $target_path1 . basename( $_FILES['uploaded_file']['name']); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$target_path1)) { echo "Success"; } else{ echo "fail";} ?>

电弧焊机应用程序在启动时显示空白页面

为Android构build的共享库(.so文件可以包含/链接并在Android应用程序中使用吗?

没有这样的文件或目录“,而试图获得亚行版本

命令行Ant未被识别

Eclipse ADT意外的exception“无法运行程序”

<?PHP $target_path1 = "/var/www/images/" $target_path1 = $target_path1 . basename( $_FILES['uploaded_file']['name']); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$target_path1)) { echo "Success"; } else { echo "fail"; } ?>

看起来你正在引用一个相对目录: var/www/images/ which,如果你的PHP文件在/var/www/开始的时候会指向/var/www/var/www/images/是你的意图。

只需更改$target_path1 ,使其成为绝对路径/var/www/images/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐