学习到了安卓上传文件了!!!
在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。