去http://tomcat.apache.org/下载Tomcat,下载JDK,JRE,配置开发环境,但是在cmd中进入Tomcat目录bin目录,虽然我的环境变量都配置正确了,但执行service.bat install Tomcat的时候报错
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
看了下service.bat文件,就是没有找到这个环境变量。但看到里面引用了setclasspath.bat,所以手动修改了下发现可以了。方法如下:打开bin下面的setclasspath.bat,大概在第22行处手动设置
rem ---------------------------------------------------------------------------
rem Set JAVA_HOME or JRE_HOME if not already set, ensure any provided settings
rem are valid and consistent with the selected start-up options and set up the
rem endorsed directory.
rem ---------------------------------------------------------------------------
set JAVA_HOME=H:\Java\jdk1.8.0_91
set JRE_HOME=H:\Java\jre
rem Make sure prerequisite environment variables are set
rem In debug mode we need a real JDK (JAVA_HOME)
if ""%1"" == ""debug"" goto needJavaHome
rem Otherwise either JRE or JDK are fine
if not "%JRE_HOME%" == "" goto gotJreHome
if not "%JAVA_HOME%" == "" goto gotJavaHome
echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
上面红色部分就是自己手动添加的
启动后打开tomcat8w.exe启动服务,打开localhost:8080,若跳出主页界面,则说明运行成功,若失败了则先手动运行shutdown.bat,然后再运行startup.bat。
启动成功后,修改配置文件
1、在tomcat 安装目录\conf\Catalina\localhost下建立任意文件名xml文件,比如:download.xml,docBase后即为http下载服务器的路径
内容如下:
2、配置web.xml(tomcat的配置文件),修改如下配置:
要将红色的false改为true。
3、重启tomcat。
此时在浏览器中运行http://localhost:8080/download/就可以看到D:\download下的文件,此时就可以用http下载的方式下载文件。
============================================================
示例代码如下:
#include
#include
#include
#pragma comment(lib,"urlmon.lib")
using namespace std;
class DownloadProgress : public IBindStatusCallback {
public:
HRESULT __stdcall QueryInterface(const IID &, void **) {
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE AddRef(void) {
return 1;
}
ULONG STDMETHODCALLTYPE Release(void) {
return 1;
}
HRESULT STDMETHODCALLTYPE OnStartBinding(DWORD dwReserved, IBinding *pib) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetPriority(LONG *pnPriority) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnLowResource(DWORD reserved) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE OnStopBinding(HRESULT hresult, LPCWSTR szError) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetBindInfo(DWORD *grfBINDF, BINDINFO *pbindinfo) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(REFIID riid, IUnknown *punk) {
return E_NOTIMPL;
}
virtual HRESULT __stdcall OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
{
if (ulProgressMax != 0)
{
double *percentage = new double(ulProgress * 1.0 / ulProgressMax * 100);
//将percentage 发送给显示
cout << *percentage << endl;
delete percentage;
}
return S_OK;
}
};
int main()
{
DownloadProgress progress;
IBindStatusCallback* callback = (IBindStatusCallback*)&progress;
HRESULT hr = URLDownloadToFileA(NULL,
"http://localhost:8080/download/compare.exe",
"F:\\compare.exe",
0,
static_cast(&progress));
if (hr == S_OK)
cout << "Download ok" << endl;
else
{
cout << "Download Error" << endl;
}
system("pause");
}
其中URLDownloadToFileA的第五个参数,是为了响应下载进度
的。若不需要可置空。
另外还有一个问题就是UrlDownloadToFile会先检查缓存,如果缓存里有,则不重新下载了。所以当服务器上的文件更改后,下载的还是原来的文件,要避免这种情况就要在下载之前删除掉缓存
#include
#pragma comment(lib,"urlmon.lib")
#pragma comment(lib, "wininet.lib")
DeleteUrlCacheEntry("http://localhost:8080/download/compare.exe");