java 实现下载功能
/**
* 下载文件
* @param filePath --文件完整路径
* @param response --HttpServletResponse对象
*/
public static void downloadFile(
String filePath,
javax.servlet.http.HttpServletResponse response) {
String fileName = ""; //文件名,输出到用户的下载对话框
//从文件完整路径中提取文件名,并进行编码转换,防止不能正确显示中文名
try {
if(filePath.lastIndexOf("/") > 0) {
fileName = new String(filePath.substring(filePath.lastIndexOf("/")+1, filePath.length()).getBytes("GB2312"), "ISO8859_1");
}else if(filePath.lastIndexOf("\\") > 0) {
fileName = new String(filePath.substring(filePath.lastIndexOf("\\")+1, filePath.length()).getBytes("GB2312"), "ISO8859_1");
}
}catch(Exception e) {}
//打开指定文件的流信息
FileInputStream fs = null;
try {
fs = new FileInputStream(new File(filePath));
}catch(FileNotFoundException e) {
e.printStackTrace();
return;
}
//设置响应头和保存文件名
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
//写出流信息
int b = 0;
try {
PrintWriter out = response.getWriter();
while((b=fs.read())!=-1) {
out.write(b);
}
fs.close();
out.close();
System.out.println("文件下载完毕.");
}catch(Exception e) {
e.printStackTrace();
System.out.println("下载文件失败!");
}
}
方法在表现层里调用,比如Struts或Servlet里。
相关文档:
在Java
虚拟机中,被加载类型的信息都保存在方法区中。这些信息在内存中的组织形式由虚拟机的实现者定义,比如,虚拟机工作在一个“little- endian”
的处理器上,他就可以将信息保存为“little-endian”
格式的,虽然在Java
类文件中他们是以“big-endian”
格式保 存的。设 ......
下载 dom4j-1.6.1.jar。
1: package org.zzp.common.xml.dom4j;
2:
3: import java.io.FileWriter;
4: import java.io.IOException;
5: import org.dom4j.Document;
6: import org.dom4j.DocumentHelper;
7: import org.dom4j.Element;
8: import org.dom4j.io.OutputFormat;
9: impor ......
记得第一次接触闭包的时候,觉得很奇怪,但从字面上很那理解闭包什么玩意,和闭包有的一比的当属控制反转,真正理解了后觉得就平常了。闭包二字其实是很经典的,闭包代码所表现的东西可不就是一个封闭的、自成一体的功能块吗?简单的说,闭包就是在内部定义的方法,拿到外面去使用。经典的javascript闭包形式如下:
Java代 ......
Introduction to XML and XML With Java
If you are looking for sample programs to parse a XML file using DOM/SAX parser or looking for a program to generate a XML file please proceed directly to programs.
This small tutorial introduces you to the basic concepts of XML and using Xer ......