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里。
相关文档:
堆栈是一种先进后出的数据结构,只能在一端进行输入或输出数据的操作 Stack类在java.util包中
向栈中输入数据的操作称为“压栈”;而从栈中输出数据的操作称为“弹栈”
1.构造方法:Stack()
2.常用方法
public Object push(Object data):向栈中输入数据,实现压栈 ......
一、JSON 是什么?
JSON 的全称是JavaScript Object Notation,是一种轻量级的数据交换格式。
JSON 与XML 具有相同的特性,例如易于人编写和阅读,易于机器生成和解析。但是JSON 比
XML 数据传输的有效性要高出很多。JSON 完全独立与编程语言,使用文本格式保存。
JSON 数据有两种结构:
• Name-Value 对构成的集 ......
需要dom4j.jar文件 ,自行下载。 test.xml 1: <?xml version="1.0" encoding="gbk"?>
2:
3: <students>
4: <person sex="男" age="21">
5: <id>1</id>
6: <name>章治鹏</name>
7: <homepage&g ......
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 ......