学习到了安卓上传文件了!!!
在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>