Analog post upload file in Java

Source: Internet
Author: User



/** * * @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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.