1. 程式人生 > >利用loopj的android-async-http進行文件上傳

利用loopj的android-async-http進行文件上傳

android 上傳文件

學習到了安卓上傳文件了!!!

在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

com.yuanlp.fileuploadandroid.os.Bundleandroid.support.v7.app.AppCompatActivityandroid.text.TextUtilsandroid.view.Viewandroid.widget.EditTextandroid.widget.Toastcom.loopj.android.http.AsyncHttpClientcom.loopj.android.http.AsyncHttpResponseHandlercom.loopj.android.http.RequestParamsjava.io.BufferedWriterjava.io.Filejava.io.FileWriterjava.io.IOExceptioncz.msebera.android.httpclient.HeaderMainActivity AppCompatActivity {

    EditText (Bundle savedInstanceState) {
        .onCreate(savedInstanceState)setContentView(R.layout.)= (EditText) findViewById(R.id.)File file=File(getCacheDir()){
            BufferedWriter writer=BufferedWriter(FileWriter(file))writer.write()writer.flush()} (IOException e) {
            e.printStackTrace()}
    }

    (View view){
            String path=.getText().toString().trim()(TextUtils.(path)){
            Toast.(Toast.).show()}
        File file=File(path)(file.exists()&&file.length()>){ AsyncHttpClient asyncHttpClient = AsyncHttpClient()RequestParams params = RequestParams(){
                params.put(file)asyncHttpClient.post(paramsAsyncHttpResponseHandler() {
                    (statusCodeHeader[] headers[] responseBody) {
                        Toast.(MainActivity.Toast.).show()}

                    (statusCodeHeader[] headers[] responseBodyThrowable error) {
                        Toast.(MainActivity.Toast.).show()}
                })} (Exception e) {
                e.printStackTrace()}
        }{
            Toast.(Toast.).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>


本文出自 “YuanGuShi” 博客,請務必保留此出處http://cm0425.blog.51cto.com/10819451/1941447

利用loopj的android-async-http進行文件上傳