/** * * @paramURL Request URL *@paramFilePath Local File address *@return */ Public Staticstring Upload (string url,string filePath) {string Fdfspath= ""; Try{HttpClient HttpClient=Newdefaulthttpclient (); HttpPost HttpPost=Newhttppost (URL); File File=NewFile (FilePath); String name=File.getname (); InputStream in=Newfileinputstream (file); Multipartentity reqentity=Newmultipartentity (); Inputstreambody Inputstreambody=Newinputstreambody (in,name); Stringbody Filenam=Newstringbody (name); Stringbody Dateflag=NewStringbody ("20160122152301"); Stringbody Datumtype=NewStringbody ("0"); Stringbody Uploadway=NewStringbody ("0"); Stringbody userId=NewStringbody ("0538"); Stringbody Tenderid=NewStringbody ("2315"); Stringbody Metrialstype=NewStringbody ("25"); Stringbody IP=NewStringbody ("0.0.0.1"); Stringbody drivername=NewStringbody ("Huawei"); Stringbody systemversion=NewStringbody ("DJF"); Stringbody position=NewStringbody ("Information Road", Charset.forname ("UTF8"))); File stream
Reqentity.addpart ("Datums", Inputstreambody); Reqentity.addpart ("FileName", Filenam); Reqentity.addpart ("Dateflag", Dateflag); Reqentity.addpart ("Datumtype", Datumtype); Reqentity.addpart ("Uploadway", Uploadway); Reqentity.addpart ("UserId", userId); Reqentity.addpart ("Tenderid", Tenderid); Reqentity.addpart ("Metrialstype", Metrialstype); Reqentity.addpart ("IP", IP); Reqentity.addpart ("DriverName", drivername); Reqentity.addpart ("Systemversion", systemversion); Reqentity.addpart ("Position", position); Httppost.setentity (reqentity); HttpResponse Response=Httpclient.execute (HttpPost); intStatusCode =response.getstatusline (). Getstatuscode (); if(StatusCode = =HTTPSTATUS.SC_OK) {System.out.println ("Server normal response ..."); Httpentity resentity=response.getentity (); System.out.println (entityutils.tostring (resentity));//HttpClient comes with the tool class to read the returned dataSystem.out.println (Resentity.getcontent ()); Entityutils.consume (resentity); } } Catch(Exception e) {e.printstacktrace (); } return""; } /** * @paramargs*/ Public Static voidMain (string[] args) {Upload ("Http://192.168.1.1:8080/xxxImageUpload.action", "e:\\weatertest\\002.jpg"); }
Image download
Private Static voidDownfile () {Try{String path= "E:\\downurl\\2016022302\\"; File Downfileurl=NewFile (path); file[] Files=Downfileurl.listfiles (); for(File file:files) {BufferedReader bfr=NewBufferedReader (NewInputStreamReader (Newfileinputstream (file)); String Downparas=NULL; while((Downparas=bfr.readline ())! =NULL) {System.out.println ("Download parameters:" +Downparas); String[] dows= Downparas.split ("&"); if(dows==NULL|| Dows.length<3) {System.out.println ("Data is unhealthy Downparas:" +Downparas); }Else{String Tenderflod= Path+dows[0].trim (); File Tender_fold=NewFile (Tenderflod.trim ()); if(!tender_fold.exists ()) {System.out.println ("Create folder:" +Tenderflod.trim ()); Tender_fold.mkdir (); } String leifold= tenderflod+ "\ \" +dows[1].trim (); File Lei_fold=NewFile (Leifold); if(!lei_fold.exists ()) {System.out.println ("Create folder:" +leifold); Lei_fold.mkdir (); } HttpClient HttpClient=Newdefaulthttpclient (); HttpPost HttpPost=NewHttpPost ("Http://imagelocal.eloancn.com/xxxdownImg.action"); Stringbody FileName=NewStringbody (dows[2]); Multipartentity reqentity=Newmultipartentity (); Reqentity.addpart ("Imgpath", fileName);//FileName File namehttppost.setentity (reqentity); HttpResponse Response=Httpclient.execute (HttpPost); intStatusCode =response.getstatusline (). Getstatuscode (); if(StatusCode = =HTTPSTATUS.SC_OK) {System.out.println ("Server normal response ..." +dows[2].substring (Dows[2].lastindexof ("/") +1) + "Download complete. "); Httpentity resentity=response.getentity (); String Savepath= lei_fold+ "//" +dows[2].substring (Dows[2].lastindexof ("/") +1). Trim (); FileOutputStream Fos=NewFileOutputStream (NewFile (Savepath)); Resentity.writeto (FOS); } } } } } Catch(Exception e) {e.printstacktrace (); } }
Analog post upload file in Java