Learned to upload files on the android!!!
On the Android side, the android-async-http frame is used,
The GitHub address is: https://github.com/koush/AndroidAsync/
It's super simple to build the frame in as, just add the following 2 sentences (2 in the yellow flag) to the Build.gradle, and build the project, as will automatically put the jar package that the framework needs into LIB.
Since the SDK has abandoned httpclient since 5.0 (or 6.0), with this framework, you need to manually add some configuration to enable Android to support HttpClient.
Also add red in the Build.gradle line configuration can be
apply:android {compilesdkversion buildtoolsversion defaultconfig {minsdkversion targetsdkversion} BuildT Ypes {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.Viewandr Oid.widget.EditTextandroid.widget.Toastcom.loopj.android.http.AsyncHttpClientcom.loopj.android.http.AsyncHttpResponseHand Lercom.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 side, with SPRINGMVC
Originally obtained to Multipartfile, is through the Multipart.getinputstream () way to generate file files, write to the hard disk, but always get not to this input stream, temporarily did not find out what the reason. In a different way, directly using the multipart Transferto () method, the parameter is a file type files, this way to write files to the background
@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 configuration Interception
<!--http request blocking filters--
<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>
This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1941447
File upload using LOOPJ's android-async-http