您好,登錄后才能下訂單哦!
學習到了安卓上傳文件了!!!
在Android端,用到的是android-async-http框架,
github地址為:https://github.com/koush/AndroidAsync/
在AS中搭建該框架超級簡單,只需要在build.gradle中加入下面2句(下圖中×××標記的2處),然后build一下項目,AS會自動把該框架需要的jar包放入到lib里。
由于sdk自從5.0(或者6.0)之后拋棄了httpclient,所以用這個框架,需要手動添加一些配置,來使Android支持httpclient。
同樣在build.gradle中加入標紅的那一行配置即可
apply : android { compileSdkVersion buildToolsVersion defaultConfig { minSdkVersion targetSdkVersion } buildTypes { release { proguardFiles getDefaultProguardFile(), } } } dependencies { compile fileTree(: , : []) androidTestCompile(, { exclude : , : }) compile compile testCompile }
1、MainActivity
package com.yuanlp.fileupload; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import cz.msebera.android.httpclient.Header; public class MainActivity extends AppCompatActivity { private EditText mPost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPost = (EditText) findViewById(R.id.et_post); /** * 在cache文件夾下創建一個文件,用來上傳 */ File file=new File(getCacheDir(),"upload.txt"); try { BufferedWriter writer=new BufferedWriter(new FileWriter(file)); writer.write("upload,test"); writer.flush(); } catch (IOException e) { e.printStackTrace(); } } /** * button的onclick方法 * @param view */ public void upload(View view){ String path=mPost.getText().toString().trim(); if (TextUtils.isEmpty(path)){ Toast.makeText(this, "路徑不能為空", Toast.LENGTH_SHORT).show(); } File file=new File(path); if (file.exists()&&file.length()>0){ //判斷文件是否存在,且是否有內容 /** * 下面是用框架做的上傳文件 */ AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); RequestParams params = new RequestParams(); try { params.put("file1",file); //這個名字一定要與web端獲取文件時填寫的名字一致,不然multipart獲取不到文件 asyncHttpClient.post("http://192.168.1.111:10010/aos/talk/audio/savePdaFiles.jhtml", params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { Toast.makeText(MainActivity.this,"上傳成功",Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { Toast.makeText(MainActivity.this,"上傳失敗",Toast.LENGTH_SHORT).show(); } }); } catch (Exception e) { e.printStackTrace(); } }else{ Toast.makeText(this, "文件不存在或內容為空", Toast.LENGTH_SHORT).show(); } } }
2 java web端,用springMVC
本來獲取到multipartfile后,是通過multipart.getInputStream()方式來生成file文件,寫入到硬盤的,但是一直獲取不到這個輸入流,暫時也沒查到什么原因。就換了一種方法,直接用multipart的transferTo()方法,參數里是個file類型文件,這樣子把文件寫入后臺
@RequestMapping(value = "savePdaFiles")
public void savePdaFiles(HttpServletRequest request, HttpServletResponse response) {
String file_path=request.getSession().getServletContext().getRealPath("upload/media");
MultipartHttpServletRequest multipartHttpServletRequest=(MultipartHttpServletRequest) request;
MultipartFile multipartFile=multipartHttpServletRequest.getFile("file1");
String fileName=multipartFile.getOriginalFilename();
try {
multipartFile.transferTo(new File(file_path+fileName));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
3、web.xml配置攔截
<!-- Http請求攔截過濾器 -->
<filter>
<filter-name>httpRequestFilter</filter-name>
<filter-class>cn.osworks.aos.web.asset.HttpRequestFilter</filter-class>
<init-param>
<param-name>enabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>excludes</param-name>
<param-value>login.jhtml,pdaLogin.jhtml,savePdaFiles.jhtml,savePdaFileList.jhtml,/esb/</param-value>
</init-param>
</filter>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。