iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现上传文件功能的方法
  • 291
分享到

Android实现上传文件功能的方法

方法上传文件Android 2022-06-06 10:06:26 291人浏览 安东尼
摘要

本文所述为一个Android上传文件的源代码,每一步实现过程都备有详尽的注释,思路比较清楚,学习了本例所述上传文件代码之后,你可以应对其它格式文件的上传。实例中主要实现上传文件

本文所述为一个Android上传文件的源代码,每一步实现过程都备有详尽的注释,思路比较清楚,学习了本例所述上传文件代码之后,你可以应对其它格式文件的上传。实例中主要实现上传文件至Server的方法,允许Input、Output,不使用Cache,使Androiod上传文件变得轻松。

主要功能代码如下:


package com.test;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
  
  private String newName="image.jpg";
  private String uploadFile="/data/image.jpg";
  private String actionUrl="http://l27.0.0.1/upload/upload.jsp";
  private TextView mText1;
  private TextView mText2;
  private Button mButton;
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   mText1 = (TextView) findViewById(R.id.myText2);
   mText1.setText("文件路径:\n"+uploadFile);
   mText2 = (TextView) findViewById(R.id.myText3);
   mText2.setText("上传网址:\n"+actionUrl);
     
   mButton = (Button) findViewById(R.id.myButton);
   mButton.setOnClickListener(new View.OnClickListener()
   {
    public void onClick(View v)
    {
     uploadFile();
    }
   });
  }
  
  private void uploadFile()
  {
   String end = "\r\n";
   String twoHyphens = "--";
   String boundary = "*****";
   try
   {
    URL url =new URL(actionUrl);
    HttpURLConnection con=(HttpURLConnection)url.openConnection();
    
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    
    con.setRequestMethod("POST");
    
    con.setRequestProperty("Connection", "Keep-Alive");
    con.setRequestProperty("Charset", "UTF-8");
    con.setRequestProperty("Content-Type",
             "multipart/fORM-data;boundary="+boundary);
    
    DataOutputStream ds = 
     new DataOutputStream(con.getOutputStream());
    ds.writeBytes(twoHyphens + boundary + end);
    ds.writeBytes("Content-Disposition: form-data; " +
           "name=\"file1\";filename=\"" +
           newName +"\"" + end);
    ds.writeBytes(end);  
    
    FileInputStream fStream = new FileInputStream(uploadFile);
    
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];
    int length = -1;
    
    while((length = fStream.read(buffer)) != -1)
    {
     
     ds.write(buffer, 0, length);
    }
    ds.writeBytes(end);
    ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
    
    fStream.close();
    ds.flush();
    
    InputStream is = con.getInputStream();
    int ch;
    StringBuffer b =new StringBuffer();
    while( ( ch = is.read() ) != -1 )
    {
     b.append( (char)ch );
    }
    
    showDialog(b.toString().trim());
    
    ds.close();
   }
   catch(Exception e)
   {
    showDialog(""+e);
   }
  }
  
  private void showDialog(String mess)
  {
   new AlertDialog.Builder(Main.this).setTitle("Message")
   .setMessage(mess)
   .setNegativeButton("确定",new DialogInterface.OnClickListener()
   {
    public void onClick(DialogInterface dialog, int which)
    {     
    }
   })
   .show();
  }
}

读者如果觉得功能不足的话可以对代码进行扩展与完善,使之更加符合自身的应用需求。

您可能感兴趣的文章:android文件上传示例分享(android图片上传)Android实现本地上传图片并设置为圆形头像Android使用post方式上传图片到服务器的方法Android Retrofit 2.0框架上传图片解决方案Android中发送Http请求(包括文件上传、servlet接收)的实例代码android 上传文件到服务器代码实例android 捕获系统异常并上传日志具体实现Android仿微信发表说说实现拍照、多图上传功能android 拍照和上传的实现代码SimpleCommand实现上传文件或视频功能(四)


--结束END--

本文标题: Android实现上传文件功能的方法

本文链接: https://www.lsjlt.com/news/26994.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作