Download an object through a url or a url
1. Problem Description
Download the file to the local device through the File url. File storage location: tomcat server folder (by reading the properties file: visible: http://www.cnblogs.com/0201zcr/p/4700418.html)
2. Implementation ideas
Read the properties file and obtain the location where the file will be stored.
Use the java Url class to download online files to a local device.
3. Code Implementation
1) read the properties file (a web project is created here)
Package com. zcr. until; import java. io. bufferedInputStream; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java. util. properties; public class GetFilePlace {/*** read the file and obtain the saved root directory * @ return the saved root directory */public String getFilePath (String fileProperties) {String dir = System. getProperty ("user. dir "); // obtain the working path of tomcat // obtain filedir that stores the file storage location. properties File path // String realDir = dir + File. separator + "src" + File. separator + "META-INF" + File. separator + "config" + File. separator + "picture. properties "; String realDir = dir. substring (0, dir. length ()-4) + File. separator + "webapps" + File. separator + "appDataGenerate" + File. separator + "WEB-INF" + File. separator + "classes" + File. separator + "META-INF" + File. separator + "config" + File. separator + fileProperties;/* String realDir = dir. substring (0, dir. length ()-4) + File. separator + "webapps" + File. separator + "appDataGenerate" + File. separator + "classes" + File. separator + "META-INF" + File. separator + "config" + File. separator + fileProperties; */System. out. println ("realDir =" + realDir); return realDir;}/*** get the value of the key in the filePath [properities file, * @ param filePath properities file path [including properities file] * @ param key the key value to be searched * @ return key the value */public String GetValueByKey (String filePath, String key) {Properties pps = new Properties (); try {InputStream in = new BufferedInputStream (new FileInputStream (filePath); pps. load (in); String value = pps. getProperty (key); in. close (); return value;} catch (IOException e) {e. printStackTrace (); return null ;}} /*** query the storage location corresponding to the properities file * @ param key query the storage address corresponding to the primary key * @ return key */public String getFileDirFromProperties (String key, String fileProperties) {return GetValueByKey (getFilePath (fileProperties), key);} public static void main (String [] args) {System. out. println (new GetFilePlace (). getFileDirFromProperties ("brandLogo", "picture. properties "));}}
2) File Download class
Package com. zcr. until; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java.net. malformedURLException; import java.net. URL; import java.net. URLConnection; import javax. servlet. http. httpServletRequest; public class URLConnectionDownloader {
// Test download public static void main (String [] args) {download ("http://stocardapp.s3-external-3.amazonaws.com/ios/icons/1001tur@2x.png", "E: \ xiazai.jpg ");} /*** download the urlString file to the * @ param filePathName properties file's file storage name * @ param fileProperties find the properties file * @ param urlString url of the file to be downloaded * @ param file name generated by fileName */public static void downloadToSelectedFolder (String filePathName, string filePropertie S, String urlString, String fileName, HttpServletRequest request) {// obtain picture. in the properties file, the value of key is android_banner_url String pathSavePath = new GetFilePlace (). getFileDirFromProperties ("android_banner_url", "picture. properties "); // obtain the relative position of the server (tomcat) pathSavePath String path = request. getSession (). getServletContext (). getRealPath (pathSavePath); // obtain the absolute path of the File storage String generateFileName = path + File. separa Tor + fileName; download (urlString, generateFileName );} /*** download the file locally ** @ param urlString * the address of the downloaded file * @ param filename * local file name */public static void download (String urlString, String filename) {// construct the URL; try {url = new url (urlString); // open the URL Connection. con = url. openConnection (); // input stream InputStream is = con. getInputStream (); // 1 k Data Buffer byte [] bs = new byte [1024]; // read Data Length int len; // Output file stream s OutputStream OS = new FileOutputStream (filename); // start to read while (len = is. read (bs ))! =-1) {OS. write (bs, 0, len);} // complete. Close all connected OS. close (); is. close ();} catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
3) webpage calls
URLConnectionDownloader.downloadToSelectedFolder("android_banner_url","picture.properties","http://stocardapp.s3-external-3.amazonaws.com/ios/icons/1001tur@2x.png","2x.png",request);
4) Test Results
Webpage images:
Downloaded Image
Thank you: Thank you for reading this article!