Wget.java
package com.karl.learn;
import com.karl.util.Utils;
public abstract class Wget {
private boolean supportContinuousDownload = true;
private String httpProxy = "";
public void download(String remotePath, String localPath, boolean sync) {
String command = buildWgetCommand(remotePath, localPath,
supportContinuousDownload, httpProxy);
executeCommand(command, sync);
}
private boolean executeCommand(String command, boolean sync) {
try {
Process p = Runtime.getRuntime().exec(command);
if (sync) {
p.waitFor();
}
return true;
} catch (Exception e) {
return false;
}
}
public static Wget getWget() {
if (Utils.isWindows()) {
return new WgetWindows();
} else {
return new WgetUnix();
}
}
abstract protected String buildWgetCommand(String remotePath,
String localPath, boolean supportContinuousDownload,
String httpProxy);
protected abstract String getLocalFileDirectory(String localFilePath);
protected String buildCommandForDownload(String commandPrefix,
String command, String commandSuffix, String remoteFilePath,
String localFilePath, boolean supportContinuousDownload,
String httpProxy) {
StringBuilder commandStr = new StringBuilder();
commandStr.append(commandPrefix);
commandStr.append(" ");
commandStr.append(command);
commandStr.append(" ");
commandStr.append(remoteFilePath);
commandStr.append(" ");
commandStr.append(getLocalFileDirectory(localFilePath));
commandStr.append(" ");
commandStr.append(localFilePath);
commandStr.append(" ");
commandStr.append(supportContinuousDownload ? "continue"
: "no-continue");
commandStr.append(" ");
commandStr.append(httpProxy);
commandStr.append(" ");
commandStr.append(commandSuffix);
return commandStr.toString();
}
import com.karl.util.Utils;
public abstract class Wget {
private boolean supportContinuousDownload = true;
private String httpProxy = "";
public void download(String remotePath, String localPath, boolean sync) {
String command = buildWgetCommand(remotePath, localPath,
supportContinuousDownload, httpProxy);
executeCommand(command, sync);
}
private boolean executeCommand(String command, boolean sync) {
try {
Process p = Runtime.getRuntime().exec(command);
if (sync) {
p.waitFor();
}
return true;
} catch (Exception e) {
return false;
}
}
public static Wget getWget() {
if (Utils.isWindows()) {
return new WgetWindows();
} else {
return new WgetUnix();
}
}
abstract protected String buildWgetCommand(String remotePath,
String localPath, boolean supportContinuousDownload,
String httpProxy);
protected abstract String getLocalFileDirectory(String localFilePath);
protected String buildCommandForDownload(String commandPrefix,
String command, String commandSuffix, String remoteFilePath,
String localFilePath, boolean supportContinuousDownload,
String httpProxy) {
StringBuilder commandStr = new StringBuilder();
commandStr.append(commandPrefix);
commandStr.append(" ");
commandStr.append(command);
commandStr.append(" ");
commandStr.append(remoteFilePath);
commandStr.append(" ");
commandStr.append(getLocalFileDirectory(localFilePath));
commandStr.append(" ");
commandStr.append(localFilePath);
commandStr.append(" ");
commandStr.append(supportContinuousDownload ? "continue"
: "no-continue");
commandStr.append(" ");
commandStr.append(httpProxy);
commandStr.append(" ");
commandStr.append(commandSuffix);
return commandStr.toString();
}
}
WgetWindows.java
package com.karl.learn;
public class WgetWindows extends Wget {
private final String WIN_DOWNLOAD_COMMAND_PREFIX = "";// "cmd /c start /b";
private final String WIN_DOWNLOAD_COMMAND = "c:\\download\\download_content.cmd";
private final String WIN_DOWNLOAD_COMMAND_SUFFIX = "";
@Override
protected String getLocalFileDirectory(String localFilePath) {
int lastSlash = localFilePath.lastIndexOf("\\");
return localFilePath.substring(0, lastSlash);
}
@Override
protected String buildWgetCommand(String remotePath, String localPath,
boolean supportContinuousDownload, String httpProxy) {
return buildCommandForDownload(WIN_DOWNLOAD_COMMAND_PREFIX,
WIN_DOWNLOAD_COMMAND, WIN_DOWNLOAD_COMMAND_SUFFIX, remotePath,
localPath, supportContinuousDownload, httpProxy);
}
public class WgetWindows extends Wget {
private final String WIN_DOWNLOAD_COMMAND_PREFIX = "";// "cmd /c start /b";
private final String WIN_DOWNLOAD_COMMAND = "c:\\download\\download_content.cmd";
private final String WIN_DOWNLOAD_COMMAND_SUFFIX = "";
@Override
protected String getLocalFileDirectory(String localFilePath) {
int lastSlash = localFilePath.lastIndexOf("\\");
return localFilePath.substring(0, lastSlash);
}
@Override
protected String buildWgetCommand(String remotePath, String localPath,
boolean supportContinuousDownload, String httpProxy) {
return buildCommandForDownload(WIN_DOWNLOAD_COMMAND_PREFIX,
WIN_DOWNLOAD_COMMAND, WIN_DOWNLOAD_COMMAND_SUFFIX, remotePath,
localPath, supportContinuousDownload, httpProxy);
}
}
WgetUnix.java
package com.karl.learn;
public class WgetUnix extends Wget {
private final String UNIX_DOWNLOAD_COMMAND_PREFIX = "nohup";
private final String UNIX_DOWNLOAD_COMMAND = "/content/download_content.sh";
private final String UNIX_DOWNLOAD_COMMAND_SUFFIX = "";
@Override
protected String getLocalFileDirectory(String localFilePath) {
int lastSlash = localFilePath.lastIndexOf("/");
return localFilePath.substring(0, lastSlash);
}
@Override
protected String buildWgetCommand(String remotePath, String localPath,
boolean supportContinuousDownload, String httpProxy) {
return buildCommandForDownload(UNIX_DOWNLOAD_COMMAND_PREFIX, UNIX_DOWNLOAD_COMMAND,
UNIX_DOWNLOAD_COMMAND_SUFFIX, remotePath, localPath, supportContinuousDownload, httpProxy);
}
}
public class WgetUnix extends Wget {
private final String UNIX_DOWNLOAD_COMMAND_PREFIX = "nohup";
private final String UNIX_DOWNLOAD_COMMAND = "/content/download_content.sh";
private final String UNIX_DOWNLOAD_COMMAND_SUFFIX = "";
@Override
protected String getLocalFileDirectory(String localFilePath) {
int lastSlash = localFilePath.lastIndexOf("/");
return localFilePath.substring(0, lastSlash);
}
@Override
protected String buildWgetCommand(String remotePath, String localPath,
boolean supportContinuousDownload, String httpProxy) {
return buildCommandForDownload(UNIX_DOWNLOAD_COMMAND_PREFIX, UNIX_DOWNLOAD_COMMAND,
UNIX_DOWNLOAD_COMMAND_SUFFIX, remotePath, localPath, supportContinuousDownload, httpProxy);
}
}
download_content.cmd
@echo off
setlocal
set CONTENT_URL=%1
set LOCAL_PATH=%2
set LOCAL_FILE=%3
set CONTINUE=%4
set HTTP_PROXY=%5
if "%CONTINUE%" == "" goto USAGE
set LOCAL_SRC_FILE=%LOCAL_FILE%.sourceurl
set LOCAL_PART_FILE=%LOCAL_FILE%.part
set LOCAL_FAIL_FILE=%LOCAL_FILE%.fail
set D_TIMEOUT=90
if "%HTTP_PROXY%"=="" goto DO_WGET_PREPARE
:SET_PROXY
set http_proxy=%HTTP_PROXY%
set no_proxy=localhost,127.0.0.1
REM also may set http_proxy in C:\Program Files\GnuWin32\etc\wgetrc
goto DO_WGET_PREPARE
:DO_WGET_PREPARE
mkdir %LOCAL_PATH%
echo %CONTENT_URL% > %LOCAL_SRC_FILE%
del /F /Q %LOCAL_FAIL_FILE%
set PARAM=--quiet --timeout=%D_TIMEOUT%
if "%CONTINUE%"=="continue" set PARAM=%PARAM% --continue
if "%CONTINUE%"=="continue" goto DO_WGET
:DEL_LEGACY_FILE
del /F /Q %LOCAL_PART_FILE%
:DO_WGET
"C:\Program Files\GnuWin32\bin\wget" -O %LOCAL_PART_FILE% %PARAM% %CONTENT_URL%
set RESULT=%errorlevel%
if "%RESULT%"=="0" goto SUCCESS_HANDLE
goto FAIL_HANDLE
:FAIL_HANDLE
REM del /F /Q %LOCAL_PART_FILE%
echo %RESULT% > %LOCAL_FAIL_FILE%
goto END
:SUCCESS_HANDLE
move %LOCAL_PART_FILE% %LOCAL_FILE%
goto END
:USAGE
echo Usage:
echo download_content CONTENT_URL LOCAL_PATH LOCAL_FILE continue|no-continue [HTTP_PROXY]
echo .
goto END
:END
endlocal
setlocal
set CONTENT_URL=%1
set LOCAL_PATH=%2
set LOCAL_FILE=%3
set CONTINUE=%4
set HTTP_PROXY=%5
if "%CONTINUE%" == "" goto USAGE
set LOCAL_SRC_FILE=%LOCAL_FILE%.sourceurl
set LOCAL_PART_FILE=%LOCAL_FILE%.part
set LOCAL_FAIL_FILE=%LOCAL_FILE%.fail
set D_TIMEOUT=90
if "%HTTP_PROXY%"=="" goto DO_WGET_PREPARE
:SET_PROXY
set http_proxy=%HTTP_PROXY%
set no_proxy=localhost,127.0.0.1
REM also may set http_proxy in C:\Program Files\GnuWin32\etc\wgetrc
goto DO_WGET_PREPARE
:DO_WGET_PREPARE
mkdir %LOCAL_PATH%
echo %CONTENT_URL% > %LOCAL_SRC_FILE%
del /F /Q %LOCAL_FAIL_FILE%
set PARAM=--quiet --timeout=%D_TIMEOUT%
if "%CONTINUE%"=="continue" set PARAM=%PARAM% --continue
if "%CONTINUE%"=="continue" goto DO_WGET
:DEL_LEGACY_FILE
del /F /Q %LOCAL_PART_FILE%
:DO_WGET
"C:\Program Files\GnuWin32\bin\wget" -O %LOCAL_PART_FILE% %PARAM% %CONTENT_URL%
set RESULT=%errorlevel%
if "%RESULT%"=="0" goto SUCCESS_HANDLE
goto FAIL_HANDLE
:FAIL_HANDLE
REM del /F /Q %LOCAL_PART_FILE%
echo %RESULT% > %LOCAL_FAIL_FILE%
goto END
:SUCCESS_HANDLE
move %LOCAL_PART_FILE% %LOCAL_FILE%
goto END
:USAGE
echo Usage:
echo download_content CONTENT_URL LOCAL_PATH LOCAL_FILE continue|no-continue [HTTP_PROXY]
echo .
goto END
:END
endlocal
echo off
download_content.sh
#!/bin/sh
CONTENT_URL=$1
LOCAL_DIR=$2
LOCAL_FILE=$3
CONTINUE=$4
HTTP_PROXY=$5
LOCAL_SRC_FILE=$LOCAL_FILE.sourceurl
LOCAL_PART_FILE=$LOCAL_FILE.part
LOCAL_FAIL_FILE=$LOCAL_FILE.fail
LOCAL_PID_FILE=$LOCAL_FILE.pid
D_TIMEOUT=90
# check if local file is a directory
if [ -d $LOCAL_FILE ]
then
echo "error: local file is a directory."
touch $LOCAL_FAIL_FILE
echo "error: local file is a directory." > $LOCAL_FAIL_FILE
exit 2
fi
# set proxy server
if [ ! "$HTTP_PROXY" = "" ]
then
export http_proxy=$HTTP_PROXY
export no_proxy=localhost,127.0.0.1
fi
# create local path, if path exists, ignore error
mkdir -p $LOCAL_DIR
chmod 777 $LOCAL_DIR
# generate src file, which contains content URL
echo $CONTENT_URL > $LOCAL_SRC_FILE
# generate pid file, which contains current script process id
echo $$ > $LOCAL_PID_FILE
# if fail file exists, delete it
if [ -e $LOCAL_FAIL_FILE ]
then
rm -f $LOCAL_FAIL_FILE
fi
# delete legacy file for NO-Continue downloading
if [ "x$CONTINUE" != "xcontinue" ]
then
rm -f $LOCAL_PART_FILE
fi
# prepare WGET parameters
PARAM="--quiet --timeout=$D_TIMEOUT"
if [ "x$CONTINUE" = "xcontinue" ]
then
PARAM="$PARAM --continue"
fi
########### download ##############
wget -O $LOCAL_PART_FILE $PARAM $CONTENT_URL
RESULT=$?
if [ "x$RESULT" = "x0" ]
then
mv $LOCAL_PART_FILE $LOCAL_FILE
rm -f $LOCAL_FAIL_FILE
else
# rm -f $LOCAL_PART_FILE
echo $RESULT > $LOCAL_FAIL_FILE
fi
# finalizing...
CONTENT_URL=$1
LOCAL_DIR=$2
LOCAL_FILE=$3
CONTINUE=$4
HTTP_PROXY=$5
LOCAL_SRC_FILE=$LOCAL_FILE.sourceurl
LOCAL_PART_FILE=$LOCAL_FILE.part
LOCAL_FAIL_FILE=$LOCAL_FILE.fail
LOCAL_PID_FILE=$LOCAL_FILE.pid
D_TIMEOUT=90
# check if local file is a directory
if [ -d $LOCAL_FILE ]
then
echo "error: local file is a directory."
touch $LOCAL_FAIL_FILE
echo "error: local file is a directory." > $LOCAL_FAIL_FILE
exit 2
fi
# set proxy server
if [ ! "$HTTP_PROXY" = "" ]
then
export http_proxy=$HTTP_PROXY
export no_proxy=localhost,127.0.0.1
fi
# create local path, if path exists, ignore error
mkdir -p $LOCAL_DIR
chmod 777 $LOCAL_DIR
# generate src file, which contains content URL
echo $CONTENT_URL > $LOCAL_SRC_FILE
# generate pid file, which contains current script process id
echo $$ > $LOCAL_PID_FILE
# if fail file exists, delete it
if [ -e $LOCAL_FAIL_FILE ]
then
rm -f $LOCAL_FAIL_FILE
fi
# delete legacy file for NO-Continue downloading
if [ "x$CONTINUE" != "xcontinue" ]
then
rm -f $LOCAL_PART_FILE
fi
# prepare WGET parameters
PARAM="--quiet --timeout=$D_TIMEOUT"
if [ "x$CONTINUE" = "xcontinue" ]
then
PARAM="$PARAM --continue"
fi
########### download ##############
wget -O $LOCAL_PART_FILE $PARAM $CONTENT_URL
RESULT=$?
if [ "x$RESULT" = "x0" ]
then
mv $LOCAL_PART_FILE $LOCAL_FILE
rm -f $LOCAL_FAIL_FILE
else
# rm -f $LOCAL_PART_FILE
echo $RESULT > $LOCAL_FAIL_FILE
fi
# finalizing...
rm -f $LOCAL_PID_FILE