Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓文件上传_Android_Http_File Upload - Fatal编程技术网

Android 安卓文件上传

Android 安卓文件上传,android,http,file-upload,Android,Http,File Upload,我想使用http将文件上载到服务器 try { URL url = new URL(dst); File file = new File(src); urlconnection = url.openConnection(); urlconnection.setDoOutput(true); urlconnection.setDoInput(true); if

我想使用http将文件上载到服务器

     try {
         URL url = new URL(dst);
          File file = new File(src);
           urlconnection = url.openConnection();
           urlconnection.setDoOutput(true);
           urlconnection.setDoInput(true);

           if (urlconnection instanceof HttpURLConnection) {
            try {
             ((HttpURLConnection)urlconnection).setRequestMethod("PUT");
             ((HttpURLConnection)urlconnection).setRequestProperty("Content-type", "text/html");
             ((HttpURLConnection)urlconnection).connect();


            } catch (ProtocolException e) {

             e.printStackTrace();
            }
           }


           BufferedOutputStream bos = new BufferedOutputStream(urlconnection
             .getOutputStream());
           BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
             file));
            int i;
            // read byte by byte until end of stream
            while ((i = bis.read()) >0) {
             bos.write(i);
            }
           System.out.println(((HttpURLConnection)urlconnection).getResponseMessage());

          }
          catch(Exception e1)
          {
           e1.printStackTrace();
          }
          try {

           InputStream inputStream;
           int responseCode=((HttpURLConnection)urlconnection).getResponseCode();
           if ((responseCode>= 200) &&(responseCode<=202) ) {
            inputStream = ((HttpURLConnection)urlconnection).getInputStream();
            int j;
            while ((j = inputStream.read()) >0) {
             System.out.println(j);
            }

           } else {
            inputStream = ((HttpURLConnection)urlconnection).getErrorStream();
           }
           ((HttpURLConnection)urlconnection).disconnect();

          } catch (IOException e) {

           e.printStackTrace();
          }
试试看{
URL=新URL(dst);
文件=新文件(src);
urlconnection=url.openConnection();
urlconnection.setDoOutput(true);
urlconnection.setDoInput(true);
if(HttpURLConnection的urlconnection实例){
试一试{
((HttpURLConnection)urlconnection.setRequestMethod(“PUT”);
(HttpURLConnection)urlconnection.setRequestProperty(“内容类型”、“文本/html”);
((HttpURLConnection)urlconnection.connect();
}捕获(协议例外e){
e、 printStackTrace();
}
}
BufferedOutputStream bos=新的BufferedOutputStream(urlconnection
.getOutputStream());
BufferedInputStream bis=新BufferedInputStream(新文件输入流(
档案);;
int i;
//逐字节读取,直到流结束
而((i=bis.read())>0){
bos.write(i);
}
System.out.println(((HttpURLConnection)urlconnection.getResponseMessage());
}
捕获(异常e1)
{
e1.printStackTrace();
}
试一试{
输入流输入流;
int responseCode=((HttpURLConnection)urlconnection.getResponseCode();
如果((响应代码>=200)和&(响应代码0){
系统输出println(j);
}
}否则{
inputStream=((HttpURLConnection)urlconnection.getErrorStream();
}
((HttpURLConnection)urlconnection.disconnect();
}捕获(IOE异常){
e、 printStackTrace();
}
在System.out.println(((HttpURLConnection)urlconnection.getResponseMessage())上,行写“未实现”,我找不到为什么将文件上载到php服务器

public void上传文件(字符串sourceFileUri){

将文件上载到php服务器

public void上传文件(字符串sourceFileUri){


您确定您的服务器侦听PUT请求吗?可能尝试POST。当我尝试POST时,是否写入“不允许方法”您确定您的服务器侦听PUT请求吗?可能尝试POST。当我尝试POST时,是否写入“不允许方法”是否编写此代码也适用于.net webservice?如果是,请发布webmethod结构。我得到的响应代码为200,但在服务器上没有得到任何信息。此代码也适用于.net webservice吗?如果是,请发布webmethod结构。我得到的响应代码为200,但在服务器上没有得到任何信息。
    Log.e("upload Started", "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
    String fileName = sourceFileUri;

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new File(sourceFileUri);

    if (!sourceFile.isFile()) {
        Log.e("uploadFile", "Source File not exist :" + sdcard + ""
                + "log.txt");

    } 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", device_id + ".txt");

            dos = new DataOutputStream(conn.getOutputStream());

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                    + device_id + ".txt" + "\"" + 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 (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() {

                        Toast.makeText(STB.this, "File Upload Complete.",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }
            DataInputStream inStream = new DataInputStream(
                    conn.getInputStream());
            String str;

            while ((str = inStream.readLine()) != null) {
                Log.e("Debug", "Server Response " + str);
            }
            inStream.close();
            // close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();
        } catch (MalformedURLException ex) {

            dialog.dismiss();
            ex.printStackTrace();



            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
        } catch (Exception e) {

            e.printStackTrace();



        }

    } // End else block

}