方法一,採用java vuer開發php
import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import lrapi.lr; public class Actions { public int init() throws Throwable { return 0; }//end of init public int action() throws Throwable { int DownLoadSize = 0; String path = "c:\\temp\\"; //設置下載文件保存路徑 String vuid = String.valueOf(lr.get_vuser_id()); //獲取當前虛擬用戶ID並轉換成字符串 DownLoadSize = UrlTools.getHttpFileByUrl("http://forum.ubuntu.org.cn/download/file.php?id=129973&sid=78fc8d76767ef49b606595824ceb963d",vuid,path); //調用UrlTools.getHttpFileByUrl() return 0; }//end of action public int end() throws Throwable { return 0; }//end of end } class UrlTools { public static int getHttpFileByUrl(String address,String userid,String path) { //定義下面須要用到的變量 URL url; URLConnection conn = null; int BUFF_SIZE = 1024; byte[] buf = new byte[BUFF_SIZE]; int DownLoadSize = 0; BufferedInputStream bis; FileOutputStream fos = null; int size = 0; try { url = new URL(address); //address爲傳遞進來須要下載的連接 conn = url.openConnection();//使用url實例化須要下載的連接 bis = new BufferedInputStream(conn.getInputStream()); //把須要下載的文件內容保存在bis這個輸入流中 fos = new FileOutputStream(path+"\"+userid+"test000001"+「.pdf」));//組成完整路徑,並實例化到輸出流,這裏能夠進行參數化,如參數化文件名, // 路徑須要事先手動建立好,固然你也能夠在腳本中建立實現不一樣的路徑 System.out.println("須要下載的文件大小爲:" + conn.getContentLength()/1024 + "k"); while((size = bis.read(buf)) != -1) #按照設置的buf大小寫文件並記錄下載的大小 { fos.write(buf,0,size); DownLoadSize = DownLoadSize+size; } bis.close(); fos.close(); System.out.println("用戶" + userid + "下載" + url +"完成!"); }
catch(MalformedURLException e) { System.out.println("下載發生異常:"); e.printStackTrace(); }
catch(IOException e) { System.out.println("下載發生異常:"); e.printStackTrace(); } return DownLoadSize/1024; } }
方法二:將getHttpFileByUrl函數單獨封裝起來,在lr中調用,在Eclipse中新建一個.javavue