对于wap页面下载软件记录下
URL要带文件名,否则手机不支持
例如:http://localhost:8080/client/test.CAB
symbian手机对于IP:8080/方式貌似不支持,需要http://域名:端口/client/test.CAB
下载方式可以有2种:
1)wap标签连接
<wall:a href="<%=filePath%>">download</wall:a>
2)servlet write文件流返回(字节流)
配置一个DownLoadServlet
web.xml:
<servlet>
<servlet-name>downLoadClientServlet</servlet-name>
<servlet-class>com.test.DownLoadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>downLoadClientServlet</servlet-name>
<url-pattern>/client/*</url-pattern>
</servlet-mapping>
DownLoadServlet:
- public class DownLoadServlet extends HttpServlet {
- private GeneralLogger logger = LoggerFactory.getLogger(DownLoadServlet.class);
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- logger.debug("------------------------------------------------------------------------------------");
- try {
- String filePath = "d:/mytest/client/test.CAB";
- logger.debug("filePath = " + filePath);
- sendReply(filePath, response);
- } catch (Exception e) {
- logger.error("error :" + e.toString());
- }
- logger.debug("------------------------------------------------------------------------------------");
- }
- public void sendReply(String filePath, HttpServletResponse response) {
- File file = new File(filePath);
- if (file.exists()) {
- String fileName = file.getName();
- response.setContentType("application/x-download");//设置为下载application/x-download
- response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
- OutputStream output = null;
- FileInputStream fis = null;
- try {
- output = response.getOutputStream();
- fis = new FileInputStream(filePath);
- byte[] b = new byte[1024];
- int i = 0;
- while ((i = fis.read(b)) != -1) {
- output.write(b, 0, i);
- }
- output.flush();
- b = null;
- }
- catch (Exception e) {
- logger.error("error :" + e.toString());
- }
- finally {
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- fis = null;
- }
- if (output != null) {
- try {
- output.close();
- } catch (IOException e) {
- //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- output = null;
- }
- }
- }
- }
- }
public class DownLoadServlet extends HttpServlet {
private GeneralLogger logger = LoggerFactory.getLogger(DownLoadServlet.class);
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.debug("------------------------------------------------------------------------------------");
try {
String filePath = "d:/mytest/client/test.CAB";
logger.debug("filePath = " + filePath);
sendReply(filePath, response);
} catch (Exception e) {
logger.error("error :" + e.toString());
}
logger.debug("------------------------------------------------------------------------------------");
}
public void sendReply(String filePath, HttpServletResponse response) {
File file = new File(filePath);
if (file.exists()) {
String fileName = file.getName();
response.setContentType("application/x-download");//设置为下载application/x-download
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
OutputStream output = null;
FileInputStream fis = null;
try {
output = response.getOutputStream();
fis = new FileInputStream(filePath);
byte[] b = new byte[1024];
int i = 0;
while ((i = fis.read(b)) != -1) {
output.write(b, 0, i);
}
output.flush();
b = null;
}
catch (Exception e) {
logger.error("error :" + e.toString());
}
finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
//e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
fis = null;
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
//e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
output = null;
}
}
}
}
}
顺便说下,第一种方式软件要放在web文件夹或子文件夹下,否则tomcat等容器是不允许访问的
第二种方式就比较灵活些,对于手机客户端软件升级,后台连接,可采用第二种方式。