Android上传文件到服务端并显示进度条

 更新时间:2021年08月19日 16:09:55   作者:cc杂货圈  
这篇文章主要为大家详细介绍了Android上传文件到服务端,并显示进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近在做上传文件的服务,简单看了网上的教程。结合实践共享出代码。

由于网上的大多数没有服务端的代码,这可不行呀,没服务端怎么调试呢。

Ok,先上代码。

Android 上传比较简单,主要用到的是 HttpURLConnection 类,然后加一个进度条组件。

private ProgressBar mPgBar; 
class UploadTask extends AsyncTask<Object,Integer,Void>{ 
  private DataOutputStream outputStream = null; 
  private String fileName; 
  private String uri; 
  private String mLineEnd = "\r\n"; 
  private String mTwoHyphens = "--"; 
  private String boundary = "*****"; 
  File uploadFile ; 
  long mTtotalSize ; // Get size of file, bytes 
  public UploadTask(String fileName,String uri){ 
   this.fileName = fileName; 
   this.uri = uri; 
   uploadFile= new File(fileName); 
    mTtotalSize = uploadFile.length(); 
  } 
 
  /** 
   * 开始上传文件 
   * @param objects 
   * @return 
   */ 
  @Override 
  protected Void doInBackground(Object... objects) { 
   long length = 0; 
   int mBytesRead, mbytesAvailable, mBufferSize; 
   byte[] buffer; 
   int maxBufferSize = 256 * 1024;// 256KB 
   try{ 
 
    FileInputStream fileInputStream = new FileInputStream(new File(fileName)); 
 
    URL url = new URL(uri); 
 
    HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
 
    //如果有必要则可以设置Cookie 
//    conn.setRequestProperty("Cookie","JSESSIONID="+cookie); 
 
    // Set size of every block for post 
 
    con.setChunkedStreamingMode(256 * 1024);// 256KB 
 
    // Allow Inputs & Outputs 
    con.setDoInput(true); 
    con.setDoOutput(true); 
    con.setUseCaches(false); 
 
    // Enable POST method 
    con.setRequestMethod("POST"); 
    con.setRequestProperty("Connection", "Keep-Alive"); 
    con.setRequestProperty("Charset", "UTF-8"); 
    con.setRequestProperty("Content-Type", 
      "multipart/form-data;boundary=" + boundary); 
 
    outputStream = new DataOutputStream( 
      con.getOutputStream()); 
    outputStream.writeBytes(mTwoHyphens + boundary + mLineEnd); 
    outputStream.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"" + mLineEnd); 
    outputStream.writeBytes("Content-Type:application/octet-stream \r\n"); 
    outputStream.writeBytes(mLineEnd); 
 
    mbytesAvailable = fileInputStream.available(); 
    mBufferSize = Math.min(mbytesAvailable, maxBufferSize); 
    buffer = new byte[mBufferSize]; 
 
    // Read file 
    mBytesRead = fileInputStream.read(buffer, 0, mBufferSize); 
 
    while (mBytesRead > 0) { 
     outputStream.write(buffer, 0, mBufferSize); 
     length += mBufferSize; 
 
     publishProgress((int) ((length * 100) / mTtotalSize)); 
 
     mbytesAvailable = fileInputStream.available(); 
 
     mBufferSize = Math.min(mbytesAvailable, maxBufferSize); 
 
     mBytesRead = fileInputStream.read(buffer, 0, mBufferSize); 
    } 
    outputStream.writeBytes(mLineEnd); 
    outputStream.writeBytes(mTwoHyphens + boundary + mTwoHyphens 
      + mLineEnd); 
    publishProgress(100); 
 
    // Responses from the server (code and message) 
    int serverResponseCode = con.getResponseCode(); 
    String serverResponseMessage = con.getResponseMessage(); 
    fileInputStream.close(); 
    outputStream.flush(); 
    outputStream.close(); 
 
   } catch (Exception ex) { 
    ex.printStackTrace(); 
    Log.v(TAG,"uploadError"); 
   } 
   return null; 
  } 
 
  @Override 
  protected void onProgressUpdate(Integer... progress) { 
   mPgBar.setProgress(progress[0]); 
  } 
 } 

主要流程为继承AsyncTask,然后使用HttpURLConnection 去上传文件。代码比较简单,就不一一讲解了。
其中要注意的是需要在

复制代码 代码如下:
outputStream.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" +  fileName + "\"" + mLineEnd); 

将name 设置为web 请求的参数名,由于我的服务端是将文件设置为file参数,所以我可以直接填file .所以大家可以根据实际情况作相应修改。

那么接着上服务端代码,服务端主要使用status 2框架作请求。那么我们就需要进行封装。

//上传文件集合 
 private List<File> file; 
 //上传文件名集合 
 private List<String> fileFileName; 
 //上传文件内容类型集合 
 private List<String> fileContentType; 
 
 public List<File> getFile() { 
  return file; 
 } 
 
 public void setFile(List<File> file) { 
  this.file = file; 
 } 
 
 public List<String> getFileFileName() { 
  return fileFileName; 
 } 
 
 public void setFileFileName(List<String> fileFileName) { 
  this.fileFileName = fileFileName; 
 } 
 
 public List<String> getFileContentType() { 
  return fileContentType; 
 } 
 
 public void setFileContentType(List<String> fileContentType) { 
  this.fileContentType = fileContentType; 
 } 

