栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从Android设备在php服务器中上传文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从Android设备在php服务器中上传文件

我不知道您的代码,但是为您提供了两个有效的代码:这适用于所有类型的文件。我将其用于图像,音频和视频文件。

Android:

new UploadFileAsync().execute("");  private class UploadFileAsync extends AsyncTask<String, Void, String> {    @Override    protected String doInBackground(String... params) {        try {  String sourceFileUri = "/mnt/sdcard/abc.png"; 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()) {     try {         String upLoadServerUri = "http://website.com/abc.php?";         // 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("bill", sourceFileUri);         dos = new DataOutputStream(conn.getOutputStream());         dos.writeBytes(twoHyphens + boundary + lineEnd);         dos.writeBytes("Content-Disposition: form-data; name="bill";filename=""      + sourceFileUri + """ + 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);         // Responses from the server (pre and message)         serverResponseCode = conn.getResponseCode();         String serverResponseMessage = conn      .getResponseMessage();         if (serverResponseCode == 200) {  // messageText.setText(msg);  //Toast.makeText(ctx, "File Upload Complete.",  //      Toast.LENGTH_SHORT).show();  // recursiveDelete(mDirectory1);         }         // close the streams //         fileInputStream.close();         dos.flush();         dos.close();     } catch (Exception e) {         // dialog.dismiss();         e.printStackTrace();     }     // dialog.dismiss(); } // End else block        } catch (Exception ex) { // dialog.dismiss(); ex.printStackTrace();        }        return "Executed";    }    @Override    protected void onPostExecute(String result) {    }    @Override    protected void onPreExecute() {    }    @Override    protected void onProgressUpdate(Void... values) {    }}

PHP ::

 <?php     if (is_uploaded_file($_FILES['bill']['tmp_name'])) {    $uploads_dir = './';      $tmp_name = $_FILES['bill']['tmp_name'];      $pic_name = $_FILES['bill']['name'];      move_uploaded_file($tmp_name, $uploads_dir.$pic_name);      }    else{        echo "File not uploaded successfully.";}   ?>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/394140.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号