Android将文件上传到服务器

卡鲁索

基于前一篇文章的建议,我尝试使用Android:使用php在服务器上上传图像,但是我发现文件未找到异常。

这是我在上面的帖子中描述的功能。我的输入是:

图库:uploadFile:Source File not exist :content://media/external/images/media/342照片:uploadFile:Source File not exist: file:///storage/emulated/0/MyDir/blah

这些uri源自意图倾斜/选择它们的意图。有什么想法让我File Not Found例外吗?

private void doFileUpload(String exsistingFileName){
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null; 

    //String exsistingFileName = "/sdcard/six.3gp";
    // Is this the place are you doing something wrong.

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;
    String urlString = "http://192.168.1.5/upload.php";
    try
    {
        Log.e("MediaPlayer","Inside second Method");
        FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
        URL url = new URL(urlString);
        conn = (HttpURLConnection) url.openConnection();
        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=\"" + exsistingFileName +"\"" + lineEnd);
        dos.writeBytes(lineEnd);
        Log.e("MediaPlayer","Headers are written");
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        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);
        }
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        String LogString = "";

        while ((inputLine = in.readLine()) != null)  {
            LogString= LogString + inputLine;
        }

        Log.i(Utils.TAG, LogString);
        // close streams
        fileInputStream.close();
        dos.flush();
        dos.close();
    }
    catch (MalformedURLException ex)
    {
        Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
    }
    catch (IOException ioe)
    {
        Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
    }

    //------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream ( conn.getInputStream() );
        String str;            
        while (( str = inStream.readLine()) != null)
        {
            Log.e("MediaPlayer","Server Response"+str);
        }
        /*while((str = inStream.readLine()) !=null ){

        }*/
        inStream.close();
    }
    catch (IOException ioex){
        Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
    }
}
常用软件

我收到文件找不到异常

那是因为这些都不是文件的路径。您可以通过查看它们来说明这一点。您也没有按照我之前的回答进行操作

更换:

FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );

与:

InputStream contentInputStream = getContentResolver().openInputStream(Uri.parse(exsistingFileName));

(并在其余方法fileInputStream中用替换出现contentInputStream

注意:

  • 假设您doFileUpload()是在继承自的某个类Context(例如anActivity或a)上实现的Service您将需要安排获得ContentResolverdoFileUpload()通过其他方式,如果doFileUpload()没有访问getContentResolver()

  • 您可以通过将Uri收到的传入doFileUpload()而不是先将其转换为aString然后再转换为a来简化事务Uri

  • 您将需要为Content-Disposition:标题创建自己的文件名,因为您不会从中获取文件名Uri

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从Android将MultiPart文件上传到服务器(SpringMVC)

来自分类Dev

Android将文件上传到服务器网络上

来自分类Dev

Android将任何文件上传到服务器

来自分类Dev

将文件从Android设备上传到远程服务器

来自分类Dev

Android将任何文件上传到服务器

来自分类Dev

将文件上传到服务器

来自分类Dev

将文件上传到服务器

来自分类Dev

从Android直接将文件直接上传到服务器而无需Web服务

来自分类Dev

通过发布将文件上传到服务器OutOfMemory

来自分类Dev

通过网站将文件上传到SFTP服务器

来自分类Dev

将文件上传到Web服务器Delphi + PHP

来自分类Dev

将图片文件上传到其他服务器

来自分类Dev

将xml文件上传到服务器

来自分类Dev

使用curl将文件上传到django服务器

来自分类Dev

如何将文件上传到ftp服务器?

来自分类Dev

将文件上传到ftp服务器时出错

来自分类Dev

使用AJAX将Zip文件上传到服务器

来自分类Dev

tizen将文件上传到服务器

来自分类Dev

使用python将文件上传到Django服务器

来自分类Dev

将文件上传到Codeigniter路径之外的服务器

来自分类Dev

Drupal将文件上传到服务器

来自分类Dev

将单个文件上传到多个服务器

来自分类Dev

将文件上传到我的Apache服务器

来自分类Dev

将文件夹上传到FTP服务器

来自分类Dev

无法使用curl将文件上传到服务器

来自分类Dev

将文件上传到ftp服务器时出错

来自分类Dev

在后台将文件上传到服务器

来自分类Dev

无法将文件上传到服务器Laravel

来自分类Dev

使用NSURLSessionUploadTask将文件上传到PHP服务器