最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android上傳文件到服務(wù)端并顯示進(jìn)度條

 更新時間:2021年08月19日 16:09:55   作者:cc雜貨圈  
這篇文章主要為大家詳細(xì)介紹了Android上傳文件到服務(wù)端,并顯示進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近在做上傳文件的服務(wù),簡單看了網(wǎng)上的教程。結(jié)合實踐共享出代碼。

由于網(wǎng)上的大多數(shù)沒有服務(wù)端的代碼,這可不行呀,沒服務(wù)端怎么調(diào)試呢。

Ok,先上代碼。

Android 上傳比較簡單,主要用到的是 HttpURLConnection 類,然后加一個進(jìn)度條組件。

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(); 
 
    //如果有必要則可以設(shè)置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 去上傳文件。代碼比較簡單,就不一一講解了。
其中要注意的是需要在

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

將name 設(shè)置為web 請求的參數(shù)名,由于我的服務(wù)端是將文件設(shè)置為file參數(shù),所以我可以直接填file .所以大家可以根據(jù)實際情況作相應(yīng)修改。

那么接著上服務(wù)端代碼,服務(wù)端主要使用status 2框架作請求。那么我們就需要進(jìn)行封裝。

//上傳文件集合 
 private List<File> file; 
 //上傳文件名集合 
 private List<String> fileFileName; 
 //上傳文件內(nèi)容類型集合 
 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; 
 } 

到這里就已經(jīng)才不多完成動作了,現(xiàn)在需要開始寫上傳的方法 uploadFile(int index),由于定義file 為多文件上傳,而我們上傳只上傳了一個文件,所以這里參數(shù)為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(); 
   //然后進(jìn)行計算 
   return uploadFile.getAbsolutePath(); 
  } catch (FileNotFoundException ex) { 
   ex.printStackTrace(); 
  } catch (IOException ex) { 
   ex.printStackTrace(); 
  } 
  return null; 
 } 

上面方法為將緩存區(qū)域的文件 然后搞到了D://UploadData/ 文件中,然后以自己的格式進(jìn)行命名,這里我使用了電腦的UUID和文件名進(jìn)行組合,確保我復(fù)制過來的文件不重復(fù)。
最后上傳成功之后返回文件的真實地址。

ok,寫到這里上傳文件的功能基本上做完了。最后只剩下配置action 動作。

ok,我們打開status.xml 文件進(jìn)行配置

<!-- 系統(tǒng)常量定義,定義上傳文件字符集編碼 --> 
 <constant name="struts.i18n.encoding" value="utf-8"></constant> 
 <!-- 系統(tǒng)常量定義,定義上傳文件零時存放路徑 --> 
 <constant name="struts.multipart.saveDir" value="c:\tmp\"></constant> 
 <constant name="struts.multipart.maxSize" value="10000000" /> 

這里主要定義上傳文件的臨時存放位置,然后大小限制。
大家可以根據(jù)實際情況進(jìn)行配置。

最后上傳一張效果圖。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android編程實現(xiàn)圖片拍照剪裁的方法

    Android編程實現(xiàn)圖片拍照剪裁的方法

    這篇文章主要介紹了Android編程實現(xiàn)圖片拍照剪裁的方法,涉及Android調(diào)用裁剪工具操作圖片的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-12-12
  • Android實現(xiàn)簡易的鬧鐘功能

    Android實現(xiàn)簡易的鬧鐘功能

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)簡易的鬧鐘功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Android 6.0 無法在SD卡創(chuàng)建目錄的方法

    Android 6.0 無法在SD卡創(chuàng)建目錄的方法

    今天小編就為大家分享一篇Android 6.0 無法在SD卡創(chuàng)建目錄的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • flutter 怎么實現(xiàn)app整體灰色效果

    flutter 怎么實現(xiàn)app整體灰色效果

    Flutter 是 Google 開源的 UI 工具包,幫助開發(fā)者通過一套代碼庫高效構(gòu)建多平臺精美應(yīng)用,支持移動、Web、桌面和嵌入式平臺。這篇文章給大家介紹flutter 怎么實現(xiàn)app整體灰色效果,感興趣的朋友一起看看吧
    2020-04-04
  • Android條目拖拽刪除功能實例代碼

    Android條目拖拽刪除功能實例代碼

    最近做項目遇到這樣的需求,要做條目條目拖拽刪除效果,實際效果和QQ消息刪除一樣,側(cè)滑有制定和刪除,下面通過本文給大家分享Android條目拖拽刪除功能,需要的朋友參考下吧
    2017-08-08
  • Android入門之IntentService的使用教程詳解

    Android入門之IntentService的使用教程詳解

    IntentService的生命周期中有一個非常好的方法-onHandleIntent方法,它是一個abstract方法,開發(fā)者在實現(xiàn)IntentService時可以覆蓋它來處理“長事務(wù)”。本文就來聊聊IntentService的使用,需要的可以參考一下
    2022-12-12
  • Android實現(xiàn)通話自動錄音

    Android實現(xiàn)通話自動錄音

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)通話自動錄音,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Flutter學(xué)習(xí)之構(gòu)建、布局及繪制三部曲

    Flutter學(xué)習(xí)之構(gòu)建、布局及繪制三部曲

    這篇文章主要給大家介紹了關(guān)于Flutter學(xué)習(xí)之構(gòu)建、布局及繪制三部曲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Android自定義TextView實現(xiàn)drawableLeft內(nèi)容居中

    Android自定義TextView實現(xiàn)drawableLeft內(nèi)容居中

    這篇文章主要介紹了Android自定義TextView實現(xiàn)drawableLeft內(nèi)容居中的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android Retrofit框架的使用

    Android Retrofit框架的使用

    這篇文章主要介紹了Android Retrofit框架的使用,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03

最新評論

共和县| 繁峙县| 阿勒泰市| 藁城市| 义马市| 和平区| 新晃| 通城县| 合江县| 和硕县| 泰州市| 武定县| 邯郸县| 西城区| 富锦市| 黑河市| 平乡县| 双江| 黄石市| 原阳县| 自治县| 千阳县| 辛集市| 甘泉县| 盈江县| 高要市| 甘孜| 德江县| 溆浦县| 车致| 乐亭县| 光泽县| 灌南县| 锡林浩特市| 郑州市| 沙洋县| 巴彦县| 斗六市| 株洲市| 榆中县| 铜山县|