eclipseを使ってjarに打って包んでそしてバッチを作ってbatを処理します。
(1)梱包方法:右クリック工程名--export-jar File--包装が必要なファイルにチェックを付ける(デフォルトでOK)
(2)バッチ処理を行う:start.batを作成し、内容は下記の通りである。
set classipath=download.jar;common-i-o-1.11.jar //ここで必要な第三者jarと(1)タクシーのjarは全部指定して、セミコロンでjava FileSaveを送ります。 //ここはmainメソッドを含むクラス名です。
pause
追加:業務jarパッケージはpropertiesファイルを読み込む必要があります。このパスは包装前にルートディレクトリに対して、包装後、jarパッケージにpropertiesファイルを追加しないと、propertiesファイルが見つかりません。
ですから、できれば配置ファイルをプロジェクトのルートディレクトリに入れて、jarを打ってから、同じクラスのディレクトリに入れてください。
例:config.propertiesはルートディレクトリに位置します。
(2)バッチ処理を行う:start.batを作成し、内容は下記の通りである。
set classipath=download.jar;common-i-o-1.11.jar //ここで必要な第三者jarと(1)タクシーのjarは全部指定して、セミコロンでjava FileSaveを送ります。 //ここはmainメソッドを含むクラス名です。
pause
追加:業務jarパッケージはpropertiesファイルを読み込む必要があります。このパスは包装前にルートディレクトリに対して、包装後、jarパッケージにpropertiesファイルを追加しないと、propertiesファイルが見つかりません。
ですから、できれば配置ファイルをプロジェクトのルートディレクトリに入れて、jarを打ってから、同じクラスのディレクトリに入れてください。
例:config.propertiesはルートディレクトリに位置します。
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import java.util.Random;
import org.apache.commons.io.FileUtils;
public class FileSave
{
static String remoteUrl = "";
static String saveFileUrl = "";
static int threadNums = 0;
public static void main(String[] args) throws IOException
{
//
readConfig();
for (int i = 0; i < threadNums; i++)
{
new Thread(new myThread(remoteUrl, saveFileUrl)).start();
}
}
public static void readConfig() throws IOException
{
String fp = System.getProperty("user.dir") + File.separator
+ "config.properties";
File file = new File(fp);
Properties properties = new Properties();
java.io.FileInputStream in = new java.io.FileInputStream(file);
properties.load(in);
in.close();
remoteUrl = properties.getProperty("remoteUrl");
saveFileUrl = properties.getProperty("saveFileUrl") + ":/download/";
threadNums = Integer.parseInt(properties.getProperty("threadNums"));
}
}
class myThread implements Runnable
{
static long NUM = 0;
String remoteUrl = "";
String saveFileUrl = "";
public myThread(String remoteUrl, String saveFileUrl)
{
this.remoteUrl = remoteUrl;
this.saveFileUrl = saveFileUrl;
}
public void run()
{
downloadFromUrl(remoteUrl, saveFileUrl);
}
/**
*
*/
public static String downloadFromUrl(String url, String dir)
{
String fileName = "";
try
{
URL httpurl = new URL(url);
String[] us = url.split("/");
fileName = us[us.length - 1];
String ramdom = System.currentTimeMillis() + ""
+ new Random().nextInt(100) + new Random().nextInt(100)
+ new Random().nextInt(100) + getSequence();
fileName = ramdom + "_" + fileName;
System.out.println("fileName:" + fileName);
File f = new File(dir + fileName);
FileUtils.copyURLToFile(httpurl, f);
} catch (Exception e)
{
e.printStackTrace();
return "Fault!";
}
return fileName;
}
public static synchronized long getSequence()
{
if (NUM > 100000000)
{
NUM = 0;
}
return NUM++;
}
}