采用了多文件上传的方法,定义了List 集合。
那么处理文件上传的action ,由于是测试方法。这里就定义为testUpload

public String testUpload()throws Exception{ 
  System.out.println("success"); 
  uploadFile(0); 
  return SUCCESS; 
 } 

到这里就已经才不多完成动作了,现在需要开始写上传的方法 uploadFile(int index),由于定义file 为多文件上传,而我们上传只上传了一个文件,所以这里参数为0

/** 
  * 上传功能 
  * @param i 
  * @return 
  * @throws FileNotFoundException 
  * @throws IOException 
  */ 
 private String uploadFile(int i) throws FileNotFoundException, IOException { 
   
  try { 
   InputStream in = new FileInputStream(file.get(i)); 
 
   //String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR); 
    
   String dir = "D://UploadData/"; 
 
   File uploadFile = new File(dir,StringUtils.getUUID()+getFile( this.getFileFileName().get(i))); 
 
   OutputStream out = new FileOutputStream(uploadFile); 
 
   byte[] buffer = new byte[1024 * 1024]; 
 
   int length; 
   while ((length = in.read(buffer)) > 0) { 
    out.write(buffer, 0, length); 
   } 
 
   in.close(); 
   out.close(); 
   //然后进行计算 
   return uploadFile.getAbsolutePath(); 
  } catch (FileNotFoundException ex) { 
   ex.printStackTrace(); 
  } catch (IOException ex) { 
   ex.printStackTrace(); 
  } 
  return null; 
 } 

上面方法为将缓存区域的文件 然后搞到了D://UploadData/ 文件中,然后以自己的格式进行命名,这里我使用了电脑的UUID和文件名进行组合,确保我复制过来的文件不重复。
最后上传成功之后返回文件的真实地址。

ok,写到这里上传文件的功能基本上做完了。最后只剩下配置action 动作。

ok,我们打开status.xml 文件进行配置

<!-- 系统常量定义,定义上传文件字符集编码 --> 
 <constant name="struts.i18n.encoding" value="utf-8"></constant> 
 <!-- 系统常量定义,定义上传文件零时存放路径 --> 
 <constant name="struts.multipart.saveDir" value="c:\tmp\"></constant> 
 <constant name="struts.multipart.maxSize" value="10000000" /> 

这里主要定义上传文件的临时存放位置,然后大小限制。
大家可以根据实际情况进行配置。

最后上传一张效果图。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 深入学习Android中的Intent

    深入学习Android中的Intent

    深入学习Android中的Intent,Intent提供了一种通用的消息系统,它允许在你的应用程序见传递Intent来执行动作和产生事件,对Intent感兴趣的小伙伴们可以参考一下
    2015-12-12
  • Android 菜单栏DIY实现效果详解

    Android 菜单栏DIY实现效果详解

    这篇文章主要为大家介绍了Android 菜单栏DIY实现效果详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Android仿微博加载长图滚动查看效果

    Android仿微博加载长图滚动查看效果

    这篇文章主要为大家详细介绍了Android仿微博加载长图滚动查看效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android开发实现模仿360二维码扫描功能实例详解

    Android开发实现模仿360二维码扫描功能实例详解

    这篇文章主要介绍了Android开发实现模仿360二维码扫描功能,结合实例形式详细分析了Android开发二维码扫描功能所涉及的zxing开源项目文件使用方法及具体扫码功能相关实现技巧,需要的朋友可以参考下
    2017-10-10
  • Android多线程断点续传下载示例详解

    Android多线程断点续传下载示例详解

    这篇文章主要为大家详细介绍了Android多线程断点续传下载示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • Android实现在列表List中显示半透明小窗体效果的控件用法详解

    Android实现在列表List中显示半透明小窗体效果的控件用法详解

    这篇文章主要介绍了Android实现在列表List中显示半透明小窗体效果的控件用法,结合实例形式分析了Android半透明提示框的实现与设置技巧,需要的朋友可以参考下
    2016-06-06
  • Android 多层嵌套后的 Fragment 懒加载实现示例

    Android 多层嵌套后的 Fragment 懒加载实现示例

    这篇文章主要介绍了Android 多层嵌套后的 Fragment 懒加载实现示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Android Jetpack结构运用Compose实现微博长按点赞彩虹效果

    Android Jetpack结构运用Compose实现微博长按点赞彩虹效果

    Compose在动画方面下足了功夫,提供了丰富的API。但也正由于API种类繁多,如果想一气儿学下来,最终可能会消化不良,导致似懂非懂。结合例子学习是一个不错的方法,本文就带大家边学边做,通过实现一个微博长按点赞的动画效果,学习了解Compose动画的常见思路和开发技巧
    2022-07-07
  • Kotlin匿名函数使用介绍

    Kotlin匿名函数使用介绍

    定义时不取名字的函数,我们称之为匿名函数,匿名函数通常整体传递给其他函数或者从其他函数返回,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • Android 实现锚点定位思路详解

    Android 实现锚点定位思路详解

    本篇文章就使用tablayout、scrollview来实现android锚点定位的功能。通过<a href="#head" rel="external nofollow" > 去设置页面内锚点定位跳转。具体实现思路大家跟随脚本之家小编一起通过本文看下吧
    2018-07-07

最新评论